1
|
|
|
<?php
|
2
|
|
|
namespace Mouf\Utils\Cache;
|
3
|
|
|
use Mouf\Utils\Log\LogInterface;
|
4
|
|
|
use Psr\Log\LoggerInterface;
|
5
|
|
|
|
6
|
|
|
/**
|
7
|
|
|
* This package contains a cache mechanism that relies on executable PHP files.
|
8
|
|
|
* For a file cache, it is quite fast because it relies on the Opcache to cache data.
|
9
|
|
|
*
|
10
|
|
|
* It features best performances than the regular FileCache. However, be aware that it generates PHP files
|
11
|
|
|
* that are directly "included" in your application, so no third party should have access to the repository where
|
12
|
|
|
* you store the cache.
|
13
|
|
|
*
|
14
|
|
|
*/
|
15
|
|
|
class PhpFileCache extends FileCache {
|
16
|
|
|
|
17
|
|
|
public function __construct() {
|
18
|
|
|
// Let's check if the parameters are ok.
|
19
|
|
|
// We should not allow opcache file revalidation should be set to 0
|
20
|
|
|
if (ini_get('opcache.revalidate_freq') != 0) {
|
21
|
|
|
throw new \Exception("In order to use PhpFileCache, you must set the parameter 'opcache.revalidate_freq' to 0 in your php.ini file. Current value: '".ini_get('opcache.revalidate_freq')."'");
|
22
|
|
|
}
|
23
|
|
|
}
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* Returns the cached value for the key passed in parameter.
|
27
|
|
|
*
|
28
|
|
|
* @param string $key
|
29
|
|
|
* @return mixed
|
30
|
|
|
*/
|
31
|
|
|
public function get($key) {
|
32
|
|
|
$filename = $this->getFileName($key);
|
33
|
|
|
|
34
|
|
|
if ( ! is_file($filename)) {
|
35
|
|
|
if ($this->log) {
|
36
|
|
|
if ($this->log instanceof LoggerInterface) {
|
37
|
|
|
$this->log->info("Retrieving key '{key}' from file cache: cache miss.", array('key'=>$key));
|
38
|
|
|
} else {
|
39
|
|
|
$this->log->trace("Retrieving key '$key' from file cache: cache miss.");
|
40
|
|
|
}
|
41
|
|
|
}
|
42
|
|
|
return null;
|
43
|
|
|
}
|
44
|
|
|
$value = include $filename;
|
45
|
|
|
|
46
|
|
|
if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
|
47
|
|
|
if ($this->log) {
|
48
|
|
|
if ($this->log instanceof LoggerInterface) {
|
49
|
|
|
$this->log->info("Retrieving key '{key}' from file cache: key outdated, cache miss.", array('key'=>$key));
|
50
|
|
|
} else {
|
51
|
|
|
$this->log->trace("Retrieving key '$key' from file cache: key outdated, cache miss.");
|
52
|
|
|
}
|
53
|
|
|
}
|
54
|
|
|
return null;
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
if ($this->log) {
|
58
|
|
|
if ($this->log instanceof LoggerInterface) {
|
59
|
|
|
$this->log->info("Retrieving key '{key}' from file cache.", array('key'=>$key));
|
60
|
|
|
} else {
|
61
|
|
|
$this->log->trace("Retrieving key '$key' from file cache.");
|
62
|
|
|
}
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
return $value['data'];
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Sets the value in the cache.
|
70
|
|
|
*
|
71
|
|
|
* @param string $key The key of the value to store
|
72
|
|
|
* @param mixed $value The value to store
|
73
|
|
|
* @param float $timeToLive The time to live of the cache, in seconds.
|
74
|
|
|
*/
|
75
|
|
|
public function set($key, $value, $timeToLive = null) {
|
76
|
|
|
$filename = $this->getFileName($key);
|
77
|
|
|
//$this->log->trace("Storing value in cache: key '$key', value '".var_export($value, true)."'");
|
78
|
|
|
if ($this->log) {
|
79
|
|
|
if ($this->log instanceof LoggerInterface) {
|
80
|
|
|
$this->log->info("Storing value in cache: key '{key}'", array('key'=>$key));
|
81
|
|
|
} else {
|
82
|
|
|
$this->log->trace("Storing value in cache: key '$key'");
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
$oldUmask = umask(0);
|
87
|
|
|
|
88
|
|
View Code Duplication |
if (!is_writable($filename)) {
|
|
|
|
|
89
|
|
|
if (!file_exists($this->getDirectory())) {
|
90
|
|
|
mkdir($this->getDirectory(), 0777, true);
|
91
|
|
|
}
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
View Code Duplication |
if ($timeToLive == null) {
|
|
|
|
|
95
|
|
|
if (empty($this->defaultTimeToLive)) {
|
96
|
|
|
$timeOut = 0;
|
97
|
|
|
} else {
|
98
|
|
|
$timeOut = time() + $this->defaultTimeToLive;
|
99
|
|
|
}
|
100
|
|
|
} else {
|
101
|
|
|
$timeOut = time() + $timeToLive;
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
$data = array(
|
105
|
|
|
'lifetime' => $timeOut,
|
106
|
|
|
'data' => $value
|
107
|
|
|
);
|
108
|
|
|
|
109
|
|
|
$serializeData = serialize($data);
|
110
|
|
|
if(strpos($serializeData, 'r:') !== false || (is_object($value) && !method_exists($value, '__set_state'))){
|
111
|
|
|
$code = sprintf('<?php return unserialize(%s);', var_export($serializeData, true));
|
112
|
|
|
file_put_contents($filename, $code);
|
113
|
|
|
}else{
|
114
|
|
|
$data = var_export($data, true);
|
115
|
|
|
$code = sprintf('<?php return %s;', $data);
|
116
|
|
|
file_put_contents($filename, $code);
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
// Cache is shared with group, not with the rest of the world.
|
120
|
|
|
chmod($filename, 0660);
|
121
|
|
|
|
122
|
|
|
umask($oldUmask);
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
}
|
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.