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