1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cache using the filesystem |
4
|
|
|
* |
5
|
|
|
* @package erdiko/core |
6
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
7
|
|
|
* @author Varun Brahme |
8
|
|
|
* @author John Arroyo <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace erdiko\core\cache; |
11
|
|
|
|
12
|
|
|
use erdiko\core\cache\CacheInterface; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class File extends \erdiko\core\datasource\File implements CacheInterface |
16
|
|
|
{ |
17
|
|
|
/** Constructor */ |
18
|
|
|
public function __construct($cacheDir = null) |
19
|
|
|
{ |
20
|
|
|
if (!isset($cacheDir)) { |
21
|
|
|
$cacheDir = ERDIKO_VAR."/cache"; |
22
|
|
|
} |
23
|
|
|
parent::__construct($cacheDir); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get Key Code |
28
|
|
|
* |
29
|
|
|
* @param string $key |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getKeyCode($key) |
33
|
|
|
{ |
34
|
|
|
return md5($key); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Put Key |
39
|
|
|
* |
40
|
|
|
* @param mixed $key |
41
|
|
|
* @param mixed $data |
42
|
|
|
*/ |
43
|
|
|
public function put($key, $data) |
44
|
|
|
{ |
45
|
|
|
$filename = $this->getKeyCode($key); |
46
|
|
|
$data = json_encode($data); |
47
|
|
|
$this->write($data, $filename); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get Key |
52
|
|
|
* |
53
|
|
|
* @param string @key |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function get($key) |
57
|
|
|
{ |
58
|
|
|
$filename = $this->getKeyCode($key); |
59
|
|
|
|
60
|
|
|
if ($this->fileExists($filename)) { |
61
|
|
|
$value = $this->read($filename); |
62
|
|
|
} else { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return json_decode($value, true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Delete a key |
71
|
|
|
* |
72
|
|
|
* @param string @key |
73
|
|
|
* @note path is only for compatibility, do not use |
74
|
|
|
*/ |
75
|
|
|
public function delete($key, $path = null) |
76
|
|
|
{ |
77
|
|
|
$filename = $this->getKeyCode($key); |
78
|
|
|
parent::delete($filename, $path); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete all keys |
83
|
|
|
*/ |
84
|
|
|
public function clear() |
85
|
|
|
{ |
86
|
|
|
$files = glob(ERDIKO_VAR."/cache/*"); |
87
|
|
|
foreach ($files as $file) { |
88
|
|
|
if (is_file($file)) { |
89
|
|
|
parent::delete(basename($file)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if a key exists |
96
|
|
|
* |
97
|
|
|
* @param mixed $key |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function has($key) |
101
|
|
|
{ |
102
|
|
|
$filename = $this->getKeyCode($key); |
103
|
|
|
return $this->fileExists($filename); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.