1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require_once 'Zend/Cache.php'; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A decorator for a Zend_Cache_Backend cache service that mutates cache keys |
7
|
|
|
* dynamically depending on versioned state |
8
|
|
|
*/ |
9
|
|
|
class CacheProxy extends Zend_Cache_Core { |
10
|
|
|
/** |
11
|
|
|
* @var Zend_Cache_Backend|Zend_Cache_Backend_ExtendedInterface |
12
|
|
|
*/ |
13
|
|
|
protected $cache; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CacheProxy constructor. |
17
|
|
|
* @param Zend_Cache_Core $cache |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Zend_Cache_Core $cache) { |
20
|
|
|
$this->cache = $cache; |
|
|
|
|
21
|
|
|
|
22
|
|
|
parent::__construct(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setDirectives($directives) { |
26
|
|
|
$this->cache->setDirectives($directives); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setConfig(Zend_Config $config) { |
30
|
|
|
return $this->cache->setConfig($config); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setBackend(Zend_Cache_Backend $backendObject) { |
34
|
|
|
return $this->cache->setBackend($backendObject); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getBackend() { |
38
|
|
|
return $this->cache->getBackend(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setOption($name, $value) { |
42
|
|
|
$this->cache->setOption($name, $value); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getOption($name) { |
46
|
|
|
return $this->cache->getOption($name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setLifetime($newLifetime) { |
50
|
|
|
return $this->cache->setLifetime($newLifetime); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getIds() { |
54
|
|
|
return $this->cache->getIds(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getTags() { |
58
|
|
|
return $this->cache->getTags(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getIdsMatchingTags($tags = array()) { |
62
|
|
|
return $this->cache->getIdsMatchingTags($tags); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getIdsNotMatchingTags($tags = array()) { |
66
|
|
|
return $this->cache->getIdsNotMatchingTags($tags); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getIdsMatchingAnyTags($tags = array()) { |
70
|
|
|
return $this->cache->getIdsMatchingAnyTags($tags); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getFillingPercentage() { |
74
|
|
|
return $this->cache->getFillingPercentage(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getMetadatas($id) { |
78
|
|
|
return $this->cache->getMetadatas($this->getKeyID($id)); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function touch($id, $extraLifetime) { |
82
|
|
|
return $this->cache->touch($this->getKeyID($id), $extraLifetime); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false) { |
86
|
|
|
return $this->cache->load($this->getKeyID($id), $doNotTestCacheValidity, $doNotUnserialize); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test($id) { |
90
|
|
|
return $this->cache->test($this->getKeyID($id)); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) { |
94
|
|
|
return $this->cache->save( |
|
|
|
|
95
|
|
|
$data, |
96
|
|
|
$this->getKeyID($id), |
97
|
|
|
$tags, |
98
|
|
|
$specificLifetime, |
99
|
|
|
$priority |
|
|
|
|
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function remove($id) { |
104
|
|
|
return $this->cache->remove($this->getKeyID($id)); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { |
108
|
|
|
return $this->cache->clean($mode, $tags); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates a dynamic key based on versioned state |
113
|
|
|
* @param string $key |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function getKeyID($key) { |
117
|
|
|
$state = Versioned::get_reading_mode(); |
118
|
|
|
if ($state) { |
119
|
|
|
return $key . '_' . md5($state); |
120
|
|
|
} |
121
|
|
|
return $key; |
122
|
|
|
} |
123
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.