1: <?php
2: /**
3: * ElggStaticVariableCache
4: * Dummy cache which stores values in a static array. Using this makes future
5: * replacements to other caching back ends (eg memcache) much easier.
6: *
7: * @package Elgg.Core
8: * @subpackage Cache
9: */
10: class ElggStaticVariableCache extends ElggSharedMemoryCache {
11: /**
12: * The cache.
13: *
14: * @var array
15: */
16: private static $__cache;
17:
18: /**
19: * Create the variable cache.
20: *
21: * This function creates a variable cache in a static variable in
22: * memory, optionally with a given namespace (to avoid overlap).
23: *
24: * @param string $namespace The namespace for this cache to write to.
25: * @warning namespaces of the same name are shared!
26: */
27: function __construct($namespace = 'default') {
28: $this->setNamespace($namespace);
29: $this->clear();
30: }
31:
32: /**
33: * Save a key
34: *
35: * @param string $key Name
36: * @param string $data Value
37: *
38: * @return boolean
39: */
40: public function save($key, $data) {
41: $namespace = $this->getNamespace();
42:
43: ElggStaticVariableCache::$__cache[$namespace][$key] = $data;
44:
45: return true;
46: }
47:
48: /**
49: * Load a key
50: *
51: * @param string $key Name
52: * @param int $offset Offset
53: * @param int $limit Limit
54: *
55: * @return string
56: */
57: public function load($key, $offset = 0, $limit = null) {
58: $namespace = $this->getNamespace();
59:
60: if (isset(ElggStaticVariableCache::$__cache[$namespace][$key])) {
61: return ElggStaticVariableCache::$__cache[$namespace][$key];
62: }
63:
64: return false;
65: }
66:
67: /**
68: * Invalidate a given key.
69: *
70: * @param string $key Name
71: *
72: * @return bool
73: */
74: public function delete($key) {
75: $namespace = $this->getNamespace();
76:
77: unset(ElggStaticVariableCache::$__cache[$namespace][$key]);
78:
79: return true;
80: }
81:
82: /**
83: * Clears the cache for a particular namespace
84: *
85: * @return void
86: */
87: public function clear() {
88: $namespace = $this->getNamespace();
89:
90: if (!isset(ElggStaticVariableCache::$__cache)) {
91: ElggStaticVariableCache::$__cache = array();
92: }
93:
94: ElggStaticVariableCache::$__cache[$namespace] = array();
95: }
96: }
97: