CacheEngine.php
3 years ago
CacheException.php
1 year ago
CacheNameSpaceTrait.php
1 month ago
ObjectCache.php
1 year ago
WPCacheEngine.php
1 year ago
WPCacheEngine.php
136 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 The 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 | * Retrieves a set of objects cached under the given keys. |
| 27 | * |
| 28 | * @param string[] $keys The keys under which the object to retrieve is cached. |
| 29 | * @param string $group The group under which the object is cached. |
| 30 | * |
| 31 | * @return array The cached array of objects keyed by the given keys, values will be null for any non-cached keys. |
| 32 | */ |
| 33 | public function get_cached_objects( array $keys, string $group = '' ) { |
| 34 | $prefix = self::get_cache_prefix( $group ); |
| 35 | $key_map = array_combine( |
| 36 | $keys, |
| 37 | array_map( |
| 38 | function ( $key ) use ( $prefix ) { |
| 39 | return $prefix . $key; |
| 40 | }, |
| 41 | $keys |
| 42 | ) |
| 43 | ); |
| 44 | |
| 45 | $cached_values = wp_cache_get_multiple( array_values( $key_map ), $group ); |
| 46 | $return_values = array(); |
| 47 | foreach ( $key_map as $key => $prefixed_key ) { |
| 48 | if ( isset( $cached_values[ $prefixed_key ] ) && false !== $cached_values[ $prefixed_key ] ) { |
| 49 | $return_values[ $key ] = $cached_values[ $prefixed_key ]; |
| 50 | } else { |
| 51 | $return_values[ $key ] = null; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $return_values; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Caches an object under a given key, and with a given expiration. |
| 60 | * |
| 61 | * @param string $key The key under which the object will be cached. |
| 62 | * @param array|object $object The object to cache. |
| 63 | * @param int $expiration Expiration for the cached object, in seconds. |
| 64 | * @param string $group The group under which the object will be cached. |
| 65 | * |
| 66 | * @return bool True if the object is cached successfully, false otherwise. |
| 67 | */ |
| 68 | public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool { |
| 69 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 70 | return false !== wp_cache_set( $prefixed_key, $object, $group, $expiration ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Caches an object under a given key, and with a given expiration. |
| 75 | * |
| 76 | * @param array $objects The objects to cache keyed by the key to cache under. |
| 77 | * @param int $expiration Expiration for the cached object, in seconds. |
| 78 | * @param string $group The group under which the object will be cached. |
| 79 | * |
| 80 | * @return array Array of return values, grouped by key. Each value is either |
| 81 | * true on success, or false on failure |
| 82 | */ |
| 83 | public function cache_objects( array $objects, int $expiration, string $group = '' ): array { |
| 84 | $prefix = self::get_cache_prefix( $group ); |
| 85 | |
| 86 | $objects = array_combine( |
| 87 | array_map( |
| 88 | function ( $key ) use ( $prefix ) { |
| 89 | return $prefix . $key; |
| 90 | }, |
| 91 | array_keys( $objects ) |
| 92 | ), |
| 93 | $objects, |
| 94 | ); |
| 95 | |
| 96 | return wp_cache_set_multiple( $objects, $group, $expiration ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Removes a cached object from the cache. |
| 101 | * |
| 102 | * @param string $key They key under which the object is cached. |
| 103 | * @param string $group The group under which the object is cached. |
| 104 | * |
| 105 | * @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason). |
| 106 | */ |
| 107 | public function delete_cached_object( string $key, string $group = '' ): bool { |
| 108 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 109 | return false !== wp_cache_delete( $prefixed_key, $group ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Checks if an object is cached under a given key. |
| 114 | * |
| 115 | * @param string $key The key to verify. |
| 116 | * @param string $group The group under which the object is cached. |
| 117 | * |
| 118 | * @return bool True if there's an object cached under the given key, false otherwise. |
| 119 | */ |
| 120 | public function is_cached( string $key, string $group = '' ): bool { |
| 121 | $prefixed_key = self::get_prefixed_key( $key, $group ); |
| 122 | return false !== wp_cache_get( $prefixed_key, $group ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Deletes all cached objects under a given group. |
| 127 | * |
| 128 | * @param string $group The group to delete. |
| 129 | * |
| 130 | * @return bool True if the group is deleted successfully, false otherwise. |
| 131 | */ |
| 132 | public function delete_cache_group( string $group = '' ): bool { |
| 133 | return false !== self::invalidate_cache_group( $group ); |
| 134 | } |
| 135 | } |
| 136 |