1 | <?php |
||
8 | class Adapter extends AbstractCache |
||
9 | { |
||
10 | /** |
||
11 | * @var AdapterInterface An adapter |
||
12 | */ |
||
13 | protected $adapter; |
||
14 | |||
15 | /** |
||
16 | * @var string the file to cache to |
||
17 | */ |
||
18 | protected $file; |
||
19 | |||
20 | /** |
||
21 | * @var int|null seconds until cache expiration |
||
22 | */ |
||
23 | protected $expire = null; |
||
24 | |||
25 | /** |
||
26 | * Constructor. |
||
27 | * |
||
28 | * @param AdapterInterface $adapter adapter |
||
29 | * @param string $file the file to cache to |
||
30 | * @param int|null $expire seconds until cache expiration |
||
31 | */ |
||
32 | 21 | public function __construct(AdapterInterface $adapter, $file, $expire = null) |
|
38 | |||
39 | /** |
||
40 | * Set the expiration time in seconds. |
||
41 | * |
||
42 | * @param int $expire relative expiration time |
||
43 | */ |
||
44 | 21 | protected function setExpire($expire) |
|
50 | |||
51 | /** |
||
52 | * Get expiration time in seconds. |
||
53 | * |
||
54 | * @param int $time relative expiration time |
||
55 | * |
||
56 | * @return int actual expiration time |
||
57 | */ |
||
58 | 9 | protected function getTime($time = 0) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 9 | public function setFromStorage($json) |
|
67 | { |
||
68 | 9 | list($cache, $complete, $expire) = json_decode($json, true); |
|
69 | |||
70 | 9 | if (! $expire || $expire > $this->getTime()) { |
|
71 | 6 | $this->cache = is_array($cache) ? $cache : []; |
|
72 | 6 | $this->complete = is_array($complete) ? $complete : []; |
|
73 | 6 | } else { |
|
74 | 3 | $this->adapter->delete($this->file); |
|
75 | } |
||
76 | 9 | } |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 12 | public function load() |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 12 | public function getForStorage() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 12 | public function save() |
|
115 | } |
||
116 |