Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Cacheable | |
0.00% |
0 / 26 |
|
0.00% |
0 / 10 |
182 | |
0.00% |
0 / 1 |
| store | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| generate_cache_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonUnserialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| get | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| set_cache_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_cache_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| clear_cache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| delete_by_cache_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| cache_prefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Base abstract class for cacheable value objects. |
| 4 | * |
| 5 | * @package automattic/jetpack-boost-core |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Boost_Core\Lib; |
| 9 | |
| 10 | /** |
| 11 | * Class Cacheable. |
| 12 | */ |
| 13 | abstract class Cacheable implements \JsonSerializable { |
| 14 | |
| 15 | /** |
| 16 | * Default cache expiry. |
| 17 | * Can be overridden by child classes. |
| 18 | */ |
| 19 | protected const DEFAULT_EXPIRY = 300; // 5 minutes. |
| 20 | |
| 21 | /** |
| 22 | * The ID of this object, if cached as a transient. |
| 23 | * |
| 24 | * @var string|null $cache_id Cache id. |
| 25 | */ |
| 26 | private $cache_id; |
| 27 | |
| 28 | /** |
| 29 | * Store the object. |
| 30 | * |
| 31 | * @param int $expiry Expiry in seconds. |
| 32 | * |
| 33 | * @return mixed|void |
| 34 | */ |
| 35 | public function store( $expiry = null ) { |
| 36 | if ( null === $expiry ) { |
| 37 | $expiry = static::DEFAULT_EXPIRY; |
| 38 | } |
| 39 | |
| 40 | if ( ! $this->cache_id ) { |
| 41 | $this->cache_id = $this->generate_cache_id(); |
| 42 | } |
| 43 | |
| 44 | Transient::set( |
| 45 | static::cache_prefix() . $this->cache_id, |
| 46 | $this->jsonSerialize(), |
| 47 | $expiry |
| 48 | ); |
| 49 | |
| 50 | return $this->cache_id; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Default implementation for generating cache key. Generates a random ASCII string. |
| 55 | */ |
| 56 | protected function generate_cache_id() { |
| 57 | return wp_generate_password( 20, false ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * This is intended to be the reverse of JsonSerializable->jsonSerialize. |
| 62 | * |
| 63 | * @param array $data Serialized data to restore the object from. |
| 64 | * |
| 65 | * @throws \Exception Throw an exception to remind to implement the method in child classes. |
| 66 | */ |
| 67 | abstract public static function jsonUnserialize( $data ); |
| 68 | |
| 69 | /** |
| 70 | * Fetch an object with the given ID. |
| 71 | * |
| 72 | * @param string $id The object ID. |
| 73 | * |
| 74 | * @return null|mixed |
| 75 | */ |
| 76 | public static function get( $id ) { |
| 77 | $data = Transient::get( static::cache_prefix() . $id, false ); |
| 78 | if ( ! $data ) { |
| 79 | return null; |
| 80 | } |
| 81 | $class = get_called_class(); |
| 82 | $object = $class::jsonUnserialize( $data, $id ); |
| 83 | if ( $object ) { |
| 84 | $object->set_cache_id( $id ); |
| 85 | } |
| 86 | |
| 87 | return $object; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set cache id. |
| 92 | * |
| 93 | * This is here so we know how this object was loaded. |
| 94 | * |
| 95 | * @param string $cache_id Cache id. |
| 96 | */ |
| 97 | public function set_cache_id( $cache_id ) { |
| 98 | $this->cache_id = $cache_id; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Getter for the cache ID. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_cache_id() { |
| 107 | return $this->cache_id; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Delete the cache entry. |
| 112 | */ |
| 113 | public function delete() { |
| 114 | $this->cache_id && static::delete_by_cache_id( $this->cache_id ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Delete all currently cached entries. |
| 119 | */ |
| 120 | public static function clear_cache() { |
| 121 | Transient::delete_by_prefix( static::cache_prefix() ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Delete the cache entry for the given cache id. |
| 126 | * |
| 127 | * @param string $cache_id The cache ID. |
| 128 | */ |
| 129 | public static function delete_by_cache_id( $cache_id ) { |
| 130 | Transient::delete( static::cache_prefix() . $cache_id ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns the cache prefix |
| 135 | * |
| 136 | * @throws \Exception Throw an exception to remind to implement the method in child classes. |
| 137 | */ |
| 138 | abstract protected static function cache_prefix(); |
| 139 | } |