MapCacheLRU.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * Per-process memory cache for storing items.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Cache
  22. */
  23. use Wikimedia\Assert\Assert;
  24. /**
  25. * Handles a simple LRU key/value map with a maximum number of entries
  26. *
  27. * The last-modification timestamp of entries is internally tracked so that callers can
  28. * specify the maximum acceptable age of entries in calls to the has() method. As a convenience,
  29. * the hasField(), getField(), and setField() methods can be used for entries that are field/value
  30. * maps themselves; such fields will have their own internally tracked last-modification timestamp.
  31. *
  32. * @see ProcessCacheLRU
  33. * @ingroup Cache
  34. * @since 1.23
  35. */
  36. class MapCacheLRU implements IExpiringStore, Serializable {
  37. /** @var array Map of (key => value) */
  38. private $cache = [];
  39. /** @var array Map of (key => (UNIX timestamp, (field => UNIX timestamp))) */
  40. private $timestamps = [];
  41. /** @var float Default entry timestamp if not specified */
  42. private $epoch;
  43. /** @var int Max number of entries */
  44. private $maxCacheKeys;
  45. /** @var float|null */
  46. private $wallClockOverride;
  47. /** @var float */
  48. const RANK_TOP = 1.0;
  49. /** @var int Array key that holds the entry's main timestamp (flat key use) */
  50. const SIMPLE = 0;
  51. /** @var int Array key that holds the entry's field timestamps (nested key use) */
  52. const FIELDS = 1;
  53. /**
  54. * @param int $maxKeys Maximum number of entries allowed (min 1)
  55. * @throws Exception When $maxKeys is not an int or not above zero
  56. */
  57. public function __construct( $maxKeys ) {
  58. Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
  59. Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
  60. $this->maxCacheKeys = $maxKeys;
  61. // Use the current time as the default "as of" timestamp of entries
  62. $this->epoch = $this->getCurrentTime();
  63. }
  64. /**
  65. * @param array $values Key/value map in order of increasingly recent access
  66. * @param int $maxKeys
  67. * @return MapCacheLRU
  68. * @since 1.30
  69. */
  70. public static function newFromArray( array $values, $maxKeys ) {
  71. $mapCache = new self( $maxKeys );
  72. $mapCache->cache = ( count( $values ) > $maxKeys )
  73. ? array_slice( $values, -$maxKeys, null, true )
  74. : $values;
  75. return $mapCache;
  76. }
  77. /**
  78. * @return array Key/value map in order of increasingly recent access
  79. * @since 1.30
  80. */
  81. public function toArray() {
  82. return $this->cache;
  83. }
  84. /**
  85. * Set a key/value pair.
  86. * This will prune the cache if it gets too large based on LRU.
  87. * If the item is already set, it will be pushed to the top of the cache.
  88. *
  89. * To reduce evictions due to one-off use of many new keys, $rank can be
  90. * set to have keys start at the top of a bottom fraction of the list. A value
  91. * of 3/8 means values start at the top of the bottom 3/8s of the list and are
  92. * moved to the top of the list when accessed a second time.
  93. *
  94. * @param string $key
  95. * @param mixed $value
  96. * @param float $rank Bottom fraction of the list where keys start off [default: 1.0]
  97. * @return void
  98. */
  99. public function set( $key, $value, $rank = self::RANK_TOP ) {
  100. if ( $this->has( $key ) ) {
  101. $this->ping( $key );
  102. } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
  103. reset( $this->cache );
  104. $evictKey = key( $this->cache );
  105. unset( $this->cache[$evictKey] );
  106. unset( $this->timestamps[$evictKey] );
  107. }
  108. if ( $rank < 1.0 && $rank > 0 ) {
  109. $offset = intval( $rank * count( $this->cache ) );
  110. $this->cache = array_slice( $this->cache, 0, $offset, true )
  111. + [ $key => $value ]
  112. + array_slice( $this->cache, $offset, null, true );
  113. } else {
  114. $this->cache[$key] = $value;
  115. }
  116. $this->timestamps[$key] = [
  117. self::SIMPLE => $this->getCurrentTime(),
  118. self::FIELDS => []
  119. ];
  120. }
  121. /**
  122. * Check if a key exists
  123. *
  124. * @param string $key
  125. * @param float $maxAge Ignore items older than this many seconds [default: INF]
  126. * @return bool
  127. * @since 1.32 Added $maxAge
  128. */
  129. public function has( $key, $maxAge = INF ) {
  130. if ( !is_int( $key ) && !is_string( $key ) ) {
  131. throw new UnexpectedValueException(
  132. __METHOD__ . ': invalid key; must be string or integer.' );
  133. }
  134. if ( !array_key_exists( $key, $this->cache ) ) {
  135. return false;
  136. }
  137. return ( $maxAge <= 0 || $this->getAge( $key ) <= $maxAge );
  138. }
  139. /**
  140. * Get the value for a key.
  141. * This returns null if the key is not set.
  142. * If the item is already set, it will be pushed to the top of the cache.
  143. *
  144. * @param string $key
  145. * @param float $maxAge Ignore items older than this many seconds [default: INF]
  146. * @param mixed|null $default Value to return if no key is found [default: null]
  147. * @return mixed Returns $default if the key was not found or is older than $maxAge
  148. * @since 1.32 Added $maxAge
  149. * @since 1.34 Added $default
  150. */
  151. public function get( $key, $maxAge = INF, $default = null ) {
  152. if ( !$this->has( $key, $maxAge ) ) {
  153. return $default;
  154. }
  155. $this->ping( $key );
  156. return $this->cache[$key];
  157. }
  158. /**
  159. * @param string|int $key
  160. * @param string|int $field
  161. * @param mixed $value
  162. * @param float $initRank
  163. */
  164. public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
  165. if ( $this->has( $key ) ) {
  166. $this->ping( $key );
  167. } else {
  168. $this->set( $key, [], $initRank );
  169. }
  170. if ( !is_int( $field ) && !is_string( $field ) ) {
  171. throw new UnexpectedValueException(
  172. __METHOD__ . ": invalid field for '$key'; must be string or integer." );
  173. }
  174. if ( !is_array( $this->cache[$key] ) ) {
  175. $type = gettype( $this->cache[$key] );
  176. throw new UnexpectedValueException( "The value of '$key' ($type) is not an array." );
  177. }
  178. $this->cache[$key][$field] = $value;
  179. $this->timestamps[$key][self::FIELDS][$field] = $this->getCurrentTime();
  180. }
  181. /**
  182. * @param string|int $key
  183. * @param string|int $field
  184. * @param float $maxAge Ignore items older than this many seconds [default: INF]
  185. * @return bool
  186. * @since 1.32 Added $maxAge
  187. */
  188. public function hasField( $key, $field, $maxAge = INF ) {
  189. $value = $this->get( $key );
  190. if ( !is_int( $field ) && !is_string( $field ) ) {
  191. throw new UnexpectedValueException(
  192. __METHOD__ . ": invalid field for '$key'; must be string or integer." );
  193. }
  194. if ( !is_array( $value ) || !array_key_exists( $field, $value ) ) {
  195. return false;
  196. }
  197. return ( $maxAge <= 0 || $this->getAge( $key, $field ) <= $maxAge );
  198. }
  199. /**
  200. * @param string|int $key
  201. * @param string|int $field
  202. * @param float $maxAge Ignore items older than this many seconds [default: INF]
  203. * @return mixed Returns null if the key was not found or is older than $maxAge
  204. * @since 1.32 Added $maxAge
  205. */
  206. public function getField( $key, $field, $maxAge = INF ) {
  207. if ( !$this->hasField( $key, $field, $maxAge ) ) {
  208. return null;
  209. }
  210. return $this->cache[$key][$field];
  211. }
  212. /**
  213. * @return array
  214. * @since 1.25
  215. */
  216. public function getAllKeys() {
  217. return array_keys( $this->cache );
  218. }
  219. /**
  220. * Get an item with the given key, producing and setting it if not found.
  221. *
  222. * If the callback returns false, then nothing is stored.
  223. *
  224. * @since 1.28
  225. * @param string $key
  226. * @param callable $callback Callback that will produce the value
  227. * @param float $rank Bottom fraction of the list where keys start off [default: 1.0]
  228. * @param float $maxAge Ignore items older than this many seconds [default: INF]
  229. * @return mixed The cached value if found or the result of $callback otherwise
  230. * @since 1.32 Added $maxAge
  231. */
  232. public function getWithSetCallback(
  233. $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
  234. ) {
  235. if ( $this->has( $key, $maxAge ) ) {
  236. $value = $this->get( $key );
  237. } else {
  238. $value = call_user_func( $callback );
  239. if ( $value !== false ) {
  240. $this->set( $key, $value, $rank );
  241. }
  242. }
  243. return $value;
  244. }
  245. /**
  246. * Clear one or several cache entries, or all cache entries
  247. *
  248. * @param string|array|null $keys
  249. * @return void
  250. */
  251. public function clear( $keys = null ) {
  252. if ( func_num_args() == 0 ) {
  253. $this->cache = [];
  254. $this->timestamps = [];
  255. } else {
  256. foreach ( (array)$keys as $key ) {
  257. unset( $this->cache[$key] );
  258. unset( $this->timestamps[$key] );
  259. }
  260. }
  261. }
  262. /**
  263. * Get the maximum number of keys allowed
  264. *
  265. * @return int
  266. * @since 1.32
  267. */
  268. public function getMaxSize() {
  269. return $this->maxCacheKeys;
  270. }
  271. /**
  272. * Resize the maximum number of cache entries, removing older entries as needed
  273. *
  274. * @param int $maxKeys Maximum number of entries allowed (min 1)
  275. * @return void
  276. * @throws Exception When $maxKeys is not an int or not above zero
  277. * @since 1.32
  278. */
  279. public function setMaxSize( $maxKeys ) {
  280. Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
  281. Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
  282. $this->maxCacheKeys = $maxKeys;
  283. while ( count( $this->cache ) > $this->maxCacheKeys ) {
  284. reset( $this->cache );
  285. $evictKey = key( $this->cache );
  286. unset( $this->cache[$evictKey] );
  287. unset( $this->timestamps[$evictKey] );
  288. }
  289. }
  290. /**
  291. * Push an entry to the top of the cache
  292. *
  293. * @param string $key
  294. */
  295. private function ping( $key ) {
  296. $item = $this->cache[$key];
  297. unset( $this->cache[$key] );
  298. $this->cache[$key] = $item;
  299. }
  300. /**
  301. * @param string|int $key
  302. * @param string|int|null $field [optional]
  303. * @return float UNIX timestamp; the age of the given entry or entry field
  304. */
  305. private function getAge( $key, $field = null ) {
  306. if ( $field !== null ) {
  307. $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
  308. } else {
  309. $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
  310. }
  311. return ( $this->getCurrentTime() - $mtime );
  312. }
  313. public function serialize() {
  314. return serialize( [
  315. 'entries' => $this->cache,
  316. 'timestamps' => $this->timestamps
  317. ] );
  318. }
  319. public function unserialize( $serialized ) {
  320. $data = unserialize( $serialized );
  321. $this->cache = $data['entries'] ?? [];
  322. $this->timestamps = $data['timestamps'] ?? [];
  323. $this->epoch = $this->getCurrentTime();
  324. }
  325. /**
  326. * @return float UNIX timestamp
  327. * @codeCoverageIgnore
  328. */
  329. protected function getCurrentTime() {
  330. return $this->wallClockOverride ?: microtime( true );
  331. }
  332. /**
  333. * @param float|null &$time Mock UNIX timestamp for testing
  334. * @codeCoverageIgnore
  335. */
  336. public function setMockTime( &$time ) {
  337. $this->wallClockOverride =& $time;
  338. }
  339. }