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