1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Saves cached values in the file. |
4
|
|
|
* |
5
|
|
|
* @package SugiPHP.Cache |
6
|
|
|
* @author Plamen Popov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/mit-license.php (MIT License) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace SugiPHP\Cache; |
11
|
|
|
|
12
|
|
|
class FileStore implements StoreInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Path to directory where cache files will be saved. |
16
|
|
|
*/ |
17
|
|
|
protected $path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Creates a File store |
21
|
|
|
* |
22
|
|
|
* @param string |
23
|
|
|
*/ |
24
|
|
|
public function __construct($path) |
25
|
|
|
{ |
26
|
|
|
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function add($key, $value, $ttl = 0) |
33
|
|
|
{ |
34
|
|
|
if ($this->has($key)) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->set($key, $value, $ttl); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function set($key, $value, $ttl = 0) |
45
|
|
|
{ |
46
|
|
|
$file = $this->filename($key); |
47
|
|
|
$expire = ($ttl) ? time() + $ttl : "9999999999"; |
48
|
|
|
// serializing is done mainly to distinguish type of the $value - string, number, etc. |
49
|
|
|
$contents = $expire.serialize($value); |
50
|
|
|
|
51
|
|
|
return (boolean) (@file_put_contents($file, $contents, LOCK_EX)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function get($key) |
58
|
|
|
{ |
59
|
|
|
$file = $this->filename($key); |
60
|
|
|
if (!is_file($file)) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$contents = file_get_contents($file); |
65
|
|
|
|
66
|
|
|
// check TTL |
67
|
|
|
$expire = substr($contents, 0, 10); |
68
|
|
|
if ($expire < time()) { |
69
|
|
|
// if cache is expired delete cache file |
70
|
|
|
$this->delete($key); |
71
|
|
|
|
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return unserialize(substr($contents, 10)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function has($key) |
82
|
|
|
{ |
83
|
|
|
return (is_null($this->get($key))) ? false : true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function delete($key) |
90
|
|
|
{ |
91
|
|
|
@unlink($this->filename($key)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function flush() |
98
|
|
|
{ |
99
|
|
|
$files = glob($this->path."*.cache"); |
100
|
|
|
if ($files) { |
|
|
|
|
101
|
|
|
foreach ($files as $file) { |
102
|
|
|
@unlink($file); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generates a filename based on the $key parameter |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function filename($key) |
115
|
|
|
{ |
116
|
|
|
return $this->path.md5($key).".cache"; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.