Test Failed
Push — master ( 398493...d4ef72 )
by Michael
11:04
created

Cache::cacheRead()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 4
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-2018 XOOPS Project (https://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @link      https://xoops.org
27
 */
28
class Cache extends AbstractHelper
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $prefix;
34
35
    /**
36
     * @var XoopsCache
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\XoopsCache was not found. Did you mean XoopsCache? If so, make sure to prefix the type with \.
Loading history...
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