1 | <?php |
||
22 | class FileStorage extends AbstractStorage |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | const EXTENSION = 'cache'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $dir; |
||
33 | |||
34 | /** |
||
35 | * @var bool|array |
||
36 | */ |
||
37 | private $previousRequest; |
||
38 | |||
39 | /** |
||
40 | * File Storage constructor. |
||
41 | * |
||
42 | * @param string $directory |
||
43 | * @param DateInterval $ttl |
||
44 | * @throws \Exception |
||
45 | */ |
||
46 | 14 | public function __construct($directory, DateInterval $ttl) |
|
60 | |||
61 | /** |
||
62 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
63 | * |
||
64 | * @param \string $key The key of the item to store. |
||
65 | * @param mixed $value The value of the item to store, must be serializable. |
||
66 | * @param null|\int $ttl Optional. The TTL value of this item. If no value is sent and |
||
67 | * the driver supports TTL then the library may set a default value |
||
68 | * for it or let the driver take care of that. |
||
69 | * |
||
70 | * @return bool True on success and false on failure. |
||
71 | * @throws InvalidArgumentException |
||
72 | */ |
||
73 | 6 | public function set($key, $value, $ttl = null) |
|
74 | { |
||
75 | 6 | $ttl = time() + $this->getTtlTimestamp($ttl); |
|
76 | 6 | $cacheItem = $this->_createCacheItem($key, $value, $ttl); |
|
77 | |||
78 | 6 | if (null !== $value){ |
|
79 | 6 | \file_put_contents($cacheItem['file'],$cacheItem['data']); |
|
80 | 6 | if (!file_exists($cacheItem['file'])){ |
|
81 | return false; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | 6 | return true; |
|
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Determines whether an item is present in the cache. |
||
91 | * |
||
92 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
93 | * and not to be used within your live applications operations for get/set, as this method |
||
94 | * is subject to a race condition where your has() will return true and immediately after, |
||
95 | * another script can remove it making the state of your app out of date. |
||
96 | * |
||
97 | * @param string $key The cache item key. |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | 5 | public function has($key){ |
|
107 | |||
108 | /** |
||
109 | * Fetches a value from the cache. |
||
110 | * |
||
111 | * @param string $key The unique key of this item in the cache. |
||
112 | * |
||
113 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||
114 | * |
||
115 | */ |
||
116 | 1 | public function get($key){ |
|
117 | 1 | if ($this->has($key)){ |
|
118 | 1 | return $this->previousRequest[1]; |
|
119 | } |
||
120 | return null; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Delete an item from the cache by its unique key. |
||
125 | * |
||
126 | * @param string $key The unique cache key of the item to delete. |
||
127 | * |
||
128 | * @return bool True if the item was successfully removed. False if there was an error. |
||
129 | */ |
||
130 | 1 | public function delete($key) |
|
134 | |||
135 | /** |
||
136 | * Wipes clean the entire cache's keys. |
||
137 | * |
||
138 | * @return bool True on success and false on failure. |
||
139 | */ |
||
140 | 1 | public function clear(){ |
|
141 | 1 | if (is_dir( $this->dir )){ |
|
142 | 1 | array_map('unlink', glob("$this->dir/*.*")); |
|
143 | } |
||
144 | |||
145 | 1 | $data = glob("$this->dir/*.*"); |
|
146 | |||
147 | 1 | if (!empty($data)){ |
|
148 | return false; |
||
149 | } |
||
150 | |||
151 | 1 | return true; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Cache is only called one for both has/get via this internal method. |
||
156 | * |
||
157 | * @param $key |
||
158 | * @return bool|mixed |
||
159 | * @internal |
||
160 | */ |
||
161 | 8 | private function _fetchCacheFile($key){ |
|
181 | |||
182 | /** |
||
183 | * Create cache item. |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param $value |
||
187 | * @param int $ttl |
||
188 | * @return array |
||
189 | * @throws InvalidArgumentException |
||
190 | */ |
||
191 | 8 | private function _createCacheItem($key, $value, $ttl){ |
|
204 | } |
||
205 |