|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Inspired by (c) Rasmus Schultz <[email protected]> |
|
4
|
|
|
* <https://github.com/mindplay-dk/php-annotations> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace micro\cache; |
|
7
|
|
|
/** |
|
8
|
|
|
* This class is responsible for storing Arrays in PHP files. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ArrayCache{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string The PHP opening tag (used when writing cache files) |
|
13
|
|
|
*/ |
|
14
|
|
|
const PHP_TAG = "<?php\n"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var int The file mode used when creating new cache files |
|
18
|
|
|
*/ |
|
19
|
|
|
private $_fileMode; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string Absolute path to a folder where cache files will be created |
|
23
|
|
|
*/ |
|
24
|
|
|
private $_root; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string Termination of file names |
|
28
|
|
|
*/ |
|
29
|
|
|
private $postfix; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes the file cache-provider |
|
33
|
|
|
* @param string $root absolute path to the root-folder where cache-files will be stored |
|
34
|
|
|
* @param string Termination of file names |
|
35
|
|
|
* @param int $fileMode file creation mode; defaults to 0777 |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($root, $postfix="",$fileMode = 0777){ |
|
38
|
|
|
$this->_root = $root; |
|
39
|
|
|
$this->_fileMode = $fileMode; |
|
40
|
|
|
$this->postfix=$postfix; |
|
41
|
|
|
if(!is_dir($root)) |
|
42
|
|
|
\mkdir($root,$fileMode,true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Check if annotation-data for the key has been stored. |
|
47
|
|
|
* @param string $key cache key |
|
48
|
|
|
* @return bool true if data with the given key has been stored; otherwise false |
|
49
|
|
|
*/ |
|
50
|
|
|
public function exists($key){ |
|
51
|
|
|
return file_exists($this->_getPath($key)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function expired($key,$duration){ |
|
55
|
|
|
if($this->exists($key)){ |
|
56
|
|
|
if(\is_int($duration)){ |
|
57
|
|
|
return \time()-$this->getTimestamp($key)<=$duration; |
|
58
|
|
|
}else{ |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
}else{ |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Caches the given data with the given key. |
|
68
|
|
|
* @param string $key cache key |
|
69
|
|
|
* @param string $code the source-code to be cached |
|
70
|
|
|
* @throws AnnotationException if file could not be written |
|
71
|
|
|
*/ |
|
72
|
|
|
public function store($key, $code,$php=true){ |
|
73
|
|
|
$path = $this->_getPath($key); |
|
74
|
|
|
$content=""; |
|
75
|
|
|
if($php) |
|
76
|
|
|
$content = self::PHP_TAG; |
|
77
|
|
|
$content .= $code . "\n"; |
|
78
|
|
|
if (@file_put_contents($path, $content, LOCK_EX) === false) { |
|
79
|
|
|
throw new \Exception("Unable to write cache file: {$path}"); |
|
80
|
|
|
} |
|
81
|
|
|
if (@chmod($path, $this->_fileMode) === false) { |
|
82
|
|
|
throw new \Exception("Unable to set permissions of cache file: {$path}"); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Fetches data stored for the given key. |
|
88
|
|
|
* @param string $key cache key |
|
89
|
|
|
* @return mixed the cached data |
|
90
|
|
|
*/ |
|
91
|
|
|
public function fetch($key){ |
|
92
|
|
|
return include($this->_getPath($key)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $key cache key |
|
99
|
|
|
* @return int unix timestamp |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getTimestamp($key){ |
|
102
|
|
|
return \filemtime($this->_getPath($key)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Maps a cache-key to the absolute path of a PHP file |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $key cache key |
|
109
|
|
|
* @return string absolute path of the PHP file |
|
110
|
|
|
*/ |
|
111
|
|
|
private function _getPath($key){ |
|
112
|
|
|
return $this->_root . DIRECTORY_SEPARATOR . $key .$this->postfix.'.php'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function remove($key){ |
|
116
|
|
|
$file=$this->_getPath($key); |
|
117
|
|
|
if(\file_exists($file)) |
|
118
|
|
|
\unlink($file); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function clear(){ |
|
122
|
|
|
$files = glob($this->_root.'/*'); |
|
123
|
|
|
foreach($files as $file){ |
|
124
|
|
|
if(\is_file($file)) |
|
125
|
|
|
\unlink($file); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string absolute path of the folder where cache files are created |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getRoot(){ |
|
133
|
|
|
return $this->_root; |
|
134
|
|
|
} |
|
135
|
|
|
} |