ElggCache.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * \ElggCache The elgg cache superclass.
  4. * This defines the interface for a cache (wherever that cache is stored).
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Cache
  8. */
  9. abstract class ElggCache implements \ArrayAccess {
  10. /**
  11. * Variables for the cache object.
  12. *
  13. * @var array
  14. */
  15. private $variables;
  16. /**
  17. * Set the constructor.
  18. */
  19. public function __construct() {
  20. $this->variables = array();
  21. }
  22. // @codingStandardsIgnoreStart
  23. /**
  24. * Set a cache variable.
  25. *
  26. * @param string $variable Name
  27. * @param string $value Value
  28. *
  29. * @return void
  30. *
  31. * @deprecated 1.8 Use \ElggCache:setVariable()
  32. */
  33. public function set_variable($variable, $value) {
  34. elgg_deprecated_notice('\ElggCache::set_variable() is deprecated by \ElggCache::setVariable()', 1.8);
  35. $this->setVariable($variable, $value);
  36. }
  37. // @codingStandardsIgnoreEnd
  38. /**
  39. * Set a cache variable.
  40. *
  41. * @param string $variable Name
  42. * @param string $value Value
  43. *
  44. * @return void
  45. */
  46. public function setVariable($variable, $value) {
  47. if (!is_array($this->variables)) {
  48. $this->variables = array();
  49. }
  50. $this->variables[$variable] = $value;
  51. }
  52. // @codingStandardsIgnoreStart
  53. /**
  54. * Get variables for this cache.
  55. *
  56. * @param string $variable Name
  57. *
  58. * @return mixed The value or null;
  59. *
  60. * @deprecated 1.8 Use \ElggCache::getVariable()
  61. */
  62. public function get_variable($variable) {
  63. elgg_deprecated_notice('\ElggCache::get_variable() is deprecated by \ElggCache::getVariable()', 1.8);
  64. return $this->getVariable($variable);
  65. }
  66. // @codingStandardsIgnoreEnd
  67. /**
  68. * Get variables for this cache.
  69. *
  70. * @param string $variable Name
  71. *
  72. * @return mixed The variable or null;
  73. */
  74. public function getVariable($variable) {
  75. if (isset($this->variables[$variable])) {
  76. return $this->variables[$variable];
  77. }
  78. return null;
  79. }
  80. /**
  81. * Class member get overloading, returning key using $this->load defaults.
  82. *
  83. * @param string $key Name
  84. *
  85. * @return mixed
  86. */
  87. public function __get($key) {
  88. return $this->load($key);
  89. }
  90. /**
  91. * Class member set overloading, setting a key using $this->save defaults.
  92. *
  93. * @param string $key Name
  94. * @param mixed $value Value
  95. *
  96. * @return void
  97. */
  98. public function __set($key, $value) {
  99. $this->save($key, $value);
  100. }
  101. /**
  102. * Supporting isset, using $this->load() with default values.
  103. *
  104. * @param string $key The name of the attribute or metadata.
  105. *
  106. * @return bool
  107. */
  108. public function __isset($key) {
  109. return (bool)$this->load($key);
  110. }
  111. /**
  112. * Supporting unsetting of magic attributes.
  113. *
  114. * @param string $key The name of the attribute or metadata.
  115. *
  116. * @return bool
  117. */
  118. public function __unset($key) {
  119. return $this->delete($key);
  120. }
  121. /**
  122. * Save data in a cache.
  123. *
  124. * @param string $key Name
  125. * @param string $data Value
  126. *
  127. * @return bool
  128. */
  129. abstract public function save($key, $data);
  130. /**
  131. * Load data from the cache using a given key.
  132. *
  133. * @todo $offset is a horrible variable name because it creates confusion
  134. * with the \ArrayAccess methods
  135. *
  136. * @param string $key Name
  137. * @param int $offset Offset
  138. * @param int $limit Limit
  139. *
  140. * @return mixed The stored data or false.
  141. */
  142. abstract public function load($key, $offset = 0, $limit = null);
  143. /**
  144. * Invalidate a key
  145. *
  146. * @param string $key Name
  147. *
  148. * @return bool
  149. */
  150. abstract public function delete($key);
  151. /**
  152. * Clear out all the contents of the cache.
  153. *
  154. * @return bool
  155. */
  156. abstract public function clear();
  157. /**
  158. * Add a key only if it doesn't already exist.
  159. * Implemented simply here, if you extend this class and your caching engine
  160. * provides a better way then override this accordingly.
  161. *
  162. * @param string $key Name
  163. * @param string $data Value
  164. *
  165. * @return bool
  166. */
  167. public function add($key, $data) {
  168. if (!isset($this[$key])) {
  169. return $this->save($key, $data);
  170. }
  171. return false;
  172. }
  173. // ARRAY ACCESS INTERFACE //////////////////////////////////////////////////////////
  174. /**
  175. * Assigns a value for the specified key
  176. *
  177. * @see \ArrayAccess::offsetSet()
  178. *
  179. * @param mixed $key The key (offset) to assign the value to.
  180. * @param mixed $value The value to set.
  181. *
  182. * @return void
  183. */
  184. public function offsetSet($key, $value) {
  185. $this->save($key, $value);
  186. }
  187. /**
  188. * Get the value for specified key
  189. *
  190. * @see \ArrayAccess::offsetGet()
  191. *
  192. * @param mixed $key The key (offset) to retrieve.
  193. *
  194. * @return mixed
  195. */
  196. public function offsetGet($key) {
  197. return $this->load($key);
  198. }
  199. /**
  200. * Unsets a key.
  201. *
  202. * @see \ArrayAccess::offsetUnset()
  203. *
  204. * @param mixed $key The key (offset) to unset.
  205. *
  206. * @return void
  207. */
  208. public function offsetUnset($key) {
  209. if (isset($this->$key)) {
  210. unset($this->$key);
  211. }
  212. }
  213. /**
  214. * Does key exist
  215. *
  216. * @see \ArrayAccess::offsetExists()
  217. *
  218. * @param mixed $key A key (offset) to check for.
  219. *
  220. * @return bool
  221. */
  222. public function offsetExists($key) {
  223. return isset($this->$key);
  224. }
  225. }