1 | <?php |
||
18 | class ConcreteCache extends Cache |
||
19 | { |
||
20 | /** |
||
21 | * @var \ArrayObject Database of cached items, |
||
22 | * we use ArrayObject so it can be easily |
||
23 | * passed by reference |
||
24 | * |
||
25 | * @since 1.1 |
||
26 | */ |
||
27 | protected $db; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * @throws \RuntimeException |
||
36 | */ |
||
37 | public function __construct($options = array()) |
||
38 | { |
||
39 | parent::__construct($options); |
||
40 | $this->db = new \ArrayObject; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * This will wipe out the entire cache's keys |
||
45 | * |
||
46 | * @return boolean The result of the clear operation. |
||
47 | * |
||
48 | * @since 1.0 |
||
49 | */ |
||
50 | public function clear() |
||
51 | { |
||
52 | // Replace the db with a new blank array |
||
53 | $clearData = $this->db->exchangeArray(array()); |
||
54 | unset($clearData); |
||
55 | |||
56 | return true; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Method to generate a Cache Item from a key. |
||
61 | * |
||
62 | * @param string $key The storage entry identifier. |
||
63 | * |
||
64 | * @return CacheItemInterface |
||
65 | * |
||
66 | * @since 1.0 |
||
67 | */ |
||
68 | public function get($key) |
||
69 | { |
||
70 | $item = $this->getItem($key); |
||
71 | |||
72 | return $item; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Method to get a storage entry value from a key. |
||
77 | * |
||
78 | * @param string $key The storage entry identifier. |
||
79 | * |
||
80 | * @return CacheItemInterface |
||
81 | * |
||
82 | * @since 1.0 |
||
83 | */ |
||
84 | public function getItem($key) |
||
85 | { |
||
86 | $item = new Item($key); |
||
87 | |||
88 | try |
||
89 | { |
||
90 | $value = $this->getValue($key); |
||
91 | $item->set($value); |
||
92 | } |
||
93 | catch ( \Exception $e) |
||
94 | { |
||
95 | /** |
||
96 | * Backend caching mechanisms may throw exceptions |
||
97 | * to indicate missing data. Catch all exceptions |
||
98 | * so program flow is uninterrupted. For misses |
||
99 | * we can safely do nothing and return the |
||
100 | * CacheItem we created since it flags itself as |
||
101 | * a miss when constructed. Specific cache classes |
||
102 | * should override this method and deal with |
||
103 | * exceptions appropriately. |
||
104 | */ |
||
105 | } |
||
106 | |||
107 | return $item; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Method to get a storage entry value from a key. |
||
112 | * |
||
113 | * @param string $key The storage entry identifier. |
||
114 | * |
||
115 | * @return mixed |
||
116 | * |
||
117 | * @since 1.0 |
||
118 | * @throws \Exception |
||
119 | */ |
||
120 | public function getValue($key) |
||
121 | { |
||
122 | try |
||
123 | { |
||
124 | $value = $this->db[$key]; |
||
125 | } |
||
126 | catch ( \Exception $e) |
||
127 | { |
||
128 | /** |
||
129 | * Backend caching mechanisms may throw exceptions |
||
130 | * to indicate missing data. Catch all exceptions |
||
131 | * so program flow is uninterrupted. If the exception |
||
132 | * can be recovered from gracefully, do so. If not |
||
133 | * re-throw the exception. As with getItem() this |
||
134 | * logic must be added for specific cache engine |
||
135 | * test suites. |
||
136 | */ |
||
137 | throw $e; |
||
138 | } |
||
139 | return $value; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Method to remove a storage entry for a key. |
||
144 | * |
||
145 | * @param string $key The storage entry identifier. |
||
146 | * |
||
147 | * @return boolean |
||
148 | * |
||
149 | * @since 1.0 |
||
150 | */ |
||
151 | public function deleteItem($key) |
||
152 | { |
||
153 | $oldCache = $this->db->getArrayCopy(); |
||
154 | |||
155 | if (array_key_exists($key, $oldCache)) |
||
156 | { |
||
157 | $keyArray = array($key => $key); |
||
158 | $newCache = array_diff_key($oldCache, $keyArray); |
||
159 | $this->db->exchangeArray($newCache); |
||
160 | |||
161 | return true; |
||
162 | } |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Method to set a value for a storage entry. |
||
169 | * |
||
170 | * @param CacheItemInterface $item The cache item to save. |
||
171 | * |
||
172 | * @return boolean |
||
173 | * |
||
174 | * @since 1.0 |
||
175 | */ |
||
176 | public function save(CacheItemInterface $item) |
||
177 | { |
||
178 | $this->db[$item->getKey()] = $item->get(); |
||
179 | |||
180 | return true; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Method to determine whether a storage entry has been set for a key. |
||
185 | * |
||
186 | * @param string $key The storage entry identifier. |
||
187 | * |
||
188 | * @return boolean |
||
189 | * |
||
190 | * @since 1.0 |
||
191 | */ |
||
192 | public function hasItem($key) |
||
193 | { |
||
194 | return array_key_exists($key, $this->db); |
||
195 | } |
||
196 | } |
||
197 |