Last active
March 11, 2017 11:08
-
-
Save zacscott/8272ac1660516df0c9db to your computer and use it in GitHub Desktop.
Caching helper class for WordPress (can be used as a mu-plugin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Cache Helper | |
* Description: CacheHelper class provider plugin | |
* Version: 1.0 | |
* Author: Zachary Scott | |
*/ | |
namespace zacscott; | |
/** | |
* Caching helper which stores objects using the transients API. | |
* | |
* Exmaple: | |
* | |
* $cache = new CacheHelper( 'my-cache' ); | |
* $cache->set( 'my-key', $value ); | |
* echo $cache->get( 'my-key' ); | |
* | |
* You can also cclear the cache like so: | |
* | |
* $cache->clear(); | |
* | |
* @author Zachary Scott <[email protected]> | |
*/ | |
class CacheManager { | |
private $group; // the cache group id | |
/** | |
* | |
* @param $group - The cache group ID | |
*/ | |
public function __construct( $group ) { | |
assert( ! empty( $group ) ); | |
$this->group = $group; | |
} | |
/** | |
* Stores the given value in the cache. | |
* | |
* @param $key - the key to file the value under | |
* @param $value - the value to store | |
* @param $ttl - Time the value is cached for (default infinite) | |
*/ | |
public function set( $key, $value, $ttl = 0 ) { | |
assert( ! empty( $key ) ); | |
// Store the value in the transient | |
$key = $this->group . '_' . $key; | |
set_transient( $key, $value ); | |
// store the cache key, for alter lookup | |
$keys = get_transient( $this->group ); | |
if ( ! is_array( $keys ) ) { | |
$keys = array(); | |
} | |
$keys[] = $key; | |
$keys = array_unique( $keys ); | |
set_transient( $this->group, $keys ); | |
} | |
/** | |
* Returns the given value from the cache. | |
* | |
* @param $key The key of the cache item to get. | |
*/ | |
public function get( $key ) { | |
assert( ! empty( $key ) ); | |
return get_transient( $this->group . '_ ' . $key ); | |
} | |
/** | |
* Clears the given cache entry. | |
* If no key is given, it clears the entire cache. | |
* | |
* @param $key The cache key to clear. If not given, will clear the entire | |
* cache. | |
*/ | |
public function clear( $key = '' ) { | |
if ( empty( $key ) ) { | |
$keys = get_transient( $htis->group ); | |
if ( ! is_array( $keys ) ) { | |
$keys = array(); | |
} | |
foreach ( $keys as $key ) { | |
transient_delete( $this->group . '_' . $key ); | |
} | |
} else { | |
transient_delete( $this->group . '_' . $key ); | |
} | |
} | |
} | |
/** | |
* Copyright (C) 2014 2015, Zachary Scott <[email protected]> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment