| 1 |
<?php |
| 2 |
namespace Averta\WordPress\Utility; |
| 3 |
|
| 4 |
|
| 5 |
class Cache{ |
| 6 |
|
| 7 |
public static function prevent(){ |
| 8 |
if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
| 9 |
define( 'DONOTCACHEPAGE', true ); |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! defined( 'DONOTCACHEDB' ) ) { |
| 13 |
define( 'DONOTCACHEDB', true ); |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! defined( 'DONOTMINIFY' ) ) { |
| 17 |
define( 'DONOTMINIFY', true ); |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! defined( 'DONOTCDN' ) ) { |
| 21 |
define( 'DONOTCDN', true ); |
| 22 |
} |
| 23 |
|
| 24 |
if ( ! defined( 'DONOTCACHCEOBJECT' ) ) { |
| 25 |
define( 'DONOTCACHCEOBJECT', true ); |
| 26 |
} |
| 27 |
|
| 28 |
// prevent caching. |
| 29 |
nocache_headers(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the value of a transient. |
| 34 |
* |
| 35 |
* If the transient does not exist, does not have a value, or has expired, |
| 36 |
* then the return value will be false. |
| 37 |
* |
| 38 |
* @param string $key Cache key. Expected to not be SQL-escaped. |
| 39 |
* |
| 40 |
* @return mixed Value of transient. |
| 41 |
*/ |
| 42 |
public static function getDatabaseCache( $key ) { |
| 43 |
global $_wp_using_ext_object_cache; |
| 44 |
|
| 45 |
$current_using_cache = $_wp_using_ext_object_cache; |
| 46 |
$_wp_using_ext_object_cache = false; |
| 47 |
|
| 48 |
$result = get_transient( $key ); |
| 49 |
|
| 50 |
$_wp_using_ext_object_cache = $current_using_cache; |
| 51 |
|
| 52 |
return $result; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set/update the value of a transient. |
| 57 |
* |
| 58 |
* You do not need to serialize values. If the value needs to be serialized, then |
| 59 |
* it will be serialized before it is set. |
| 60 |
* |
| 61 |
* |
| 62 |
* @param string $key Cache key. Expected to not be SQL-escaped. Must be |
| 63 |
* 172 characters or fewer in length. |
| 64 |
* @param mixed $value Transient value. Must be serializable if non-scalar. |
| 65 |
* Expected to not be SQL-escaped. |
| 66 |
* @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 67 |
* |
| 68 |
* @return bool False if value was not set and true if value was set. |
| 69 |
*/ |
| 70 |
public static function setDatabaseCache( $key, $value, $expiration = 0 ) { |
| 71 |
global $_wp_using_ext_object_cache; |
| 72 |
|
| 73 |
$current_using_cache = $_wp_using_ext_object_cache; |
| 74 |
$_wp_using_ext_object_cache = false; |
| 75 |
|
| 76 |
$result = set_transient( $key, $value, $expiration ); |
| 77 |
|
| 78 |
$_wp_using_ext_object_cache = $current_using_cache; |
| 79 |
|
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Delete a transient. |
| 85 |
* |
| 86 |
* @param string $key Cache key. Expected to not be SQL-escaped. |
| 87 |
* |
| 88 |
* @return bool true if successful, false otherwise |
| 89 |
*/ |
| 90 |
public static function deleteDatabaseCache( $key ) { |
| 91 |
global $_wp_using_ext_object_cache; |
| 92 |
|
| 93 |
$current_using_cache = $_wp_using_ext_object_cache; |
| 94 |
$_wp_using_ext_object_cache = false; |
| 95 |
|
| 96 |
$result = delete_transient( $key ); |
| 97 |
|
| 98 |
$_wp_using_ext_object_cache = $current_using_cache; |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|