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
|
|
|
* Initialize a new localcache. |
19
|
|
|
* |
20
|
|
|
* @param string $filepath |
21
|
|
|
*/ |
22
|
6 |
|
public function __construct($filepath) |
23
|
|
|
{ |
24
|
6 |
|
if (!is_dir($filepath)) { |
25
|
1 |
|
throw new CacheException('Cache file path is not a directory.'); |
26
|
|
|
} |
27
|
6 |
|
$this->path = rtrim($filepath, "/"); |
28
|
6 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Must implement a set method. |
32
|
|
|
* |
33
|
|
|
* @param string $key key to set as the cache value. |
34
|
|
|
* @param mixed $value returns the value of the cached item. |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
4 |
|
public function set($key, $value, $expiration = false) |
38
|
|
|
{ |
39
|
4 |
|
$path = $this->filename($key); |
40
|
4 |
|
file_put_contents($path, $this->createValue($value, $expiration)); |
|
|
|
|
41
|
4 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Must implement a get method. |
46
|
|
|
* |
47
|
|
|
* @param string $key Get a cached item by key. |
48
|
|
|
* @return mixed Returns cached item or false. |
49
|
|
|
*/ |
50
|
4 |
|
public function get($key) |
51
|
|
|
{ |
52
|
4 |
|
$path = $this->filename($key); |
53
|
4 |
|
$file = @fopen($path, 'r'); |
54
|
4 |
|
if (!$file) { |
55
|
2 |
|
return false; |
56
|
|
|
} |
57
|
2 |
|
$timestamp = fgets($file, 11); |
58
|
2 |
|
fclose($file); |
59
|
2 |
|
if ($timestamp > time() || $timestamp == "00000000000") { |
60
|
1 |
|
return substr(file_get_contents($path), 11); |
61
|
|
|
} |
62
|
1 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Must implement a delete method. |
67
|
|
|
* |
68
|
|
|
* @param string $key delete a specific cached item by key. |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
1 |
|
public function delete($key) |
72
|
|
|
{ |
73
|
1 |
|
$file = $this->filename($key); |
74
|
1 |
|
unlink($file); |
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Clear all of the values set by this cache instance. |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
1 |
|
public function clear() |
84
|
|
|
{ |
85
|
1 |
|
$key = $this->key(); |
|
|
|
|
86
|
1 |
|
$files = glob($this->path . "/_cache-" . $key . "*.cache"); |
87
|
1 |
|
$this->setKey(); |
88
|
1 |
|
$this->key(true); |
89
|
1 |
|
foreach ($files as $file) { |
90
|
1 |
|
unlink($file); |
91
|
1 |
|
} |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the filename of a key. |
97
|
|
|
* |
98
|
|
|
* @param string $key name of the key |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
4 |
|
public function filename($key) |
102
|
|
|
{ |
103
|
4 |
|
return $this->path . "/_cache-" . $this->key() . "-" . $key . ".cache"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a parsable value from the data and expiration date. |
108
|
|
|
* |
109
|
|
|
* @param string $value value of the store |
110
|
|
|
* @param integer $expiration integer value of the expiration (unix timestamp) |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
4 |
|
public function createValue($value, $expiration) |
114
|
|
|
{ |
115
|
4 |
|
$expiration = $expiration ? str_pad($expiration, 11, "0". STR_PAD_LEFT) : "00000000000"; |
116
|
4 |
|
return $expiration . $value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets the current cache namespace key. |
121
|
|
|
* |
122
|
|
|
* Note: to save on time spent reading/writing to disk, this method uses |
123
|
|
|
* static caching. Its important that when a cache key gets reset this |
124
|
|
|
* method has it's local cache reset by passing `true`. |
125
|
|
|
* |
126
|
|
|
* @param boolean $reset resets the static cache. |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
4 |
|
public function key($reset = false) |
130
|
|
|
{ |
131
|
4 |
|
static $key; |
132
|
4 |
|
$key = $reset ? false : $key; |
133
|
4 |
|
if (!$key) { |
134
|
2 |
|
$path = $this->path . "/.cache_key"; |
135
|
2 |
|
if (file_exists($path)) { |
136
|
1 |
|
$key = file_get_contents($path); |
137
|
1 |
|
} else { |
138
|
1 |
|
$key = $this->setKey(); |
139
|
|
|
} |
140
|
2 |
|
} |
141
|
4 |
|
return $key; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the current cache key. |
146
|
|
|
*/ |
147
|
2 |
|
public function setKey() |
148
|
|
|
{ |
149
|
2 |
|
$path = $this->path . "/.cache_key"; |
150
|
2 |
|
$key = bin2hex(openssl_random_pseudo_bytes(6)); |
151
|
2 |
|
file_put_contents($path, $key); |
152
|
2 |
|
return $key; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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: