| 1 |
<?php |
| 2 |
namespace Averta\WordPress\Cache; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\Sanitize; |
| 5 |
use DateInterval; |
| 6 |
use Psr\Http\Message\RequestInterface; |
| 7 |
use Psr\SimpleCache\CacheInterface; |
| 8 |
|
| 9 |
class WPCache implements CacheInterface{ |
| 10 |
|
| 11 |
/** |
| 12 |
* The list of transient keys |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
private $inUseKeys = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* A prefix for all transient keys |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $keyPrefix = ''; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Cache constructor. |
| 28 |
* |
| 29 |
* @param null $cachePrefix |
| 30 |
*/ |
| 31 |
public function __construct( $cachePrefix = null ) |
| 32 |
{ |
| 33 |
if( ! is_null( $cachePrefix ) ){ |
| 34 |
$this->keyPrefix = $cachePrefix; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the value of a transient. |
| 40 |
* |
| 41 |
* If the transient does not exist, does not have a value, or has expired, |
| 42 |
* then the return value will be false. |
| 43 |
* |
| 44 |
* @param string $key Cache key. Expected to not be SQL-escaped. |
| 45 |
* |
| 46 |
* @param bool $default Default cache value |
| 47 |
* |
| 48 |
* @return mixed Value of transient. |
| 49 |
*/ |
| 50 |
public function get( $key, $default = false ) { |
| 51 |
$key = $this->validateKey( $key ); |
| 52 |
|
| 53 |
$value = get_transient( $key ); |
| 54 |
if ( false === $value ) { |
| 55 |
$value = $default; |
| 56 |
} |
| 57 |
|
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set/update the value of a transient. |
| 63 |
* |
| 64 |
* You do not need to serialize values. If the value needs to be serialized, then |
| 65 |
* it will be serialized before it is set. |
| 66 |
* |
| 67 |
* |
| 68 |
* @param string $key Cache key. Expected to not be SQL-escaped. Must be |
| 69 |
* 172 characters or fewer in length. |
| 70 |
* @param mixed $value Transient value. Must be serializable if non-scalar. |
| 71 |
* Expected to not be SQL-escaped. |
| 72 |
* @param int $ttl Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 73 |
* |
| 74 |
* @return bool False if value was not set and true if value was set. |
| 75 |
*/ |
| 76 |
public function set( $key, $value, $ttl = null ): bool { |
| 77 |
$key = $this->validateKey( $key ); |
| 78 |
$this->addToKeysList( $key ); |
| 79 |
|
| 80 |
if ( $ttl instanceof DateInterval ) { |
| 81 |
$ttl = $this->convertDateIntervalToInteger( $ttl ); |
| 82 |
} |
| 83 |
|
| 84 |
return set_transient( $key, $value, intval($ttl) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Delete a transient. |
| 89 |
* |
| 90 |
* @param string $key Cache key. Expected to not be SQL-escaped. |
| 91 |
* |
| 92 |
* @return bool true if successful, false otherwise |
| 93 |
*/ |
| 94 |
public function delete( $key ): bool { |
| 95 |
$key = $this->validateKey( $key ); |
| 96 |
$this->deleteFromKeyList( $key ); |
| 97 |
|
| 98 |
return delete_transient( $key ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Convert a request object to a unique key |
| 103 |
* |
| 104 |
* @param RequestInterface $request |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function hashRequest( RequestInterface $request ) { |
| 109 |
$requestArgs = $request->getQueryParams(); |
| 110 |
|
| 111 |
$requestArgs['url'] = $request->getRequestTarget(); |
| 112 |
$requestArgs['url'] = remove_query_arg( ['flush', 'clearCache'], $requestArgs['url'] ); |
| 113 |
|
| 114 |
// exclude flush params from hash request key |
| 115 |
unset( $requestArgs['flush'] ); |
| 116 |
unset( $requestArgs['clearCache'] ); |
| 117 |
|
| 118 |
$requestArgs = $this->beforeHashRequest( $requestArgs, $request ); |
| 119 |
|
| 120 |
return Sanitize::textfield( md5( serialize( $requestArgs ) ) ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Prepares request args for hashing |
| 125 |
* Override this method to change requestArgs before hash |
| 126 |
* |
| 127 |
* @param array $requestArgs |
| 128 |
* @param RequestInterface $request |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
protected function beforeHashRequest( array $requestArgs, RequestInterface $request ){ |
| 133 |
return $requestArgs; |
| 134 |
} |
| 135 |
|
| 136 |
public function has( $key ): bool { |
| 137 |
return $this->get( $key, false ) !== false; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public function getMultiple( $keys, $default = null ) { |
| 142 |
$result = []; |
| 143 |
|
| 144 |
foreach ( $keys as $key ) { |
| 145 |
$result[ $key ] = $this->get( $key, $default ); |
| 146 |
} |
| 147 |
|
| 148 |
return $result; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public function setMultiple( $values, $ttl = null ): bool { |
| 153 |
foreach ( $values as $key => $value ) { |
| 154 |
if ( $this->set( $key, $value, $ttl ) ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
public function deleteMultiple( $keys ): bool { |
| 165 |
|
| 166 |
foreach ( $keys as $key ) { |
| 167 |
if ( $this->delete( $key ) ) { |
| 168 |
continue; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public function clear(): bool { |
| 178 |
|
| 179 |
if( ! empty( $this->keyPrefix ) ) { |
| 180 |
global $wpdb; |
| 181 |
$wpdb->query($wpdb->prepare( |
| 182 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", |
| 183 |
"_transient_{$this->keyPrefix}%", |
| 184 |
"_transient_timeout_{$this->keyPrefix}%" |
| 185 |
)); |
| 186 |
} |
| 187 |
|
| 188 |
return $this->deleteMultiple( $this->inUseKeys() ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param DateInterval $ttl |
| 193 |
* |
| 194 |
* @return int |
| 195 |
*/ |
| 196 |
private function convertDateIntervalToInteger( DateInterval $ttl ) : int { |
| 197 |
|
| 198 |
return ( new DateTime() ) |
| 199 |
->setTimestamp(0) |
| 200 |
->add( $ttl ) |
| 201 |
->getTimestamp(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Adds a key to cache key list |
| 206 |
* |
| 207 |
* @param string $key |
| 208 |
*/ |
| 209 |
private function addToKeysList( $key ): void { |
| 210 |
$this->inUseKeys[ $key ] = $key; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Removes a key from cache key list |
| 215 |
* |
| 216 |
* @param string $key |
| 217 |
*/ |
| 218 |
private function deleteFromKeyList( $key ): void { |
| 219 |
unset( $this->inUseKeys[ $key ] ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
private function inUseKeys(): array { |
| 224 |
return $this->inUseKeys; |
| 225 |
} |
| 226 |
|
| 227 |
protected function validateKey( $key ){ |
| 228 |
return $this->keyPrefix . ltrim( $key, $this->keyPrefix ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
public function prevent(){ |
| 233 |
if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
| 234 |
define( 'DONOTCACHEPAGE', true ); |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! defined( 'DONOTCACHEDB' ) ) { |
| 238 |
define( 'DONOTCACHEDB', true ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( ! defined( 'DONOTMINIFY' ) ) { |
| 242 |
define( 'DONOTMINIFY', true ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! defined( 'DONOTCDN' ) ) { |
| 246 |
define( 'DONOTCDN', true ); |
| 247 |
} |
| 248 |
|
| 249 |
if ( ! defined( 'DONOTCACHCEOBJECT' ) ) { |
| 250 |
define( 'DONOTCACHCEOBJECT', true ); |
| 251 |
} |
| 252 |
|
| 253 |
// prevent caching. |
| 254 |
nocache_headers(); |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|