| 1 |
<?php |
| 2 |
/** |
| 3 |
* Uses WordPress transients to store data. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Storage\Drivers; |
| 11 |
|
| 12 |
use Closure; |
| 13 |
use SolidWP\Performance\Storage\Contracts\Storage; |
| 14 |
use SolidWP\Performance\Storage\Exceptions\InvalidKeyException; |
| 15 |
use SolidWP\Performance\Storage\Traits\With_Key_Formatter; |
| 16 |
|
| 17 |
/** |
| 18 |
* Uses WordPress transients to store data. |
| 19 |
* |
| 20 |
* @package SolidWP\Performance |
| 21 |
*/ |
| 22 |
class Transient_Storage implements Storage { |
| 23 |
|
| 24 |
use With_Key_Formatter; |
| 25 |
|
| 26 |
/** |
| 27 |
* Put a value in storage. |
| 28 |
* |
| 29 |
* @param string|int|float|mixed[]|object $key The storage key. Accepts any variable that can be json encoded. |
| 30 |
* @param mixed $value The value to store. |
| 31 |
* @param int $expire The storage lifespan in seconds. |
| 32 |
* |
| 33 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 34 |
*/ |
| 35 |
public function set( $key, $value, int $expire = 0 ): bool { |
| 36 |
return (bool) set_transient( $this->key( $key ), $value, $expire ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get a value from storage. |
| 41 |
* |
| 42 |
* @param string|int|float|mixed[]|object $key The storage key. Accepts any variable that can be json encoded. |
| 43 |
* |
| 44 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 45 |
* |
| 46 |
* @return null|mixed Returns null if we can't find the storage value. |
| 47 |
*/ |
| 48 |
public function get( $key ) { |
| 49 |
$value = get_transient( $this->key( $key ) ); |
| 50 |
|
| 51 |
return $value !== false ? $value : null; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Delete a value from storage. |
| 56 |
* |
| 57 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 58 |
* |
| 59 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 60 |
*/ |
| 61 |
public function delete( $key ): bool { |
| 62 |
return (bool) delete_transient( $this->key( $key ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get an item from storage, or execute the given Closure and store the result. |
| 67 |
* |
| 68 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 69 |
* @param Closure $callback The callback used to generate and store the value. |
| 70 |
* @param int $expire The storage lifespan in seconds. |
| 71 |
* |
| 72 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 73 |
* |
| 74 |
* @return mixed The storage value. |
| 75 |
*/ |
| 76 |
public function remember( $key, Closure $callback, int $expire = 0 ) { |
| 77 |
$value = $this->get( $key ); |
| 78 |
|
| 79 |
if ( ! is_null( $value ) ) { |
| 80 |
return $value; |
| 81 |
} |
| 82 |
|
| 83 |
$value = $callback(); |
| 84 |
|
| 85 |
$this->set( $key, $value, $expire ); |
| 86 |
|
| 87 |
return $value; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Retrieve an item from storage and delete it. |
| 92 |
* |
| 93 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 94 |
* |
| 95 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 96 |
*/ |
| 97 |
public function pull( $key ) { |
| 98 |
$value = $this->get( $key ); |
| 99 |
|
| 100 |
$this->delete( $key ); |
| 101 |
|
| 102 |
return $value; |
| 103 |
} |
| 104 |
} |
| 105 |
|