1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf\Module\Helper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manage cache interaction in a module. Cache key will be prefixed |
16
|
|
|
* with the module name to segregate it from keys set by other modules |
17
|
|
|
* or system functions. Cache data is by definition serialized, so |
18
|
|
|
* any arbitrary data (i.e. array, object) can be stored. |
19
|
|
|
* |
20
|
|
|
* @category Xmf\Module\Helper\Cache |
21
|
|
|
* @package Xmf |
22
|
|
|
* @author trabis <[email protected]> |
23
|
|
|
* @author Richard Griffith <[email protected]> |
24
|
|
|
* @copyright 2011-2016 XOOPS Project (http://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
* @version Release: 1.0 |
27
|
|
|
* @link http://xoops.org |
28
|
|
|
* @since 1.0 |
29
|
|
|
*/ |
30
|
|
|
class Cache extends AbstractHelper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $prefix; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Xoops\Core\Cache\Access |
39
|
|
|
*/ |
40
|
|
|
protected $cache; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initialize parent::__constuct calls this after verifying module object. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function init() |
48
|
|
|
{ |
49
|
|
|
$this->prefix = 'module/' . $this->module->getVar('dirname'); |
50
|
|
|
$this->cache = \Xoops::getInstance()->cache(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add our module prefix to a name |
55
|
|
|
* |
56
|
|
|
* @param string $name name to prefix |
57
|
|
|
* |
58
|
|
|
* @return string module prefixed name |
59
|
|
|
*/ |
60
|
|
|
protected function prefix($name) |
61
|
|
|
{ |
62
|
|
|
return $this->prefix . '/'. $name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Write a value for a key to the cache |
67
|
|
|
* |
68
|
|
|
* @param string $key Identifier for the data |
69
|
|
|
* @param mixed $value Data to be cached - anything except a resource |
70
|
|
|
* @param int|DateTime|null $ttl Time to live, integer for ttl in seconds, |
71
|
|
|
* DateTime object to expire at a specific time, |
72
|
|
|
* or null for |
73
|
|
|
* |
74
|
|
|
* @return bool True if the data was successfully cached, false on failure |
75
|
|
|
*/ |
76
|
|
|
public function write($key, $value, $ttl = null) |
77
|
|
|
{ |
78
|
|
|
return $this->cache->write($this->prefix($key), $value, $ttl); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Read value for a key from the cache |
83
|
|
|
* |
84
|
|
|
* @param string $key Identifier for the data |
85
|
|
|
* @param mixed $default default value to return if config $key is not set |
86
|
|
|
* |
87
|
|
|
* @return mixed value if key was set, false not set or expired |
88
|
|
|
*/ |
89
|
|
|
public function read($key, $default = false) |
90
|
|
|
{ |
91
|
|
|
$value = $this->cache->read($this->prefix($key)); |
92
|
|
|
return (false !== $value) ? $value : $default; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Delete a key from the cache |
97
|
|
|
* |
98
|
|
|
* @param string $key Identifier for the data |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function delete($key) |
103
|
|
|
{ |
104
|
|
|
$this->cache->delete($this->prefix($key)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* cache block wrapper |
109
|
|
|
* |
110
|
|
|
* If the cache read for $key is a miss, call the $regenFunction to update it. |
111
|
|
|
* With the PRECOMPUTE strategy, it will trigger a miss on a read on one caller |
112
|
|
|
* before the cache expires, so it will be done in advance. |
113
|
|
|
* |
114
|
|
|
* @param string|string[] $cacheKey Identifier for the cache item |
115
|
|
|
* @param callable $regenFunction function to generate cached content |
116
|
|
|
* @param int|DateTime|null $ttl time to live, number ofseconds as integer, |
117
|
|
|
* DateTime to expire at a specific time, |
118
|
|
|
* or null for default |
119
|
|
|
* @param mixed ...$args variable argument list for $regenFunction |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
public function cacheRead($cacheKey, $regenFunction, $ttl = null, $args = null) |
124
|
|
|
{ |
125
|
|
|
return $this->cache->cacheRead($this->prefix($cacheKey), $regenFunction, $ttl, $args); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* clear all keys and data from the module's cache. This will do a hierarchical |
130
|
|
|
* delete on our module specific prefix. |
131
|
|
|
* |
132
|
|
|
* @return boolean True if the cache was successfully cleared, false otherwise |
133
|
|
|
*/ |
134
|
|
|
public function clear() |
135
|
|
|
{ |
136
|
|
|
return $this->cache->delete($this->prefix); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.