Assets.php
2 weeks ago
Cache.php
2 weeks ago
Date.php
2 weeks ago
Debug.php
2 weeks ago
FeedReader.php
2 weeks ago
HTML.php
2 weeks ago
Helper.php
2 weeks ago
JSON.php
2 weeks ago
Nonce.php
2 weeks ago
Notice.php
2 weeks ago
Number.php
2 weeks ago
NumberConverter.php
2 weeks ago
Param.php
2 weeks ago
Sanitizing.php
2 weeks ago
Strip.php
2 weeks ago
Templates.php
2 weeks ago
User.php
2 weeks ago
Validating.php
2 weeks ago
WooCommerce.php
2 weeks ago
WordPress.php
2 weeks ago
Cache.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || die(); |
| 6 | |
| 7 | class Cache { |
| 8 | private const cacheGroup = WP_PARSI_KEY; |
| 9 | |
| 10 | /** |
| 11 | * Determines cache is existing |
| 12 | * |
| 13 | * @param string $key Cache key |
| 14 | * @param bool $useDBCache Use DB Cache |
| 15 | * |
| 16 | * @return bool True, if cache exist |
| 17 | */ |
| 18 | public static function exists( string $key, bool $useDBCache = true ): bool { |
| 19 | return self::get( $key, $useDBCache ) !== false; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Set cache value |
| 24 | * |
| 25 | * @param string $key Cache key |
| 26 | * @param mixed $value Cache value |
| 27 | * @param int $expireTime Expire time base on second's |
| 28 | * @param bool $useDBCache Use DB Cache |
| 29 | * |
| 30 | * @return bool True, if cache exist |
| 31 | */ |
| 32 | public static function set( string $key, $value, int $expireTime = 0, bool $useDBCache = true ): bool { |
| 33 | $cache = $cache2 = wp_cache_set( $key, $value, self::cacheGroup, $expireTime ); |
| 34 | |
| 35 | if ( $useDBCache && $expireTime > 0 ) { |
| 36 | $cache2 = set_transient( $key . '_' . self::cacheGroup, $value, $expireTime ); |
| 37 | } |
| 38 | |
| 39 | return $cache || $cache2; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get cache value |
| 44 | * |
| 45 | * @param string $key Cache key |
| 46 | * @param bool $useDBCache Use DB Cache |
| 47 | * |
| 48 | * @return mixed Cache value, Return false if not exists |
| 49 | */ |
| 50 | public static function get( string $key, bool $useDBCache = true ) { |
| 51 | if ( ( $value = wp_cache_get( $key, self::cacheGroup ) ) !== false ) { |
| 52 | return $value; |
| 53 | } |
| 54 | |
| 55 | if ( $useDBCache && ( $value = get_transient( $key . '_' . self::cacheGroup ) ) !== false ) { |
| 56 | return $value; |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Delete cache |
| 65 | * |
| 66 | * @param string $key Cache key |
| 67 | * @param bool $deleteDBCache Delete DB Cache |
| 68 | * |
| 69 | * @return bool True, if cache successfully deleted |
| 70 | */ |
| 71 | public static function delete( string $key, bool $deleteDBCache = true ): bool { |
| 72 | $cache = wp_cache_delete( $key, self::cacheGroup ); |
| 73 | |
| 74 | $cache2 = false; |
| 75 | if ( $deleteDBCache ) { |
| 76 | $cache2 = delete_transient( $key . '_' . self::cacheGroup ); |
| 77 | } |
| 78 | |
| 79 | return $cache || $cache2; |
| 80 | } |
| 81 | } |
| 82 |