| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-cache.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.9.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache service. |
| 28 |
* |
| 29 |
* Uses cache objects to encapsulate cached data. |
| 30 |
* |
| 31 |
* This makes us completely independent from the problems related to |
| 32 |
* incomplete cache implementations that ignore the $found parameter used |
| 33 |
* to disambiguate cache misses with wp_cache_get() when false is retrieved. |
| 34 |
*/ |
| 35 |
class Groups_Cache { |
| 36 |
|
| 37 |
/** |
| 38 |
* Default cache group. |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
const CACHE_GROUP = 'groups'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Retrieve an entry from cache. |
| 45 |
* |
| 46 |
* @param string $key |
| 47 |
* @param string $group |
| 48 |
* @return Groups_Cache_Object|null returns a cache object on hit, null on cache miss |
| 49 |
*/ |
| 50 |
public static function get( $key, $group = self::CACHE_GROUP ) { |
| 51 |
$found = null; |
| 52 |
$value = wp_cache_get( $key, $group, false, $found ); |
| 53 |
if ( !( $value instanceof Groups_Cache_Object ) ) { |
| 54 |
$value = null; |
| 55 |
} |
| 56 |
return $value; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Store an entry in cache. |
| 61 |
* |
| 62 |
* @param string $key |
| 63 |
* @param string $value |
| 64 |
* @param string $group |
| 65 |
* @return true if successful, otherwise false |
| 66 |
*/ |
| 67 |
public static function set( $key, $value, $group = self::CACHE_GROUP ) { |
| 68 |
$object = new Groups_Cache_Object( $key, $value ); |
| 69 |
return wp_cache_set( $key, $object, $group ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Delete a cache entry. |
| 74 |
* |
| 75 |
* @param string $key |
| 76 |
* @param string $group |
| 77 |
* @return true if successful, otherwise false |
| 78 |
*/ |
| 79 |
public static function delete( $key, $group = self::CACHE_GROUP ) { |
| 80 |
return wp_cache_delete( $key, $group ); |
| 81 |
} |
| 82 |
} |
| 83 |
|