CacheEngine.php
3 years ago
CacheException.php
3 years ago
CacheNameSpaceTrait.php
3 years ago
ObjectCache.php
3 years ago
WPCacheEngine.php
3 years ago
WPCacheEngine.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Caching; |
| 4 | |
| 5 | /** |
| 6 | * Implementation of CacheEngine that uses the built-in WordPress cache. |
| 7 | */ |
| 8 | class WPCacheEngine implements CacheEngine { |
| 9 | use CacheNameSpaceTrait; |
| 10 | |
| 11 | /** |
| 12 | * Retrieves an object cached under a given key. |
| 13 | * |
| 14 | * @param string $key They key under which the object to retrieve is cached. |
| 15 | * @param string $group The group under which the object is cached. |
| 16 | * |
| 17 | * @return array|object|null The cached object, or null if there's no object cached under the passed key. |
| 18 | */ |
| 19 | public function get_cached_object( string $key, string $group = '' ) { |
| 20 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 21 | $value = wp_cache_get( $prefixed_key, $group ); |
| 22 | return false === $value ? null : $value; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Caches an object under a given key, and with a given expiration. |
| 27 | * |
| 28 | * @param string $key The key under which the object will be cached. |
| 29 | * @param array|object $object The object to cache. |
| 30 | * @param int $expiration Expiration for the cached object, in seconds. |
| 31 | * @param string $group The group under which the object will be cached. |
| 32 | * |
| 33 | * @return bool True if the object is cached successfully, false otherwise. |
| 34 | */ |
| 35 | public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool { |
| 36 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 37 | return wp_cache_set( $prefixed_key, $object, $group, $expiration ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Removes a cached object from the cache. |
| 42 | * |
| 43 | * @param string $key They key under which the object is cached. |
| 44 | * @param string $group The group under which the object is cached. |
| 45 | * |
| 46 | * @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason). |
| 47 | */ |
| 48 | public function delete_cached_object( string $key, string $group = '' ): bool { |
| 49 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 50 | return wp_cache_delete( $prefixed_key, $group ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Checks if an object is cached under a given key. |
| 55 | * |
| 56 | * @param string $key The key to verify. |
| 57 | * @param string $group The group under which the object is cached. |
| 58 | * |
| 59 | * @return bool True if there's an object cached under the given key, false otherwise. |
| 60 | */ |
| 61 | public function is_cached( string $key, string $group = '' ): bool { |
| 62 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 63 | return false !== wp_cache_get( $prefixed_key, $group ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Deletes all cached objects under a given group. |
| 68 | * |
| 69 | * @param string $group The group to delete. |
| 70 | * |
| 71 | * @return bool True if the group is deleted successfully, false otherwise. |
| 72 | */ |
| 73 | public function delete_cache_group( string $group = '' ): bool { |
| 74 | return self::invalidate_cache_group( $group ); |
| 75 | } |
| 76 | } |
| 77 |