| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\SmartCache\Storage; |
| 4 |
|
| 5 |
class WpCache { |
| 6 |
|
| 7 |
/** |
| 8 |
* @var string |
| 9 |
*/ |
| 10 |
public $group_name; |
| 11 |
|
| 12 |
/** |
| 13 |
* @param string $group_name |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function __construct( $group_name ) { |
| 17 |
$this->group_name = $group_name; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get the data from cache/transient based on the provided key |
| 22 |
* |
| 23 |
* @param string $key unique id for this request |
| 24 |
* @return mixed|array|object|null The graphql response or false if not found |
| 25 |
*/ |
| 26 |
public function get( $key ) { |
| 27 |
return wp_cache_get( $key, $this->group_name ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @param string $key unique id for this request |
| 32 |
* @param mixed|array|object|null $data The graphql response |
| 33 |
* @param int $expire Time in seconds for the data to persist in cache. Zero means no expiration. |
| 34 |
* |
| 35 |
* @return bool False if value was not set and true if value was set. |
| 36 |
*/ |
| 37 |
public function set( $key, $data, $expire ) { |
| 38 |
return wp_cache_set( |
| 39 |
$key, |
| 40 |
is_array( $data ) ? $data : $data->toArray(), |
| 41 |
$this->group_name, |
| 42 |
// phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined |
| 43 |
$expire |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return bool True on success, false on failure. |
| 49 |
*/ |
| 50 |
public function purge_all() { |
| 51 |
return wp_cache_flush(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param string $key unique id for this request |
| 56 |
* @return bool True on successful removal, false on failure. |
| 57 |
*/ |
| 58 |
public function delete( $key ) { |
| 59 |
return wp_cache_delete( $key, $this->group_name ); |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|