ProcessCacheLRU.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  24. * Class for process caching individual properties of expiring items
  25. *
  26. * When the key for an entire item is deleted, all properties for it are deleted
  27. *
  28. * @ingroup Cache
  29. * @deprecated Since 1.32 Use MapCacheLRU instead
  30. */
  31. class ProcessCacheLRU {
  32. /** @var MapCacheLRU */
  33. protected $cache;
  34. /**
  35. * @param int $maxKeys Maximum number of entries allowed (min 1).
  36. * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
  37. */
  38. public function __construct( $maxKeys ) {
  39. $this->cache = new MapCacheLRU( $maxKeys );
  40. }
  41. /**
  42. * Set a property field for a cache entry.
  43. * This will prune the cache if it gets too large based on LRU.
  44. * If the item is already set, it will be pushed to the top of the cache.
  45. *
  46. * @param string $key
  47. * @param string $prop
  48. * @param mixed $value
  49. * @return void
  50. */
  51. public function set( $key, $prop, $value ) {
  52. $this->cache->setField( $key, $prop, $value );
  53. }
  54. /**
  55. * Check if a property field exists for a cache entry.
  56. *
  57. * @param string $key
  58. * @param string $prop
  59. * @param float $maxAge Ignore items older than this many seconds (since 1.21)
  60. * @return bool
  61. */
  62. public function has( $key, $prop, $maxAge = 0.0 ) {
  63. return $this->cache->hasField( $key, $prop, $maxAge );
  64. }
  65. /**
  66. * Get a property field for a cache entry.
  67. * This returns null if the property is not set.
  68. * If the item is already set, it will be pushed to the top of the cache.
  69. *
  70. * @param string $key
  71. * @param string $prop
  72. * @return mixed
  73. */
  74. public function get( $key, $prop ) {
  75. return $this->cache->getField( $key, $prop );
  76. }
  77. /**
  78. * Clear one or several cache entries, or all cache entries.
  79. *
  80. * @param string|array|null $keys
  81. * @return void
  82. */
  83. public function clear( $keys = null ) {
  84. $this->cache->clear( $keys );
  85. }
  86. /**
  87. * Resize the maximum number of cache entries, removing older entries as needed
  88. *
  89. * @param int $maxKeys
  90. * @return void
  91. * @throws UnexpectedValueException
  92. */
  93. public function resize( $maxKeys ) {
  94. $this->cache->setMaxSize( $maxKeys );
  95. }
  96. /**
  97. * Get cache size
  98. * @return int
  99. */
  100. public function getSize() {
  101. return $this->cache->getMaxSize();
  102. }
  103. }