|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Ion |
|
4
|
|
|
* |
|
5
|
|
|
* Building blocks for web development |
|
6
|
|
|
* |
|
7
|
|
|
* @package Ion |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Aviat\Ion\Cache; |
|
14
|
|
|
|
|
15
|
|
|
use \Aviat\Ion\Di\ContainerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class proxying cached and fresh values from the selected cache driver |
|
19
|
|
|
*/ |
|
20
|
|
|
class CacheManager implements CacheInterface { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheDriverInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $driver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retreive the appropriate driver from the container |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ContainerInterface $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$config = $container->get('config'); |
|
33
|
|
|
$driverConf = $config->get('cache_driver'); |
|
34
|
|
|
|
|
35
|
|
|
if (empty($driverConf)) |
|
36
|
|
|
{ |
|
37
|
|
|
$driverConf = 'SQLDriver'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}"; |
|
41
|
|
|
$driver = new $driverClass($container); |
|
42
|
|
|
|
|
43
|
|
|
$this->driver = $driver; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Retreive a cached value if it exists, otherwise, get the value |
|
48
|
|
|
* from the passed arguments |
|
49
|
|
|
* |
|
50
|
|
|
* @param object $object - object to retrieve fresh value from |
|
51
|
|
|
* @param string $method - method name to call |
|
52
|
|
|
* @param [array] $args - the arguments to pass to the retrieval method |
|
|
|
|
|
|
53
|
|
|
* @return mixed - the cached or fresh data |
|
54
|
|
|
*/ |
|
55
|
|
|
public function get($object, $method, array $args=[]) |
|
56
|
|
|
{ |
|
57
|
|
|
$hash = $this->generateHashForMethod($object, $method, $args); |
|
58
|
|
|
|
|
59
|
|
|
$data = $this->driver->get($hash); |
|
60
|
|
|
|
|
61
|
|
|
if (empty($data)) |
|
62
|
|
|
{ |
|
63
|
|
|
$data = call_user_func_array([$object, $method], $args); |
|
64
|
|
|
$this->driver->set($hash, $data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retreive a fresh value from the method, and update the cache |
|
72
|
|
|
* @param object $object - object to retrieve fresh value from |
|
73
|
|
|
* @param string $method - method name to call |
|
74
|
|
|
* @param [array] $args - the arguments to pass to the retrieval method |
|
|
|
|
|
|
75
|
|
|
* @return mixed - the fresh data |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getFresh($object, $method, array $args=[]) |
|
78
|
|
|
{ |
|
79
|
|
|
$hash = $this->generateHashForMethod($object, $method, $args); |
|
80
|
|
|
$data = call_user_func_array([$object, $method], $args); |
|
81
|
|
|
$this->driver->set($hash, $data); |
|
82
|
|
|
return $data; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Clear the entire cache |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function purge() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->driver->invalidateAll(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Generate a hash as a cache key from the current method call |
|
97
|
|
|
* |
|
98
|
|
|
* @param object $object |
|
99
|
|
|
* @param string $method |
|
100
|
|
|
* @param array $args |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function generateHashForMethod($object, $method, array $args) |
|
104
|
|
|
{ |
|
105
|
|
|
$classname = get_class($object); |
|
106
|
|
|
$keyObj = [ |
|
107
|
|
|
'class' => $classname, |
|
108
|
|
|
'method' => $method, |
|
109
|
|
|
'args' => $args, |
|
110
|
|
|
]; |
|
111
|
|
|
$hash = sha1(json_encode($keyObj)); |
|
112
|
|
|
return $hash; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
// End of CacheManager.php |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.