|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Cache |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Cache\Driver; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Storage\Storage; |
|
18
|
|
|
use Psr\Cache\CacheItemInterface; |
|
19
|
|
|
use Phossa2\Cache\Interfaces\CacheItemExtendedInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* StorageDriver |
|
23
|
|
|
* |
|
24
|
|
|
* Driver using Phossa2\Storage library |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Cache |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see DriverAbstract |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
class StorageDriver extends DriverAbstract |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Storage backend |
|
36
|
|
|
* |
|
37
|
|
|
* @var Storage |
|
38
|
|
|
* @access protected |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $storage; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Cache directory in storage |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
* @access protected |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $cache_dir; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Inject storage object |
|
52
|
|
|
* |
|
53
|
|
|
* @param Storage $storage |
|
54
|
|
|
* @param string $cacheDir |
|
55
|
|
|
* @param array $properties |
|
56
|
|
|
* @access public |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( |
|
59
|
|
|
Storage $storage, |
|
60
|
|
|
/*# string */ $cacheDir = '/cache', |
|
61
|
|
|
array $properties = [] |
|
62
|
|
|
) { |
|
63
|
|
|
$this->storage = $storage; |
|
64
|
|
|
$this->cache_dir = rtrim($cacheDir, '/'); |
|
65
|
|
|
parent::__construct($properties); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritDoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function save(CacheItemInterface $item)/*# : bool */ |
|
72
|
|
|
{ |
|
73
|
|
|
if ($item instanceof CacheItemExtendedInterface) { |
|
74
|
|
|
$key = $item->getKey(); |
|
75
|
|
|
$exp = $item->getExpiration()->getTimestamp(); |
|
76
|
|
|
|
|
77
|
|
|
$res = $this->storage->put( |
|
78
|
|
|
$this->getPath($key), |
|
79
|
|
|
(string) $item, |
|
80
|
|
|
['mtime' => $exp] |
|
81
|
|
|
); |
|
82
|
|
|
return $res ?: $this->resetError(); |
|
83
|
|
|
} |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function driverHas(/*# string */ $key)/*# : array */ |
|
91
|
|
|
{ |
|
92
|
|
|
$path = $this->getPath($key); |
|
93
|
|
|
|
|
94
|
|
|
// not found |
|
95
|
|
|
if (!$this->storage->has($path)) { |
|
96
|
|
|
return []; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// translate 'mtime' to 'expire' |
|
100
|
|
|
$meta = $this->storage->meta($path); |
|
101
|
|
|
if (isset($meta['mtime'])) { |
|
102
|
|
|
$meta['expire'] = (int) $meta['mtime']; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $meta; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function driverGet(/*# string */ $key) |
|
112
|
|
|
{ |
|
113
|
|
|
$res = $this->storage->get($this->getPath($key)); |
|
114
|
|
|
$this->resetError(); |
|
115
|
|
|
return is_string($res) ? $res : null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritDoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function driverDelete(CacheItemInterface $item)/*# : bool */ |
|
122
|
|
|
{ |
|
123
|
|
|
$key = $item->getKey(); |
|
124
|
|
|
$res = $this->storage->del($this->getPath($key)); |
|
125
|
|
|
return $res ?: $this->resetError(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function driverClear()/*# : bool */ |
|
132
|
|
|
{ |
|
133
|
|
|
$res = $this->storage->del($this->cache_dir); |
|
134
|
|
|
return $res ?: $this->resetError(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Generate full path in storage base on the given key |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $key |
|
141
|
|
|
* @access protected |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function getPath(/*# string */ $key)/*# : string */ |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->cache_dir . '/' . $key; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Reset to storage error |
|
150
|
|
|
* |
|
151
|
|
|
* @return false |
|
152
|
|
|
* @access protected |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function resetError()/*# : bool */ |
|
155
|
|
|
{ |
|
156
|
|
|
$this->copyError($this->storage); |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|