|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Journey\Cache\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use Journey\Cache\CacheAdapterInterface; |
|
6
|
|
|
use Journey\Cache\CacheException; |
|
7
|
|
|
|
|
8
|
|
|
class LocalAdapter implements CacheAdapterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* A filesystem path to store cache values. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $path; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* umask value to set on cache files. |
|
19
|
|
|
* |
|
20
|
|
|
* @var integer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $umask = 000; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initialize a new localcache. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $filepath |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function __construct($filepath) |
|
30
|
|
|
{ |
|
31
|
8 |
|
if (!is_dir($filepath)) { |
|
32
|
1 |
|
throw new CacheException('Cache file path is not a directory.'); |
|
33
|
|
|
} |
|
34
|
8 |
|
$this->path = rtrim($filepath, "/"); |
|
35
|
8 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Must implement a set method. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $key key to set as the cache value. |
|
41
|
|
|
* @param mixed $value returns the value of the cached item. |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function set($key, $value, $expiration = false) |
|
45
|
|
|
{ |
|
46
|
6 |
|
$path = $this->filename($key); |
|
47
|
6 |
|
$umask = umask($this->umask); |
|
48
|
6 |
|
file_put_contents($path, $this->createValue($value, $expiration)); |
|
|
|
|
|
|
49
|
6 |
|
umask($umask); |
|
50
|
6 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Must implement a get method. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $key Get a cached item by key. |
|
57
|
|
|
* @return mixed Returns cached item or false. |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function get($key) |
|
60
|
|
|
{ |
|
61
|
5 |
|
$path = $this->filename($key); |
|
62
|
5 |
|
$file = @fopen($path, 'r'); |
|
63
|
5 |
|
if (!$file) { |
|
64
|
2 |
|
return false; |
|
65
|
|
|
} |
|
66
|
3 |
|
$timestamp = fgets($file, 11); |
|
67
|
3 |
|
fclose($file); |
|
68
|
3 |
|
if ($timestamp > time() || $timestamp == "00000000000") { |
|
69
|
2 |
|
return substr(file_get_contents($path), 11); |
|
70
|
|
|
} |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Must implement a delete method. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $key delete a specific cached item by key. |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function delete($key) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$file = $this->filename($key); |
|
83
|
1 |
|
unlink($file); |
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Clear all of the values set by this cache instance. |
|
89
|
|
|
* |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function clear() |
|
93
|
|
|
{ |
|
94
|
3 |
|
$key = $this->key(); |
|
|
|
|
|
|
95
|
3 |
|
$files = glob($this->path . "/_cache-" . $key . "*.cache"); |
|
96
|
3 |
|
$this->setKey(); |
|
97
|
3 |
|
$this->key(true); |
|
98
|
3 |
|
foreach ($files as $file) { |
|
99
|
3 |
|
unlink($file); |
|
100
|
3 |
|
} |
|
101
|
3 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the filename of a key. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key name of the key |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
6 |
|
public function filename($key) |
|
111
|
|
|
{ |
|
112
|
6 |
|
return $this->path . "/_cache-" . $this->key() . "-" . md5($key) . ".cache"; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Create a parsable value from the data and expiration date. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $value value of the store |
|
119
|
|
|
* @param integer $expiration integer value of the expiration (unix timestamp) |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
6 |
|
public function createValue($value, $expiration) |
|
123
|
|
|
{ |
|
124
|
6 |
|
$expiration = $expiration ? str_pad($expiration, 11, "0". STR_PAD_LEFT) : "00000000000"; |
|
125
|
6 |
|
return $expiration . $value; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Gets the current cache namespace key. |
|
130
|
|
|
* |
|
131
|
|
|
* Note: to save on time spent reading/writing to disk, this method uses |
|
132
|
|
|
* static caching. Its important that when a cache key gets reset this |
|
133
|
|
|
* method has it's local cache reset by passing `true`. |
|
134
|
|
|
* |
|
135
|
|
|
* @param boolean $reset resets the static cache. |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
6 |
|
public function key($reset = false) |
|
139
|
|
|
{ |
|
140
|
6 |
|
static $key; |
|
141
|
6 |
|
$key = $reset ? false : $key; |
|
142
|
6 |
|
if (!$key) { |
|
143
|
4 |
|
$path = $this->path . "/.cache_key"; |
|
144
|
4 |
|
if (file_exists($path)) { |
|
145
|
3 |
|
$key = file_get_contents($path); |
|
146
|
3 |
|
} else { |
|
147
|
1 |
|
$key = $this->setKey(); |
|
148
|
|
|
} |
|
149
|
4 |
|
} |
|
150
|
6 |
|
return $key; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set the current cache key. |
|
155
|
|
|
*/ |
|
156
|
4 |
|
public function setKey() |
|
157
|
|
|
{ |
|
158
|
4 |
|
$path = $this->path . "/.cache_key"; |
|
159
|
4 |
|
$key = bin2hex(openssl_random_pseudo_bytes(6)); |
|
160
|
4 |
|
$umask = umask($this->umask); |
|
161
|
4 |
|
file_put_contents($path, $key); |
|
162
|
4 |
|
umask($umask); |
|
163
|
4 |
|
return $key; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: