Skip to content

Commit 4747ad5

Browse files
author
Marco Cesarato
committed
feat(cache): improve performance and reduce data loss
1 parent 17036a0 commit 4747ad5

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/Cache.php

+47-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Cache extends SingletonAbstract
2828
protected function __construct()
2929
{
3030
parent::__construct();
31-
$tempdir = sys_get_temp_dir() . self::$DS . Scanner::getName() . self::$DS;
31+
$tempdir = sys_get_temp_dir() . self::$DS . Scanner::getName() . self::$DS . 'cache' . self::$DS;
3232

3333
if (!is_dir($tempdir) && (!mkdir($tempdir, 0777, true) && !is_dir($tempdir))) {
3434
throw new RuntimeException(sprintf('Temp Directory "%s" was not created', $tempdir));
@@ -50,18 +50,19 @@ public function set($key, $value, $ttl = 3600)
5050
{
5151
$key = $this->key($key);
5252

53-
if ($data = json_encode(['ttl' => $ttl > 0 ? time() + $ttl : $ttl, 'data' => $value])) {
54-
if (!Scanner::isCacheEnabled()) {
55-
self::$cache[$key] = $data;
53+
$data = ['ttl' => $ttl > 0 ? time() + $ttl : $ttl, 'data' => $value];
5654

57-
return true;
58-
}
55+
if (!Scanner::isCacheEnabled()) {
56+
self::$cache[$key] = $data;
5957

60-
if (file_put_contents($this->tempdir . $key, $data) !== false) {
61-
self::$cache[$key] = $data;
58+
return true;
59+
}
6260

63-
return true;
64-
}
61+
$fileData = '<?php return ' . var_export($data, true) . ';';
62+
if (file_put_contents($this->tempdir . $key . '.' . self::$ext, $fileData) !== false) {
63+
self::$cache[$key] = $data;
64+
65+
return true;
6566
}
6667

6768
return false;
@@ -99,20 +100,18 @@ public function touch($key, $ttl = 3600)
99100
public function get($key, $default = null)
100101
{
101102
$key = $this->key($key);
102-
$file = $this->tempdir . $key;
103+
$file = $this->tempdir . $key . '.' . self::$ext;
103104

104105
$data = false;
105106
if (isset(self::$cache[$key])) {
106107
$data = self::$cache[$key];
107108
} elseif (Scanner::isCacheEnabled() && is_file($file)) {
108-
$data = file_get_contents($file);
109+
$data = include $file;
109110
self::$cache[$key] = $data;
110111
}
111112

112113
if ($data !== false) {
113114
if (!empty($data)) {
114-
$data = json_decode($data, true);
115-
116115
if (isset($data['ttl'], $data['data'])) {
117116
if ($data['ttl'] <= 0 || $data['ttl'] >= time()) {
118117
return $data['data'];
@@ -175,16 +174,46 @@ public function deleteRegex($pattern = '*')
175174
/**
176175
* Generate key.
177176
*
177+
* @see https://stackoverflow.com/a/42058764
178+
*
178179
* @param string $key
179180
*
180181
* @return string
181182
*/
182183
protected function key($key)
183184
{
184-
$filename = $key . '.' . self::$ext;
185-
$specialChars = ['?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}'];
186-
$filename = str_replace($specialChars, '', $filename);
187-
$filename = preg_replace('/[\s-]+/', '-', $filename);
185+
$filename = preg_replace(
186+
'~' .
187+
'[<>:"/\\|?*]|' . // file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
188+
'[\x00-\x1F]|' . // control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
189+
'[\x7F\xA0\xAD]|' . // non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN
190+
'[#\[\]@!$&\'()+,;=]|' . // URI reserved https://tools.ietf.org/html/rfc3986#section-2.2
191+
'[{}^\~`]' . // URL unsafe characters https://www.ietf.org/rfc/rfc1738.txt
192+
'~x',
193+
'-',
194+
$key
195+
);
196+
// avoids ".", ".." or ".hiddenFiles"
197+
$filename = ltrim($filename, '.-');
198+
// maximize filename length to 255 bytes http://serverfault.com/a/9548/44086
199+
200+
// reduce consecutive characters
201+
$filename = preg_replace([
202+
'/ +/', // "file name.zip" becomes "file-name.zip"
203+
'/_+/', // "file___name.zip" becomes "file-name.zip"
204+
'/-+/', // "file---name.zip" becomes "file-name.zip"
205+
], '-', $filename);
206+
$filename = preg_replace([
207+
'/-*\.-*/', // "file--.--.-.--name.zip" becomes "file.name.zip"
208+
'/\.{2,}/', // "file...name..zip" becomes "file.name.zip"
209+
], '.', $filename);
210+
// lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625
211+
$filename = mb_strtolower($filename, mb_detect_encoding($filename));
212+
// ".file-name.-" becomes "file-name"
213+
$filename = trim($filename, '.-');
214+
215+
$ext = pathinfo($filename, PATHINFO_EXTENSION);
216+
$filename = mb_strcut(pathinfo($filename, PATHINFO_FILENAME), 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename)) . ($ext ? '.' . $ext : '');
188217

189218
return trim($filename, '.-_');
190219
}

0 commit comments

Comments
 (0)