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;
13:
14: use Xmf\Module\Helper\AbstractHelper;
15:
16: /**
17: * Manage cache interaction in a module. Cache key will be prefixed
18: * with the module name to segregate it from keys set by other modules
19: * or system functions. Cache data is by definition serialized, so
20: * any arbitrary data (i.e. array, object) can be stored.
21: *
22: * @category Xmf\Module\Helper\Cache
23: * @package Xmf
24: * @author trabis <lusopoemas@gmail.com>
25: * @author Richard Griffith <richard@geekwright.com>
26: * @copyright 2011-2013 XOOPS Project (http://xoops.org)
27: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28: * @version Release: 1.0
29: * @link http://xoops.org
30: * @since 1.0
31: */
32: class Cache extends AbstractHelper
33: {
34: /**
35: * @var string[]
36: */
37: protected $prefix;
38:
39: /**
40: * @var \Xoops\Core\Cache\Access
41: */
42: protected $cache;
43:
44: /**
45: * Initialize parent::__constuct calls this after verifying module object.
46: *
47: * @return void
48: */
49: public function init()
50: {
51: $this->prefix = array('module', $this->module->getVar('dirname'));
52: $this->cache = \Xoops::getInstance()->cache();
53: }
54:
55: /**
56: * Add our module prefix to a name
57: *
58: * @param string $name name to prefix
59: *
60: * @return string[] module prefixed name
61: */
62: private function prefix($name)
63: {
64: $prefixedName = $this->prefix;
65: if (!empty($name)) {
66: $name = (array) $name;
67: foreach ($name as $n) {
68: $prefixedName[] = $n;
69: }
70: }
71: return $prefixedName;
72: }
73:
74: /**
75: * Write a value for a key to the cache
76: *
77: * @param string $key Identifier for the data
78: * @param mixed $value Data to be cached - anything except a resource
79: * @param int|DateTime|null $ttl Time to live, integer for ttl in seconds,
80: * DateTime object to expire at a specific time,
81: * or null for
82: *
83: * @return bool True if the data was successfully cached, false on failure
84: */
85: public function write($key, $value, $ttl = null)
86: {
87: return $this->cache->write($this->prefix($key), $value);
88: }
89:
90: /**
91: * Read value for a key from the cache
92: *
93: * @param string $key Identifier for the data
94: *
95: * @return mixed value if key was set, false not set or expired
96: */
97: public function read($key)
98: {
99: return $this->cache->read($this->prefix($key));
100: }
101:
102: /**
103: * Delete a key from the cache
104: *
105: * @param string $key Identifier for the data
106: *
107: * @return void
108: */
109: public function delete($key)
110: {
111: $this->cache->delete($this->prefix($key));
112: }
113:
114: /**
115: * cache block wrapper
116: *
117: * If the cache read for $key is a miss, call the $regenFunction to update it.
118: * With the PRECOMPUTE strategy, it will trigger a miss on a read on one caller
119: * before the cache expires, so it will be done in advance.
120: *
121: * @param string|string[] $cacheKey Identifier for the cache item
122: * @param callable $regenFunction function to generate cached content
123: * @param int|DateTime|null $ttl time to live, number ofseconds as integer,
124: * DateTime to expire at a specific time,
125: * or null for default
126: * @param mixed ...$args variable argument list for $regenFunction
127: *
128: * @return mixed
129: */
130: public function cacheRead($cacheKey, $regenFunction, $ttl = null, $args = null)
131: {
132: return $this->cache->cacheRead($this->prefix($cacheKey), $regenFunction, $ttl, $args);
133: }
134:
135: /**
136: * clear all keys and data from the module's cache. This will do a hierarchical
137: * delete on our module specific prefix.
138: *
139: * @return boolean True if the cache was successfully cleared, false otherwise
140: */
141: public function clear()
142: {
143: return $this->delete(array());
144: }
145: }
146: