diff options
| author | turon <spider@turon> | 2014-04-29 00:07:01 -0400 |
|---|---|---|
| committer | turon <spider@turon> | 2014-04-29 00:07:01 -0400 |
| commit | 16f86286c078692ddd3df778caaf58327724af4a (patch) | |
| tree | 3745137df627a31b318e905da09dc65d1ea647df | |
| parent | 5463bc29aec0b23609a299a76bf57b65f51ca7f2 (diff) | |
| download | kernel-16f86286c078692ddd3df778caaf58327724af4a.tar.gz kernel-16f86286c078692ddd3df778caaf58327724af4a.tar.bz2 kernel-16f86286c078692ddd3df778caaf58327724af4a.zip | |
tweak caching API to BitCacheable interface; clean up of caching code
| -rw-r--r-- | BitBase.php | 44 | ||||
| -rw-r--r-- | BitSingleton.php | 12 | ||||
| -rw-r--r-- | BitSystem.php | 2 |
3 files changed, 40 insertions, 18 deletions
diff --git a/BitBase.php b/BitBase.php index 6b33f4d..7cb44b1 100644 --- a/BitBase.php +++ b/BitBase.php @@ -25,6 +25,13 @@ require_once ( KERNEL_PKG_PATH.'BitDbBase.php' ); define( 'STORAGE_BINARY', 1 ); define( 'STORAGE_IMAGE', 2 ); + +interface BitCacheable { + public function getCacheKey(); +} + + + /** * Virtual base class (as much as one can have such things in PHP) for all * derived bitweaver classes that require database access. @@ -99,7 +106,7 @@ abstract class BitBase { function __destruct() { unset( $this->mDb ); - $this->storeInCache( $this->getCacheKey() ); + $this->storeInCache(); } /** @@ -113,8 +120,8 @@ abstract class BitBase { public function clearFromCache( &$pParamHash=NULL ) { $this->mCacheTime = BIT_QUERY_CACHE_DISABLE; - if( static::isCacheable() && static::isCacheActive() && ($cacheKey = $this->getCacheKey()) ) { - $ret = apc_store( $cacheKey, $this, 3600 ); + if( $this->isCacheableObject() && static::isCacheActive() && ($cacheKey = $this->getCacheUuid()) ) { + $ret = apc_delete( $cacheKey ); } } @@ -125,16 +132,18 @@ abstract class BitBase { * @access public * @return TRUE on success, FALSE on failure */ - private function storeInCache( $pCacheKey ) { - if( $pCacheKey && static::isCacheable() && static::isCacheActive() ) { - $ret = apc_store( $pCacheKey, $this, 3600 ); + private function storeInCache() { + $ret = FALSE; + if( $this->isCacheableObject() && static::isCacheActive() && ($cacheKey = $this->getCacheUuid()) ) { + $ret = apc_store( $cacheKey, $this, 3600 ); } + return $ret; } - protected static function loadFromCache( $pCacheKey ) { + public static function loadFromCache( $pCacheKey ) { $ret = NULL; - if( static::isCacheActive() && static::isCacheable() && !empty( $pCacheKey ) ) { - if( $ret = apc_fetch( $pCacheKey ) ) { + if( static::isCacheActive() && static::isCacheableClass() && !empty( $pCacheKey ) ) { + if( $ret = apc_fetch( static::getCacheUuidFromKey( $pCacheKey ) ) ) { global $gBitDb; $ret->setDatabase( $gBitDb ); } @@ -142,9 +151,14 @@ abstract class BitBase { return $ret; } - public function getCacheKey( $pCacheKeyUuid = '' ) { + public function getCacheUuid() { + return static::getCacheUuidFromKey( $this->getCacheKey() ); + } + + public static function getCacheUuidFromKey( $pCacheUuid = '' ) { global $gBitDbName, $gBitDbHost; - return $gBitDbName.'@'.$gBitDbHost.':'.get_called_class().$pCacheKeyUuid; + $ret = $gBitDbName.'@'.$gBitDbHost.':'.get_called_class().'#'.$pCacheUuid; + return $ret; } public static function isCacheActive() { @@ -152,12 +166,16 @@ abstract class BitBase { return function_exists( 'apc_add' ); } - public static function isCacheable() { + public function isCacheableObject() { + return method_exists( $this, 'getCacheKey' ); + } + + public static function isCacheableClass() { return false; } public function isCached() { - return apc_exists( $this->getCacheKey() ); + return apc_exists( $this->getCacheUuid() ); } final public static function getClass() { diff --git a/BitSingleton.php b/BitSingleton.php index 4a53548..da41bb2 100644 --- a/BitSingleton.php +++ b/BitSingleton.php @@ -25,7 +25,7 @@ require_once( KERNEL_PKG_PATH . 'BitBase.php' ); /** * @package kernel */ -abstract class BitSingleton extends BitBase { +abstract class BitSingleton extends BitBase implements BitCacheable { protected static $singletons = null; @@ -38,7 +38,7 @@ abstract class BitSingleton extends BitBase { $globalVarName = !empty( $pVarName ) ? $pVarName : 'g'.$class; global $$globalVarName; - if( !($$globalVarName = static::loadFromCache( $class::getCacheKey() )) ) { + if( !($$globalVarName = static::loadFromCache( 'Singleton' )) ) { $$globalVarName = new $class; } if(!isset(static::$singletons[$globalVarName])) { @@ -50,10 +50,14 @@ abstract class BitSingleton extends BitBase { } public function getCacheKey() { - return parent::getCacheKey().'#Singleton'; + return 'Singleton'; } - public static function isCacheable() { + public static function isCacheableClass() { + return true; + } + + public function isCacheableObject() { return true; } diff --git a/BitSystem.php b/BitSystem.php index 8e5436d..4775d9d 100644 --- a/BitSystem.php +++ b/BitSystem.php @@ -137,7 +137,7 @@ class BitSystem extends BitSingleton { } } - protected static function loadFromCache( $pCacheKey ) { + public static function loadFromCache( $pCacheKey ) { if( $ret = parent::loadFromCache( $pCacheKey ) ) { $ret->setHttpStatus( HttpStatusCodes::HTTP_OK ); $ret->mTimer->start(); |
