|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\system; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\admin\popo\CacheFile; |
|
6
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
7
|
|
|
use Ubiquity\exceptions\CacheException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This class is responsible for storing Arrays in PHP files. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ArrayCache extends AbstractDataCache { |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @var int The file mode used when creating new cache files |
|
16
|
|
|
*/ |
|
17
|
|
|
private $_fileMode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Initializes the file cache-provider |
|
21
|
|
|
* @param string $root absolute path to the root-folder where cache-files will be stored |
|
22
|
|
|
* @param string $postfix Termination of file names |
|
23
|
|
|
* @param array $cacheParams defaults to ["fileMode"=>"0777"] |
|
24
|
|
|
*/ |
|
25
|
12 |
|
public function __construct($root, $postfix="", $cacheParams=[]) { |
|
26
|
12 |
|
parent::__construct($root, $postfix); |
|
27
|
12 |
|
$this->_fileMode=(isset($cacheParams["fileMode"])) ? $cacheParams["fileMode"] : 0777; |
|
28
|
12 |
|
if (!is_dir($root)) |
|
29
|
|
|
\mkdir($root, $this->_fileMode, true); |
|
30
|
12 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check if annotation-data for the key has been stored. |
|
34
|
|
|
* @param string $key cache key |
|
35
|
|
|
* @return boolean true if data with the given key has been stored; otherwise false |
|
36
|
|
|
*/ |
|
37
|
22 |
|
public function exists($key) { |
|
38
|
22 |
|
return file_exists($this->_getPath($key)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Caches the given data with the given key. |
|
43
|
|
|
* @param string $key cache key |
|
44
|
|
|
* @param string $content the source-code to be cached |
|
45
|
|
|
* @throws CacheException if file could not be written |
|
46
|
|
|
*/ |
|
47
|
1 |
|
protected function storeContent($key, $content, $tag) { |
|
48
|
1 |
|
$path=$this->_getPath($key); |
|
49
|
1 |
|
$dir=pathinfo($path,PATHINFO_DIRNAME); |
|
50
|
1 |
|
if(UFileSystem::safeMkdir($dir)){ |
|
51
|
1 |
|
if (@\file_put_contents($path, $content, LOCK_EX) === false) { |
|
52
|
|
|
throw new CacheException("Unable to write cache file: {$path}"); |
|
53
|
|
|
} |
|
54
|
1 |
|
if (@\chmod($path, $this->_fileMode) === false) { |
|
55
|
|
|
throw new CacheException("Unable to set permissions of cache file: {$path}"); |
|
56
|
|
|
} |
|
57
|
|
|
}else{ |
|
58
|
|
|
throw new CacheException("Unable to create folder : {$dir}"); |
|
59
|
|
|
} |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Fetches data stored for the given key. |
|
64
|
|
|
* @param string $key cache key |
|
65
|
|
|
* @return mixed the cached data |
|
66
|
|
|
*/ |
|
67
|
22 |
|
public function fetch($key) { |
|
68
|
22 |
|
return include ($this->_getPath($key)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* return data stored for the given key. |
|
73
|
|
|
* @param string $key cache key |
|
74
|
|
|
* @return mixed the cached data |
|
75
|
|
|
*/ |
|
76
|
|
|
public function file_get_contents($key) { |
|
77
|
|
|
return \file_get_contents($this->_getPath($key)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key cache key |
|
84
|
|
|
* @return int unix timestamp |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getTimestamp($key) { |
|
87
|
|
|
return \filemtime($this->_getPath($key)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Maps a cache-key to the absolute path of a PHP file |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $key cache key |
|
94
|
|
|
* @return string absolute path of the PHP file |
|
95
|
|
|
*/ |
|
96
|
24 |
|
private function _getPath($key) { |
|
97
|
24 |
|
return $this->_root . $key . $this->postfix . '.php'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::remove() |
|
104
|
|
|
*/ |
|
105
|
|
|
public function remove($key) { |
|
106
|
|
|
$file=$this->_getPath($key); |
|
107
|
|
|
if (\file_exists($file)) |
|
108
|
|
|
return \unlink($file); |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::clear() |
|
116
|
|
|
*/ |
|
117
|
|
|
public function clear($matches="") { |
|
118
|
|
|
$files=glob($this->_root . $matches . '*'); |
|
119
|
|
|
foreach ( $files as $file ) { |
|
120
|
|
|
if (\is_file($file)) |
|
121
|
|
|
\unlink($file); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getCacheFiles() |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function getCacheFiles($type) { |
|
131
|
|
|
return CacheFile::initFromFiles($this->_root . $type, \ucfirst($type), function ($file) use ($type) { |
|
132
|
1 |
|
$file=\basename($file); |
|
133
|
1 |
|
return $type . \DS . substr($file, 0, strpos($file, $this->postfix . '.php')); |
|
134
|
1 |
|
}); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::clearCache() |
|
141
|
|
|
*/ |
|
142
|
|
|
public function clearCache($type) { |
|
143
|
|
|
CacheFile::delete($this->_root.\strtolower($type)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getCacheInfo() |
|
150
|
|
|
*/ |
|
151
|
2 |
|
public function getCacheInfo() { |
|
152
|
2 |
|
$result=parent::getCacheInfo(); |
|
153
|
2 |
|
$result.="<br>Root cache directory is <b>" . UFileSystem::cleanPathname($this->_root) . "</b>."; |
|
154
|
2 |
|
return $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getEntryKey() |
|
161
|
|
|
*/ |
|
162
|
4 |
|
public function getEntryKey($key) { |
|
163
|
4 |
|
return UFileSystem::cleanFilePathname($this->_getPath($key)); |
|
164
|
|
|
} |
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritDoc} |
|
167
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::setRoot() |
|
168
|
|
|
*/ |
|
169
|
12 |
|
public function setRoot($root) { |
|
170
|
12 |
|
$this->_root=rtrim($root,\DS).\DS; |
|
171
|
12 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|