Completed
Branch FET-3467-waitlists (78124b)
by
unknown
141:27 queued 128:30
created

BasicCacheManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 118
rs 10
wmc 10
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A cachePrefix() 0 4 1
B get() 0 35 6
A clear() 0 7 2
1
<?php
2
namespace EventEspresso\core\services\cache;
3
4
use Closure;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit;
7
8
9
10
/**
11
 * Class BasicCacheManager
12
 * Controls the creation and deletion of cached content
13
 *
14
 * @package       Event Espresso
15
 * @author        Brent Christensen
16
 * @since         4.9.31
17
 */
18
class BasicCacheManager implements CacheManagerInterface
19
{
20
21
    /**
22
     * @type string
23
     */
24
    const CACHE_PREFIX = 'ee_cache_';
25
26
    /**
27
     * set to true to monitor when content is being served from cache or not
28
     *
29
     * @type boolean
30
     */
31
    const DEBUG = false;
32
33
    /**
34
     * @var CacheStorageInterface $cache_storage
35
     */
36
    private $cache_storage;
37
38
39
40
    /**
41
     * BasicCacheManager constructor.
42
     *
43
     * @param CacheStorageInterface $cache_storage [required]
44
     */
45
    public function __construct(CacheStorageInterface $cache_storage)
46
    {
47
        $this->cache_storage = $cache_storage;
48
    }
49
50
51
52
    /**
53
     * returns a string that will be prepended to all cache identifiers
54
     *
55
     * @return string
56
     */
57
    public function cachePrefix()
58
    {
59
        return BasicCacheManager::CACHE_PREFIX;
60
    }
61
62
63
64
    /**
65
     * @param string  $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types.
66
     *                           May also be helpful to include an additional specific identifier,
67
     *                           such as a post ID as part of the $id_prefix so that individual caches
68
     *                           can be found and/or cleared. ex: "venue-28", or "shortcode-156".
69
     *                           BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id.
70
     * @param string  $cache_id  [required] Additional identifying details that make this cache unique.
71
     *                           It is advisable to use some of the actual data
72
     *                           that is used to generate the content being cached,
73
     *                           in order to guarantee that the cache id is unique for that content.
74
     *                           The cache id will be md5'd before usage to make it more db friendly,
75
     *                           and the entire cache id string will be truncated to 190 characters.
76
     * @param Closure $callback  [required] since the point of caching is to avoid generating content when not
77
     *                           necessary,
78
     *                           we wrap our content creation in a Closure so that it is not executed until needed.
79
     * @param int     $expiration
80
     * @return Closure|mixed
81
     */
82
    public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS)
83
    {
84
        $content = '';
85
        // how long should we cache this content for? 0 means no caching.
86
        $expiration = absint(
87
            apply_filters(
88
                'FHEE__CacheManager__get__cache_expiration',
89
                $expiration,
90
                $id_prefix,
91
                $cache_id
92
            )
93
        );
94
        $cache_id = substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182);
95
        // is caching enabled for this content ?
96
        if ($expiration) {
97
            $content = $this->cache_storage->get($cache_id);
98
        }
99
        // any existing content ?
100
        if (empty($content)) {
101
            // nope! let's generate some new stuff
102
            $content = $callback();
103
            // save the new content if caching is enabled
104
            if ($expiration) {
105
                if (BasicCacheManager::DEBUG) {
106
                    \EEH_Debug_Tools::printr($cache_id, 'REFRESH CACHE', __FILE__, __LINE__);
107
                }
108
                $this->cache_storage->add($cache_id, $content, $expiration);
109
            }
110
        } else {
111
            if (BasicCacheManager::DEBUG) {
112
                \EEH_Debug_Tools::printr($cache_id, 'CACHED CONTENT', __FILE__, __LINE__);
113
            }
114
        }
115
        return $content;
116
    }
117
118
119
120
    /**
121
     * @param array|string $cache_id [required] Could be an ID prefix affecting many caches
122
     *                               or a specific ID targeting a single cache item
123
     * @return void
124
     */
125
    public function clear($cache_id)
126
    {
127
        // ensure incoming arg is in an array
128
        $cache_id = is_array($cache_id) ? $cache_id : array($cache_id);
129
        // delete corresponding transients for the supplied id prefix
130
        $this->cache_storage->deleteMany($cache_id);
131
    }
132
133
134
135
}
136
// End of file BasicCacheManager.php
137
// Location: core/services/cache/BasicCacheManager.php