|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\system; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheFile; |
|
6
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
|
7
|
|
|
use Phpfastcache\CacheManager; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This class is responsible for storing values with PhpFastCache. |
|
11
|
|
|
* Ubiquity\cache\system$PhpFastCacheDriver |
|
12
|
|
|
* This class is part of Ubiquity |
|
13
|
|
|
* |
|
14
|
|
|
* @author jcheron <[email protected]> |
|
15
|
|
|
* @version 1.1.0 |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class PhpFastCacheDriver extends AbstractDataCache { |
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var ExtendedCacheItemPoolInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cacheInstance; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Initializes the cache-provider |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($root, $postfix = "", $cacheParams = [ ]) { |
|
29
|
|
|
parent::__construct ( $root, $postfix ); |
|
30
|
|
|
$cacheType = $cacheParams ['type'] ?? 'Files'; |
|
31
|
|
|
$configClass = '\\Phpfastcache\\Drivers\\' . \ucfirst ( $cacheType ) . '\\Config'; |
|
32
|
|
|
unset ( $cacheParams ['type'] ); |
|
33
|
|
|
$defaultParams = [ 'defaultTtl' => 86400,'itemDetailedDate' => true ]; |
|
34
|
|
|
$cacheParams = \array_merge ( $defaultParams, $cacheParams ); |
|
35
|
|
|
$this->cacheInstance = CacheManager::getInstance ( $cacheType, new $configClass ( $cacheParams ) ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Check if annotation-data for the key has been stored. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $key cache key |
|
42
|
|
|
* @return boolean true if data with the given key has been stored; otherwise false |
|
43
|
|
|
*/ |
|
44
|
|
|
public function exists($key) { |
|
45
|
|
|
return $this->cacheInstance->hasItem ( $this->getRealKey ( $key ) ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function store($key, $content, $tag = null) { |
|
49
|
|
|
$key = $this->getRealKey ( $key ); |
|
50
|
|
|
$item = $this->cacheInstance->getItem ( $key ); |
|
51
|
|
|
$item->set ( $content ); |
|
52
|
|
|
if ($tag != null) { |
|
53
|
|
|
$item->addTag ( $tag ); |
|
54
|
|
|
} |
|
55
|
|
|
$this->cacheInstance->save ( $item ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getRealKey($key) { |
|
59
|
|
|
return \str_replace ( [ '\\','/' ], '-', $key ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Fetches data stored for the given key. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key cache key |
|
66
|
|
|
* @return mixed the cached data |
|
67
|
|
|
*/ |
|
68
|
|
|
public function fetch($key) { |
|
69
|
|
|
return $this->cacheInstance->getItem ( $this->getRealKey ( $key ) )->get (); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* return data stored for the given key. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $key cache key |
|
76
|
|
|
* @return mixed the cached data |
|
77
|
|
|
*/ |
|
78
|
|
|
public function file_get_contents($key) { |
|
79
|
|
|
return $this->cacheInstance->getItem ( $this->getRealKey ( $key ) )->get (); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key cache key |
|
86
|
|
|
* @return int unix timestamp |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getTimestamp($key) { |
|
89
|
|
|
return $this->cacheInstance->getItem ( $this->getRealKey ( $key ) )->getModificationDate ()->getTimestamp (); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function remove($key) { |
|
93
|
|
|
$this->cacheInstance->deleteItem ( $this->getRealKey ( $key ) ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function clear() { |
|
97
|
|
|
$this->cacheInstance->clear (); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getCacheEntries($type) { |
|
101
|
|
|
return $this->cacheInstance->getItemsByTag ( $type ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getCacheFiles($type) { |
|
105
|
|
|
$result = [ ]; |
|
106
|
|
|
$entries = $this->getCacheEntries ( $type ); |
|
107
|
|
|
|
|
108
|
|
|
foreach ( $entries as $entry ) { |
|
109
|
|
|
$key = $entry->getKey (); |
|
110
|
|
|
$result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry->getCreationDate ()->getTimestamp (), "", $key ); |
|
111
|
|
|
} |
|
112
|
|
|
if (\sizeof ( $result ) === 0) |
|
113
|
|
|
$result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" ); |
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function clearCache($type) { |
|
118
|
|
|
$this->cacheInstance->deleteItemsByTag ( $type ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getCacheInfo() { |
|
122
|
|
|
return parent::getCacheInfo () . "<br>Driver name : <b>" . $this->cacheInstance->getDriverName () . "</b>"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getEntryKey($key) { |
|
126
|
|
|
return $this->cacheInstance->getItem ( $this->getRealKey ( $key ) )->getKey (); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|