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 <lusopoemas@gmail.com>
23: * @author Richard Griffith <richard@geekwright.com>
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: * @link http://xoops.org
27: */
28: class Cache extends AbstractHelper
29: {
30: /**
31: * @var string
32: */
33: protected $prefix;
34:
35: /**
36: * @var XoopsCache
37: */
38: protected $cache;
39:
40: /**
41: * Initialize parent::__construct calls this after verifying module object.
42: *
43: * @return void
44: */
45: public function init()
46: {
47: \XoopsLoad::load('xoopscache');
48: $this->prefix = $this->module->getVar('dirname') . '_';
49: $this->cache = \XoopsCache::getInstance();
50: }
51:
52: /**
53: * Add our module prefix to a name
54: *
55: * @param string $name name to prefix
56: *
57: * @return string module prefixed name
58: */
59: protected function prefix($name)
60: {
61: return $this->prefix . $name;
62: }
63:
64: /**
65: * Write a value for a key to the cache
66: *
67: * @param string $key Identifier for the data
68: * @param mixed $value Data to be cached - anything except a resource
69: * @param int|null $ttl Time to live in seconds
70: *
71: * @return bool True if the data was successfully cached, false on failure
72: */
73: public function write($key, $value, $ttl = null)
74: {
75: return $this->cache->write($this->prefix($key), $value, $ttl);
76: }
77:
78: /**
79: * Read value for a key from the cache
80: *
81: * @param string $key Identifier for the data
82: * @param mixed $default default value to return if config $key is not set
83: *
84: * @return mixed value if key was set, false not set or expired
85: */
86: public function read($key, $default = false)
87: {
88: $value = $this->cache->read($this->prefix($key));
89: return (false !== $value) ? $value : $default;
90: }
91:
92: /**
93: * Delete a key from the cache
94: *
95: * @param string $key Identifier for the data
96: *
97: * @return void
98: */
99: public function delete($key)
100: {
101: $this->cache->delete($this->prefix($key));
102: }
103:
104: /**
105: * cache block wrapper
106: *
107: * If the cache read for $key is a miss, call the $regenFunction to update it.
108: *
109: * @param string $key Identifier for the cache item
110: * @param callable $regenFunction function to generate cached content
111: * @param int|null $ttl time to live, number of seconds as integer
112: * or null for default
113: * @param mixed $args variable argument list for $regenFunction
114: *
115: * @return mixed
116: */
117: public function cacheRead($key, $regenFunction, $ttl = null, $args = null)
118: {
119: if (null === $args) {
120: $varArgs = array();
121: } else {
122: $varArgs = func_get_args();
123: array_shift($varArgs); // pull off $key
124: array_shift($varArgs); // pull off $regenFunction
125: array_shift($varArgs); // pull off $ttl
126: }
127:
128: $cachedContent = $this->read($key);
129:
130: // Check to see if the cache missed, which could mean that it either didn't exist or was stale.
131: if ($cachedContent === false) {
132: // Run the relatively expensive code.
133: $cachedContent = call_user_func_array($regenFunction, $varArgs);
134:
135: // save result
136: $this->write($key, $cachedContent, $ttl);
137: }
138:
139: return $cachedContent;
140: }
141: }
142: