|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir WordPress plugin. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +require_once __DIR__.'/bootstrap.php'; |
| 15 | + |
| 16 | +use Ymir\Plugin\ObjectCache\ObjectCacheInterface; |
| 17 | +use Ymir\Plugin\ObjectCache\PersistentObjectCacheInterface; |
| 18 | +use Ymir\Plugin\ObjectCache\PreloadedObjectCacheInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Ymir object cache API. |
| 22 | + * |
| 23 | + * @link https://developer.wordpress.org/reference/classes/wp_object_cache |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * Adds data to the cache, if the cache key doesn't already exist. |
| 28 | + * |
| 29 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_add |
| 30 | + */ |
| 31 | +function wp_cache_add($key, $data,$group = '', $expire = 0): bool |
| 32 | +{ |
| 33 | + global $wp_object_cache; |
| 34 | + |
| 35 | + return $wp_object_cache->add(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Adds a group or set of groups to the list of global groups. |
| 40 | + * |
| 41 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_add_global_groups |
| 42 | + */ |
| 43 | +function wp_cache_add_global_groups($groups) |
| 44 | +{ |
| 45 | + global $wp_object_cache; |
| 46 | + |
| 47 | + $wp_object_cache->addGlobalGroups((array) $groups); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Adds a group or set of groups to the list of non-persistent groups. |
| 52 | + * |
| 53 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_add_non_persistent_groups |
| 54 | + */ |
| 55 | +function wp_cache_add_non_persistent_groups($groups) |
| 56 | +{ |
| 57 | + global $wp_object_cache; |
| 58 | + |
| 59 | + $wp_object_cache->addNonPersistentGroups((array) $groups); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Closes the cache. |
| 64 | + * |
| 65 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_close |
| 66 | + */ |
| 67 | +function wp_cache_close(): bool |
| 68 | +{ |
| 69 | + global $wp_object_cache; |
| 70 | + |
| 71 | + return $wp_object_cache->close(); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Decrements numeric cache item's value. |
| 76 | + * |
| 77 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_decr |
| 78 | + */ |
| 79 | +function wp_cache_decr($key, $offset = 1, $group = '') |
| 80 | +{ |
| 81 | + global $wp_object_cache; |
| 82 | + |
| 83 | + return $wp_object_cache->decrement(trim((string) $group) ?: 'default', (string) $key, (int) $offset); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Removes the cache contents matching key and group. |
| 88 | + * |
| 89 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_delete |
| 90 | + */ |
| 91 | +function wp_cache_delete($key, $group = ''): bool |
| 92 | +{ |
| 93 | + global $wp_object_cache; |
| 94 | + |
| 95 | + return $wp_object_cache->delete(trim((string) $group) ?: 'default', (string) $key); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Removes all cache items. |
| 100 | + * |
| 101 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_flush |
| 102 | + */ |
| 103 | +function wp_cache_flush(): bool |
| 104 | +{ |
| 105 | + global $wp_object_cache; |
| 106 | + |
| 107 | + return $wp_object_cache->flush(); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Retrieves the cache contents from the cache by key and group. |
| 112 | + * |
| 113 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_get |
| 114 | + */ |
| 115 | +function wp_cache_get($key, $group = '', $force = false, &$found = null) |
| 116 | +{ |
| 117 | + global $wp_object_cache; |
| 118 | + |
| 119 | + return $wp_object_cache->get(trim((string) $group) ?: 'default', (string) $key, (bool) $force, $found); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Retrieves multiple values from the cache in one call. |
| 124 | + * |
| 125 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_get_multiple |
| 126 | + */ |
| 127 | +function wp_cache_get_multiple($keys, $group = '', $force = false): array |
| 128 | +{ |
| 129 | + global $wp_object_cache; |
| 130 | + |
| 131 | + return $wp_object_cache->getMultiple(trim((string) $group) ?: 'default', (array) $keys, (bool) $force); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Increment numeric cache item's value. |
| 136 | + * |
| 137 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_incr |
| 138 | + */ |
| 139 | +function wp_cache_incr($key, $offset = 1, $group = '') |
| 140 | +{ |
| 141 | + global $wp_object_cache; |
| 142 | + |
| 143 | + return $wp_object_cache->increment(trim((string) $group) ?: 'default', (string) $key, (int) $offset); |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Sets up Object Cache Global and assigns it. |
| 148 | + * |
| 149 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_init |
| 150 | + */ |
| 151 | +function wp_cache_init() |
| 152 | +{ |
| 153 | + global $wp_object_cache, $ymir; |
| 154 | + |
| 155 | + try { |
| 156 | + $objectCache = $ymir->getContainer()->get('ymir_object_cache'); |
| 157 | + |
| 158 | + if (!$objectCache instanceof ObjectCacheInterface) { |
| 159 | + throw new RuntimeException('Object cache needs to implement ObjectCacheInterface'); |
| 160 | + } elseif ($objectCache instanceof PersistentObjectCacheInterface && !$objectCache->isAvailable()) { |
| 161 | + throw new RuntimeException('Persistent object cache is unavailable'); |
| 162 | + } |
| 163 | + |
| 164 | + $wp_object_cache = $objectCache; |
| 165 | + |
| 166 | + if ($objectCache instanceof PreloadedObjectCacheInterface) { |
| 167 | + $objectCache->load(); |
| 168 | + } |
| 169 | + } catch (Exception $exception) { |
| 170 | + $wp_object_cache = $ymir->getContainer()->get('wordpress_object_cache'); |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Replaces the contents of the cache with new data. |
| 177 | + * |
| 178 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_replace |
| 179 | + */ |
| 180 | +function wp_cache_replace($key, $data, $group = '', $expire = 0): bool |
| 181 | +{ |
| 182 | + global $wp_object_cache; |
| 183 | + |
| 184 | + return $wp_object_cache->replace(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire); |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Reset internal cache keys and structures. |
| 189 | + * |
| 190 | + * If the cache back end uses global blog or site IDs as part of its cache keys, |
| 191 | + * this function instructs the back end to reset those keys and perform any cleanup |
| 192 | + * since blog or site IDs have changed since cache init. |
| 193 | + * |
| 194 | + * This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
| 195 | + * function when preparing the cache for a blog switch. For clearing the cache |
| 196 | + * during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
| 197 | + * recommended outside of unit tests as the performance penalty for using it is |
| 198 | + * high. |
| 199 | + * |
| 200 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_reset |
| 201 | + */ |
| 202 | +function wp_cache_reset() { |
| 203 | + _deprecated_function(__FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()'); |
| 204 | +} |
| 205 | + |
| 206 | +/** |
| 207 | + * Saves the data to the cache. |
| 208 | + * |
| 209 | + * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. |
| 210 | + * |
| 211 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_save |
| 212 | + */ |
| 213 | +function wp_cache_set($key, $data, $group = '', $expire = 0): bool |
| 214 | +{ |
| 215 | + global $wp_object_cache; |
| 216 | + |
| 217 | + return $wp_object_cache->set(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire); |
| 218 | +} |
| 219 | + |
| 220 | +/** |
| 221 | + * Switches the internal blog ID. |
| 222 | + * |
| 223 | + * This changes the blog id used to create keys in blog specific groups. |
| 224 | + * |
| 225 | + * @link https://developer.wordpress.org/reference/functions/wp_cache_switch_to_blog |
| 226 | + */ |
| 227 | +function wp_cache_switch_to_blog($blogId) |
| 228 | +{ |
| 229 | + global $wp_object_cache; |
| 230 | + |
| 231 | + $wp_object_cache->switchToBlog((int) $blogId); |
| 232 | +} |
0 commit comments