Memory.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\storage\cache\adapter;
  9. /**
  10. * A minimal in-memory cache.
  11. *
  12. * This Memory adapter provides basic support for `write`, `read`, `delete`
  13. * and `clear` cache functionality, as well as allowing the first four
  14. * methods to be filtered as per the Lithium filtering system.
  15. *
  16. * This cache adapter does not implement any expiry-based cache invalidation
  17. * logic, as the cached data will only persist for the lifetime of the current request.
  18. *
  19. * As a result, this cache adapter is best suited for generic memoization of data, and
  20. * should not be used for for anything that must persist longer than the current
  21. * request cycle.
  22. */
  23. class Memory extends \lithium\core\Object {
  24. /**
  25. * Array used to store cached data by this adapter
  26. *
  27. * @var array
  28. */
  29. protected $_cache = array();
  30. /**
  31. * Magic method to provide an accessor (getter) to protected class variables.
  32. *
  33. * @param string $variable The variable requested.
  34. * @return mixed Variable if it exists, null otherwise.
  35. */
  36. public function __get($variable) {
  37. if (isset($this->{"_$variable"})) {
  38. return $this->{"_$variable"};
  39. }
  40. }
  41. /**
  42. * Read value(s) from the cache
  43. *
  44. * Note: When using an array of keys in $key for multi-read,
  45. * note that this is not an atomic operation.
  46. *
  47. * @param string $key The key to uniquely identify the cached item.
  48. * @return closure Function returning cached value if successful, `false` otherwise.
  49. */
  50. public function read($key) {
  51. $cache =& $this->_cache;
  52. return function($self, $params) use (&$cache) {
  53. extract($params);
  54. if (is_array($key)) {
  55. $results = array();
  56. foreach ($key as $k) {
  57. if (isset($cache[$k])) {
  58. $results[$k] = $cache[$k];
  59. }
  60. }
  61. return $results;
  62. }
  63. return isset($cache[$key]) ? $cache[$key] : null;
  64. };
  65. }
  66. /**
  67. * Write value(s) to the cache.
  68. *
  69. * Note: When using an array of keys => values in $key for multi-write,
  70. * note that this is not an atomic operation.
  71. *
  72. * @param string $key The key to uniquely identify the cached item.
  73. * @param mixed $data The value to be cached.
  74. * @param string $expiry A strtotime() compatible cache time.
  75. * @return closure Function returning boolean `true` on successful write, `false` otherwise.
  76. */
  77. public function write($key, $data, $expiry) {
  78. $cache =& $this->_cache;
  79. return function($self, $params) use (&$cache) {
  80. extract($params);
  81. if (is_array($key)) {
  82. foreach ($key as $k => &$v) {
  83. $cache[$k] = $v;
  84. }
  85. return true;
  86. }
  87. return (boolean) ($cache[$key] = $data);
  88. };
  89. }
  90. /**
  91. * Delete value from the cache
  92. *
  93. * @param string $key The key to uniquely identify the cached item.
  94. * @return closure Function returning boolean `true` on successful delete, `false` otherwise.
  95. */
  96. public function delete($key) {
  97. $cache =& $this->_cache;
  98. return function($self, $params) use (&$cache) {
  99. extract($params);
  100. if (isset($cache[$key])) {
  101. unset($cache[$key]);
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. };
  107. }
  108. /**
  109. * Performs a decrement operation on specified numeric cache item.
  110. *
  111. * @param string $key Key of numeric cache item to decrement.
  112. * @param integer $offset Offset to decrement - defaults to 1.
  113. * @return closure Function returning item's new value on successful decrement,
  114. * `false` otherwise.
  115. */
  116. public function decrement($key, $offset = 1) {
  117. $cache =& $this->_cache;
  118. return function($self, $params) use (&$cache, $offset) {
  119. extract($params);
  120. return $cache[$key] -= 1;
  121. };
  122. }
  123. /**
  124. * Performs an increment operation on specified numeric cache item.
  125. *
  126. * @param string $key Key of numeric cache item to increment.
  127. * @param integer $offset Offset to increment - defaults to 1.
  128. * @return closure Function returning item's new value on successful increment,
  129. * `false` otherwise.
  130. */
  131. public function increment($key, $offset = 1) {
  132. $cache =& $this->_cache;
  133. return function($self, $params) use (&$cache, $offset) {
  134. extract($params);
  135. return $cache[$key] += 1;
  136. };
  137. }
  138. /**
  139. * Clears user-space cache
  140. *
  141. * @return mixed True on successful clear, false otherwise.
  142. */
  143. public function clear() {
  144. foreach ($this->_cache as $key => &$value) {
  145. unset($this->_cache[$key]);
  146. }
  147. return true;
  148. }
  149. /**
  150. * This adapter is always enabled, as it has no external dependencies.
  151. *
  152. * @return boolean True
  153. */
  154. public static function enabled() {
  155. return true;
  156. }
  157. /**
  158. * Garbage collection (GC) is not enabled for this adapter
  159. *
  160. * @return boolean False
  161. */
  162. public function clean() {
  163. return false;
  164. }
  165. }
  166. ?>