Skip to content

Instantly share code, notes, and snippets.

@jacobsantos
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsantos/a2a26a76e9625ddd8ccc to your computer and use it in GitHub Desktop.
Save jacobsantos/a2a26a76e9625ddd8ccc to your computer and use it in GitHub Desktop.
WordPress Cache Refactoring Example
<?php
interface CacheDataInterface {
public function setIndex($name, $group = '');
public function index();
public function setData( $value );
public function data();
public function increment( $offset = 1 );
public function decrement( $offset = 1 );
public function setExpiration( $time = 0 );
public function expiration();
public function setForce( $force = false );
public function force();
public function toArray();
public function fromArray( array $data );
}
<?php
interface CacheStorageInterface {
public function initialize();
public function deinitialize();
public function add( CacheDataInterface $cache );
public function remove( CacheDataInterface $cache );
public function get( CacheDataInterface $cache, &$found = null );
public function set( CacheDataInterface $cache );
public function replace( CacheDataInterface $cache );
public function flush();
public function reset();
}
<?php
function cache_registry(CacheStorageInterface $storage = null) {
static $_storage = null;
if ( is_object($storage) ) {
$_storage = $storage;
}
if ( !is_object($_storage) ) {
$_storage = new Default_Cache_Storage;
}
return $_storage;
}
function cache_data_object_name( $className = null ) {
static $_objectName = 'Default_Cache_Object';
if ( $className !== null && class_exists($className) ) {
$_objectName = $className;
}
return $_objectName;
}
function cache_new_data_object() {
$objName = cache_data_object_name();
return new $objName;
}
function cache_set_data_object( $key, $data, $group = '', $expire = 0 ) {
return cache_new_data_object()->setIndex( $key, $group )->setExpire( (int) $expire )->setData($data);
}
<?php
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
return cache_registry()->add( cache_set_data_object( $key, $data, $group, $expire ) );
}
function wp_cache_delete($key, $group = '') {
return cache_registry()->remove( cache_new_data_object()->setIndex( $key, $group ) );
}
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
$cache = cache_new_data_object()->setIndex( $key, $group )->setForce( $force );
return cache_registry()->get( $cache, &$found )->data();
}
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
$cache = cache_registry()->get( cache_new_data_object()->setIndex( $key, $group ) );
return cache_registry()->replace( $cache->increment( $offset ) );
}
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
$cache = cache_registry()->get( cache_new_data_object()->setIndex( $key, $group ) );
return cache_registry()->replace( $cache->decrement( $offset ) );
}
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
return cache_registry()->replace( cache_set_data_object( $key, $data, $group, $expire ) );
}
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
return cache_registry()->set( cache_set_data_object( $key, $data, $group, $expire ) );
}
function wp_cache_switch_to_blog( $blog_id ) {
$obj = cache_registry();
if ( method_exists($obj, 'switch_to_blog') ) {
return $obj->switch_to_blog( $blog_id );
}
if ( method_exists($obj, 'wp_cache_switch_to_blog') ) {
return $obj->wp_cache_switch_to_blog( $blog_id );
}
return null;
}
function wp_cache_add_global_groups( $groups ) {
$obj = cache_registry();
if ( method_exists($obj, 'add_global_groups') ) {
return $obj->add_global_groups( $groups );
}
if ( method_exists($obj, 'wp_cache_add_global_groups') ) {
return $obj->wp_cache_add_global_groups( $groups );
}
return null;
}
function wp_cache_add_non_persistent_groups( $groups ) {
$obj = cache_registry();
if ( method_exists($obj, 'add_non_persistent_groups') ) {
return $obj->add_non_persistent_groups( $groups );
}
if ( method_exists($obj, 'wp_cache_add_non_persistent_groups') ) {
return $obj->wp_cache_add_non_persistent_groups( $groups );
}
return null;
}
function wp_cache_flush() {
return cache_registry()->flush();
}
function wp_cache_init() {
return cache_registry()->initialize();
}
function wp_cache_close() {
return cache_registry()->deinitialize();
}
function wp_cache_reset() {
_deprecated_function( __FUNCTION__, '3.5' );
return cache_registry()->reset();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment