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
|
2 |
|
public function __construct($namespace = 'default') { |
28
|
2 |
|
$this->setNamespace($namespace); |
29
|
2 |
|
$this->clear(); |
30
|
2 |
|
} |
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
|
4 |
|
public function clear() { |
88
|
4 |
|
$namespace = $this->getNamespace(); |
89
|
|
|
|
90
|
4 |
|
if (!isset(\ElggStaticVariableCache::$__cache)) { |
91
|
1 |
|
\ElggStaticVariableCache::$__cache = array(); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
4 |
|
\ElggStaticVariableCache::$__cache[$namespace] = array(); |
95
|
4 |
|
} |
96
|
|
|
} |
97
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.