1 | <?php |
||
19 | class Store implements StoreInterface |
||
20 | { |
||
21 | private $timeout = 7200; |
||
22 | private $storage = null; |
||
23 | private $currentPath = null; |
||
24 | |||
25 | /** |
||
26 | * @param object $storage An object implementing the methods in \arc\cache\FileStore |
||
27 | * @param int|string $timeout Default cache timeout in seconds or a string parseable by strtotime |
||
28 | * @param null $currentPath The current path in the cache store. |
||
29 | */ |
||
30 | 4 | public function __construct($storage, $timeout = 7200, $currentPath = null) |
|
36 | |||
37 | /* \arc\KeyValueStoreInterface */ |
||
38 | |||
39 | /** |
||
40 | * Part of the KeyValueStoreInterface, this function will return the value for the given name |
||
41 | * but only if it is still fresh. Identical to getIfFresh(). |
||
42 | * @param string $name |
||
43 | * @return mixed value |
||
44 | */ |
||
45 | public function getVar($name) |
||
49 | |||
50 | /** |
||
51 | * Part of the KeyValueStoreInterface, this function will set a value for the given name |
||
52 | * Identical to set(). It will use the default timeout set when the cache store was constructed. |
||
53 | * @param string $name |
||
54 | * @param string $value |
||
55 | * @return mixed value |
||
56 | */ |
||
57 | 1 | public function putVar($name, $value) |
|
61 | |||
62 | /* PathTreeInterface */ |
||
63 | |||
64 | /** |
||
65 | * Part of the PathTreeInterface, this function will return a new cache store with the new path. |
||
66 | * @param string $path |
||
67 | * @return \arc\cache\Store |
||
68 | */ |
||
69 | 2 | public function cd($path) |
|
75 | |||
76 | /** |
||
77 | * Part of the PathTreeInterface, this function will return a list of items in the cache store. |
||
78 | * @return array |
||
79 | */ |
||
80 | public function ls() |
||
84 | |||
85 | /* StoreInterface */ |
||
86 | |||
87 | /** |
||
88 | * This method will return the cached image with the given name, if it is still fresh, or |
||
89 | * if not call the callable method to generate a new value, store it and return that. |
||
90 | * @param string $name |
||
91 | * @param callable $calculateCallback |
||
92 | * @return mixed |
||
93 | */ |
||
94 | 1 | public function cache($name, $calculateCallback) |
|
105 | |||
106 | /** |
||
107 | * This method returns a new cache store with the given timeout. In all other respects |
||
108 | * the new store is a copy of the current one. |
||
109 | * @param mixed $timeout Either a timestamp (int) or a string parseable by strtotime. |
||
110 | * @return \arc\cache\Store |
||
111 | */ |
||
112 | public function timeout($timeout) |
||
116 | |||
117 | /** |
||
118 | * This method returns the value stored for the given name - even if no longer fresh - |
||
119 | * or null if there is no value stored. |
||
120 | * @param string $name |
||
121 | * @return mixed |
||
122 | */ |
||
123 | 3 | public function get($name) |
|
132 | |||
133 | /** |
||
134 | * This method stores a name - value pair, with either a given timeout or the default |
||
135 | * timeout for this Store. |
||
136 | * @param string $name |
||
137 | * @param mixed $value |
||
138 | * @param mixed $timeout Either a timestamp (int) or a string parseable by strtotime. |
||
139 | * @return bool true for successfull storage, false for failure. |
||
140 | */ |
||
141 | 5 | public function set($name, $value, $timeout = null) |
|
154 | |||
155 | /** |
||
156 | * This method returns metadata for the given name. |
||
157 | * @param string $name |
||
158 | * @return array |
||
159 | */ |
||
160 | 3 | public function getInfo($name) |
|
164 | |||
165 | /** |
||
166 | * This method checks if the value for the given name is fresh for at least the time |
||
167 | * passed as the freshness param. |
||
168 | * @param string $name |
||
169 | * @param mixed $freshness either a unix timestamp or a string parseable by strtotime |
||
170 | * @return bool |
||
171 | */ |
||
172 | 3 | public function isFresh($name, $freshness = 0) |
|
182 | |||
183 | /** |
||
184 | * This method returns the value associated with the given name if it is still fresh, |
||
185 | * otherwise it will return null. You can set a minimum freshness time. |
||
186 | * @param string $name |
||
187 | * @param mixed $freshness either a unix timestamp or a string parseable by strtotime |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 2 | public function getIfFresh($name, $freshness = 0) |
|
198 | |||
199 | /** |
||
200 | * This method locks the store for the given name. If blocking is set to true, other |
||
201 | * processes are blocked from reading any current value. Returns true if the lock |
||
202 | * succeeded. |
||
203 | * @param string $name |
||
204 | * @param bool $blocking |
||
205 | * @return bool |
||
206 | */ |
||
207 | 2 | public function lock($name, $blocking = false) |
|
211 | |||
212 | /** |
||
213 | * This method waits for the store to be unlocked for the given name. |
||
214 | * @param string $name |
||
215 | */ |
||
216 | public function wait($name) |
||
224 | |||
225 | /** |
||
226 | * This method unlocks the store for the given name. Returns true on succes, |
||
227 | * false on failure. |
||
228 | * @param string $name |
||
229 | * @return bool |
||
230 | */ |
||
231 | 5 | public function unlock($name) |
|
235 | |||
236 | /** |
||
237 | * This method removes any value for a given name from the store. |
||
238 | * @param string $name |
||
239 | * @return bool |
||
240 | */ |
||
241 | 1 | public function remove($name) |
|
245 | |||
246 | /** |
||
247 | * This method removes any value for a given name from the store. If the name |
||
248 | * is a path, it will also remove any values for any child paths. |
||
249 | * @param string $name |
||
250 | * @return bool |
||
251 | */ |
||
252 | 2 | public function purge($name = null) |
|
256 | |||
257 | 5 | private function getTimeout($timeout) |
|
269 | } |
||
270 |