|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\services\cache; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use EventEspresso\core\domain\services\session\SessionIdentifierInterface; |
|
6
|
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class BasicCacheManager |
|
13
|
|
|
* Controls the creation and deletion of cached content |
|
14
|
|
|
* |
|
15
|
|
|
* @package Event Espresso |
|
16
|
|
|
* @author Brent Christensen |
|
17
|
|
|
* @since 4.9.31 |
|
18
|
|
|
*/ |
|
19
|
|
|
class BasicCacheManager implements CacheManagerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @type string |
|
24
|
|
|
*/ |
|
25
|
|
|
const CACHE_PREFIX = 'ee_cache_'; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var CacheStorageInterface $cache_storage |
|
30
|
|
|
*/ |
|
31
|
|
|
private $cache_storage; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* BasicCacheManager constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param CacheStorageInterface $cache_storage [required] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(CacheStorageInterface $cache_storage) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->cache_storage = $cache_storage; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* returns a string that will be prepended to all cache identifiers |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function cachePrefix() |
|
53
|
|
|
{ |
|
54
|
|
|
return BasicCacheManager::CACHE_PREFIX; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
61
|
|
|
* May also be helpful to include an additional specific identifier, |
|
62
|
|
|
* such as a post ID as part of the $id_prefix so that individual caches |
|
63
|
|
|
* can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
64
|
|
|
* BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
65
|
|
|
* @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
66
|
|
|
* It is advisable to use some of the actual data |
|
67
|
|
|
* that is used to generate the content being cached, |
|
68
|
|
|
* in order to guarantee that the cache id is unique for that content. |
|
69
|
|
|
* The cache id will be md5'd before usage to make it more db friendly, |
|
70
|
|
|
* and the entire cache id string will be truncated to 190 characters. |
|
71
|
|
|
* @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
72
|
|
|
* necessary, |
|
73
|
|
|
* we wrap our content creation in a Closure so that it is not executed until needed. |
|
74
|
|
|
* @param int $expiration |
|
75
|
|
|
* @return Closure|mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS) |
|
78
|
|
|
{ |
|
79
|
|
|
$content = ''; |
|
80
|
|
|
$expiration = absint( |
|
81
|
|
|
apply_filters( |
|
82
|
|
|
'FHEE__CacheManager__get__cache_expiration', |
|
83
|
|
|
$expiration, |
|
84
|
|
|
$id_prefix, |
|
85
|
|
|
$cache_id |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
$cache_id = $this->generateCacheIdentifier($id_prefix, $cache_id); |
|
89
|
|
|
// is caching enabled for this content ? |
|
90
|
|
|
if ($expiration) { |
|
91
|
|
|
$content = $this->cache_storage->get($cache_id); |
|
92
|
|
|
} |
|
93
|
|
|
// any existing content ? |
|
94
|
|
|
if (empty($content)) { |
|
95
|
|
|
// nope! let's generate some new stuff |
|
96
|
|
|
$content = $callback(); |
|
97
|
|
|
// save the new content if caching is enabled |
|
98
|
|
|
if ($expiration) { |
|
99
|
|
|
$this->cache_storage->add($cache_id, $content, $expiration); |
|
100
|
|
|
if (EE_DEBUG) { |
|
101
|
|
|
$content .= $this->displayCacheNotice($cache_id, 'REFRESH CACHE'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
if (EE_DEBUG) { |
|
106
|
|
|
$content .= $this->displayCacheNotice($cache_id, 'CACHED CONTENT'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return $content; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Generates a unique identifier string for the cache |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $id_prefix [required] see BasicCacheManager::get() |
|
118
|
|
|
* @param string $cache_id [required] see BasicCacheManager::get() |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
private function generateCacheIdentifier($id_prefix, $cache_id) |
|
122
|
|
|
{ |
|
123
|
|
|
// let's make the cached content unique for this "page" |
|
124
|
|
|
$cache_id .= filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL); |
|
125
|
|
|
// with these parameters |
|
126
|
|
|
$cache_id .= filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL); |
|
127
|
|
|
// then md5 the above to control it's length, add all of our prefixes, and truncate |
|
128
|
|
|
return substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
135
|
|
|
* or a specific ID targeting a single cache item |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
public function clear($cache_id) |
|
139
|
|
|
{ |
|
140
|
|
|
// ensure incoming arg is in an array |
|
141
|
|
|
$cache_id = is_array($cache_id) ? $cache_id : array($cache_id); |
|
142
|
|
|
// delete corresponding transients for the supplied id prefix |
|
143
|
|
|
$this->cache_storage->deleteMany($cache_id); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
150
|
|
|
* or a specific ID targeting a single cache item |
|
151
|
|
|
* @param string $type |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
private function displayCacheNotice($cache_id, $type) { |
|
155
|
|
|
return ' |
|
156
|
|
|
<div class="ee-cached-content-notice" style="position:fixed; bottom:0; left: 0;"> |
|
157
|
|
|
<p style="font-size:9px;font-weight:normal;color:#666;line-height: 12px;margin:0 0 3px 5px"> |
|
158
|
|
|
<b>' . $type . '</b><span style="color:#999"> : </span> |
|
159
|
|
|
<span>' . $cache_id . '</span> |
|
160
|
|
|
<span style="margin-left:2em;">' . __FILE__ . '</span> |
|
161
|
|
|
</p> |
|
162
|
|
|
</div>'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
// End of file BasicCacheManager.php |
|
167
|
|
|
// Location: core/services/cache/BasicCacheManager.php |
|
168
|
|
|
|