| 1 |
<?php |
| 2 |
|
| 3 |
namespace OptimoleWP\PageProfiler\Storage; |
| 4 |
|
| 5 |
/** |
| 6 |
* WordPress object cache implementation for page profiler data storage. |
| 7 |
* |
| 8 |
* This class provides methods to store, retrieve, and manage page profiler data |
| 9 |
* using WordPress's object cache system. |
| 10 |
*/ |
| 11 |
class ObjectCache extends Base { |
| 12 |
/** |
| 13 |
* Cache group name for Optimole page profiler data. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
const GROUP = 'optimole_page_profiler'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Default cache expiration time in seconds (7 days). |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
const EXPIRATION = 7 * DAY_IN_SECONDS; |
| 25 |
|
| 26 |
/** |
| 27 |
* The cache expiration time in seconds. |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
private $expiration; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initialize the object cache storage. |
| 35 |
* |
| 36 |
* Sets up the cache expiration time, which can be modified using the |
| 37 |
* 'optml_page_profiler_object_cache_expiration' filter. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
$this->expiration = apply_filters( 'optml_page_profiler_object_cache_expiration', self::EXPIRATION ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Store data in the object cache. |
| 45 |
* |
| 46 |
* @param string $key The unique identifier for the data. |
| 47 |
* @param array $data The data to store. |
| 48 |
* @return bool True on success, false on failure. |
| 49 |
*/ |
| 50 |
public function store( string $key, array $data ) { |
| 51 |
return wp_cache_set( $key, $data, self::GROUP, $this->expiration ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Retrieve data from the object cache. |
| 56 |
* |
| 57 |
* @param string $key The unique identifier for the data to retrieve. |
| 58 |
* @return array|false The stored data or false if not found. |
| 59 |
*/ |
| 60 |
public function get( string $key ) { |
| 61 |
return wp_cache_get( $key, self::GROUP ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Delete data from the object cache. |
| 66 |
* |
| 67 |
* @param string $key The unique identifier for the data to delete. |
| 68 |
* @return bool True on success, false on failure. |
| 69 |
*/ |
| 70 |
public function delete( string $key ) { |
| 71 |
return wp_cache_delete( $key, self::GROUP ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Delete all data from the object cache group. |
| 76 |
* |
| 77 |
* @return bool True on success, false on failure. |
| 78 |
*/ |
| 79 |
public function delete_all() { |
| 80 |
return wp_cache_flush_group( self::GROUP ); |
| 81 |
} |
| 82 |
} |
| 83 |
|