1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
<?php
namespace Studip\Cache;
use Config;
use Exception;
use Psr\Cache\CacheItemInterface;
/**
* Cache implementation using files
*
* @author André Noack <noack@data-quest.de>
* @copyright 2007 André Noack <noack@data-quest.de>
* @license GPL2 or any later version
*/
class FileCache extends Cache
{
use KeyTrait;
/**
* full path to cache directory
*
* @var string
*/
private string $dir;
/**
* @return string A translateable display name for this cache class.
*/
public static function getDisplayName(): string
{
return _('Dateisystem');
}
/**
* without the 'dir' argument the cache path is taken from
* $CACHING_FILECACHE_PATH or is set to
* $TMP_PATH/studip_cache
*
* @param string $path the path to use
* @throws Exception if the directory does not exist or could not be
* created
*/
public function __construct(string $path = '')
{
$this->dir = $path
?: (
Config::get()->SYSTEMCACHE['type'] === self::class
? Config::get()->SYSTEMCACHE['config']['path']
: ''
)
?: $GLOBALS['CACHING_FILECACHE_PATH']
?: ($GLOBALS['TMP_PATH'] . '/' . 'studip_cache');
$this->dir = rtrim($this->dir, '\\/') . '/';
if (!is_dir($this->dir) && !@mkdir($this->dir, 0700)) {
throw new \Exception('Could not create directory: ' . $this->dir);
}
if (!is_writable($this->dir)) {
throw new \Exception('Can not write to directory: ' . $this->dir);
}
}
/**
* get path to cache directory
*
* @return string
*/
public function getCacheDir()
{
return $this->dir;
}
/**
* expire cache item
*
* @param string $arg
*
* @return void
* @throws Exception
* @see Cache::expire()
*/
public function expire($arg)
{
$key = $this->getCacheKey($arg);
if ($file = $this->getPathAndFile($key)){
@unlink($file);
}
}
/**
* Expire all items from the cache.
*/
public function flush()
{
rmdirr($this->dir);
}
/**
* checks if specified cache item is expired
* if expired the cache file is deleted
*
* @param string $key a cache key to check
*
* @return array|bool the path to the cache file or false if expired
* @throws Exception
*/
private function check($key)
{
if ($file = $this->getPathAndFile($key)){
[$id, $expire] = explode('-', basename($file));
if (time() < $expire) {
return [$file, $expire];
} else {
@unlink($file);
}
}
return false;
}
/**
* get the full path to a cache file
*
* the cache files are organized in sub-folders named by
* the first two characters of the hashed cache key.
* the filename is constructed from the hashed cache key
* and the timestamp of expiration
*
* @param string $key a cache key
* @param int|null $expire expiry time in seconds
*
* @return string|bool full path to cache item or false on failure
* @throws Exception
*/
private function getPathAndFile(string $key, ?int $expire = null): bool|string
{
$id = hash('md5', $key);
$path = $this->dir . mb_substr($id, 0, 2);
if (!is_dir($path) && !@mkdir($path, 0700)) {
throw new \Exception('Could not create directory: ' . $path);
}
if (!is_null($expire)){
return $path . '/' . $id . '-' . (time() + $expire);
} else {
$files = @glob("{$path}/{$id}*");
if (count($files) > 0) {
return $files[0];
}
}
return false;
}
/**
* purges expired entries from the cache directory
*
* @param bool $be_quiet echo messages if set to false
*
* @return int the number of deleted files
*/
public function purge(bool $be_quiet = true): int
{
$now = time();
$deleted = 0;
foreach (@glob($this->dir . '*', GLOB_ONLYDIR) as $current_dir){
foreach (@glob("{$current_dir}/*") as $file){
[$id, $expire] = explode('-', basename($file));
if ($expire < $now) {
if (@unlink($file)) {
++$deleted;
if (!$be_quiet) {
echo "File: {$file} deleted.\n";
}
}
} else if (!$be_quiet) {
echo "File: {$file} expires on " . date('Y-m-d H:i:s', $expire) . "\n";
}
}
}
return $deleted;
}
/**
* Return statistics.
*
* @return array|array[]
*/
public function getStats(): array
{
return [
__CLASS__ => [
'name' => _('Anzahl Einträge'),
'value' => \DBManager::get()->fetchColumn("SELECT COUNT(*) FROM `cache`")
]
];
}
/**
* Return the Vue component name and props that handle configuration.
*
* @return array
*/
public static function getConfig(): array
{
$currentCache = Config::get()->SYSTEMCACHE;
// Set default config for this cache
$currentConfig = [
'path' => $GLOBALS['TMP_PATH'] . '/studip_cache'
];
// If this cache is set as system cache, use config from global settings.
if ($currentCache['type'] == __CLASS__) {
$currentConfig = $currentCache['config'];
}
return [
'component' => 'FileCacheConfig',
'props' => $currentConfig
];
}
/**
* @inheritDoc
*/
public function getItem(string $key): CacheItemInterface
{
$real_key = $this->getCacheKey($key);
$item = new \Studip\Cache\Item($key);
$file_data = $this->check($real_key);
if ($file_data) {
$file = $file_data[0];
$expire = $file_data[1];
$f = @fopen($file, 'rb');
if ($f) {
@flock($f, LOCK_SH);
$result = stream_get_contents($f);
@fclose($f);
}
$item->setHit();
$item->set(unserialize($result));
$expiration = new \DateTime();
$expiration->setTimestamp($expire);
$item->expiresAt($expiration);
}
return $item;
}
/**
* @inheritDoc
*/
public function hasItem(string $key): bool
{
$real_key = $this->getCacheKey($key);
$file_data = $this->check($real_key);
return $file_data !== false;
}
/**
* @inheritDoc
*/
public function save(CacheItemInterface $item): bool
{
$expiration = $this->getExpiration($item);
if ($expiration < 1) {
//The item would expire immediately.
return false;
}
$real_key = $this->getCacheKey($item->getKey());
$this->expire($real_key);
$file = $this->getPathAndFile($real_key, $expiration);
return @file_put_contents($file, serialize($item->get()), LOCK_EX);
}
}
|