| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cache Utility |
| 4 |
* |
| 5 |
* @package Disco |
| 6 |
* @subpackage App\Utility |
| 7 |
* @since 1.0.0 |
| 8 |
* @category Utility |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Disco\App\Utility; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Cache |
| 15 |
* |
| 16 |
* @package Disco |
| 17 |
* @subpackage App\Utility |
| 18 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 19 |
* @link https://webappick.com |
| 20 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 21 |
* @category Utility |
| 22 |
*/ |
| 23 |
class Cache { |
| 24 |
|
| 25 |
const DISCO_CACHE_PREFIX = 'disco_';// phpcs:ignore |
| 26 |
|
| 27 |
/** |
| 28 |
* Get Cached Data |
| 29 |
* If cache not found then call callback function or method, cache the data and return data. |
| 30 |
* If callback is not callable then return false. |
| 31 |
* |
| 32 |
* @param string $key Cache Name. |
| 33 |
* @param array|null $callback Callback function or method. |
| 34 |
* @param array $args Callback arguments. |
| 35 |
* @return mixed|false false if cache not found. |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
public static function remember( $key, $callback = null, $args = array() ) { |
| 39 |
$key = self::sanitize_key( $key ); |
| 40 |
|
| 41 |
$data = wp_cache_get( $key, DISCO_TEXTDOMAIN ); |
| 42 |
|
| 43 |
if ( false === $data && $callback && is_callable( $callback ) ) { |
| 44 |
$data = call_user_func( $callback, ...$args ); |
| 45 |
|
| 46 |
if ( ! is_wp_error( $data ) ) { |
| 47 |
wp_cache_set( $key, $data, DISCO_TEXTDOMAIN ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $data; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Set Cached Data |
| 56 |
* |
| 57 |
* @param string $key Cache name. Expected to not be SQL-escaped. Must be |
| 58 |
* 172 characters or fewer. |
| 59 |
* @param mixed $data Data to cache. |
| 60 |
* @param int|bool $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
public static function set( $key, $data, $expiration = false ) { |
| 64 |
$key = self::sanitize_key( $key ); |
| 65 |
|
| 66 |
self::forget( $key ); |
| 67 |
|
| 68 |
if ( $expiration && ! is_numeric( $expiration ) ) { |
| 69 |
$expiration = PHP_INT_MAX; |
| 70 |
} elseif ( is_numeric( $expiration ) ) { |
| 71 |
$expiration = time() + $expiration; |
| 72 |
} else { |
| 73 |
$expiration = 0; |
| 74 |
} |
| 75 |
|
| 76 |
return wp_cache_set( $key, $data, DISCO_TEXTDOMAIN, $expiration ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Retrieve and subsequently delete a value from the object cache. |
| 81 |
* |
| 82 |
* @param string $key The cache key. |
| 83 |
* @param string $group Optional. The cache group. Default is empty. |
| 84 |
* @param mixed $default_value Optional. The default value to return if the given key doesn't |
| 85 |
* exist in the object cache. Default is null. |
| 86 |
* @return mixed The cached value, when available, or $default. |
| 87 |
*/ |
| 88 |
public static function forget( $key, $group = '', $default_value = null ) { |
| 89 |
$found = false; |
| 90 |
$cached = wp_cache_get( $key, $group, false, $found ); |
| 91 |
|
| 92 |
if ( false !== $found ) { |
| 93 |
wp_cache_delete( $key, $group ); |
| 94 |
|
| 95 |
return $cached; |
| 96 |
} |
| 97 |
|
| 98 |
return $default_value; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Delete All Cached Data |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public static function flush() { |
| 107 |
$disco_keys = wp_cache_get( 'disco_cache_keys', DISCO_TEXTDOMAIN ); |
| 108 |
|
| 109 |
if ( ! is_array( $disco_keys ) ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
foreach ( $disco_keys as $key ) { |
| 114 |
wp_cache_delete( $key, DISCO_TEXTDOMAIN ); |
| 115 |
} |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Sanitize Cache Key |
| 122 |
* Remove all non-alphanumeric characters. |
| 123 |
* |
| 124 |
* @param string $key Cache Name. |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public static function sanitize_key( $key ) { |
| 128 |
if ( empty( $key ) ) { |
| 129 |
return self::DISCO_CACHE_PREFIX; |
| 130 |
} |
| 131 |
|
| 132 |
// Remove repeated cache prefix. |
| 133 |
$key = str_replace( self::DISCO_CACHE_PREFIX, '', $key ); |
| 134 |
// Set cache prefix |
| 135 |
$key = self::DISCO_CACHE_PREFIX . $key; |
| 136 |
// Remove all non-alphanumeric characters. |
| 137 |
$key = sanitize_key( $key ); |
| 138 |
|
| 139 |
// Cache disco keys to flush later. |
| 140 |
$disco_keys = wp_cache_get( 'disco_cache_keys', DISCO_TEXTDOMAIN ); |
| 141 |
|
| 142 |
if ( false === $disco_keys ) { |
| 143 |
$disco_keys = array( $key ); |
| 144 |
} elseif ( is_array( $disco_keys ) && ! in_array( $key, $disco_keys, true ) ) { |
| 145 |
$disco_keys[] = $key; |
| 146 |
} |
| 147 |
|
| 148 |
wp_cache_set( 'disco_cache_keys', $disco_keys, DISCO_TEXTDOMAIN ); |
| 149 |
|
| 150 |
return $key; |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|