From 766d4360e5649ba63ce2e6599737db1a59e3ce61 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Tue, 4 Jan 2011 22:48:42 +1300 Subject: [PATCH] [AwlCache] Make this mostly static. Signed-off-by: Andrew McMillan --- inc/AwlCache.php | 58 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/inc/AwlCache.php b/inc/AwlCache.php index c69fe33..8ed5c0c 100644 --- a/inc/AwlCache.php +++ b/inc/AwlCache.php @@ -8,9 +8,9 @@ */ class AwlCache { - private $m; - private $servers; - private $working; + private static $m; + private static $servers; + private static $working; /** * Initialise the cache connection. We use getpid() to give us a persistent connection. @@ -18,20 +18,22 @@ class AwlCache { function __construct() { global $c; - $this->working = false; + if ( isset(self::$working) ) return; + + self::$working = false; if ( isset($c->memcache_servers) && class_exists('Memcached') ) { dbg_error_log('Cache', 'Using Memcached interface connection'); - $this->servers = $c->memcache_servers; - $this->m = new Memcached(); - foreach( $this->servers AS $v ) { + self::$servers = $c->memcache_servers; + self::$m = new Memcached(); + foreach( self::$servers AS $v ) { dbg_error_log('Cache', 'Adding server '.$v); $server = explode(',',$v); if ( isset($server[2]) ) - $this->m->addServer($server[0],$server[1],$server[2]); + self::$m->addServer($server[0],$server[1],$server[2]); else - $this->m->addServer($server[0],$server[1]); + self::$m->addServer($server[0],$server[1]); } - $this->working = true; + self::$working = true; // Hack to allow the regression tests to flush the cache at start if ( isset($_SERVER['HTTP_X_DAVICAL_FLUSH_CACHE'])) $this->flush(); } @@ -44,7 +46,7 @@ class AwlCache { * So we can find out if we are actually using the cache. */ function isActive() { - return $this->working; + return self::$working; } /** @@ -62,9 +64,9 @@ class AwlCache { * @param $key */ function get( $namespace, $key ) { - if ( !$this->working ) return false; + if ( !self::$working ) return false; $ourkey = self::nskey($namespace,$key); - $value = $this->m->get($ourkey); + $value = self::$m->get($ourkey); // var_dump($value); // if ( $value !== false ) dbg_error_log('Cache', 'Got value for cache key "'.$ourkey.'" - '.strlen(serialize($value)).' bytes'); return $value; @@ -78,32 +80,32 @@ class AwlCache { * @param $expiry */ function set( $namespace, $key, $value, $expiry=864000 ) { - if ( !$this->working ) return false; + if ( !self::$working ) return false; $ourkey = self::nskey($namespace,$key); $nskey = self::nskey($namespace,null); - $keylist = $this->m->get( $nskey, null, $cas_token ); + $keylist = self::$m->get( $nskey, null, $cas_token ); if ( isset($keylist) && is_array($keylist) ) { if ( !isset($keylist[$ourkey]) ) { $keylist[$ourkey] = 1; - $success = $this->m->cas( $cas_token, $nskey, $keylist ); + $success = self::$m->cas( $cas_token, $nskey, $keylist ); $i=0; - while( !$success && $i++ < 10 && $this->m->getResultCode() == Memcached::RES_DATA_EXISTS ) { - $keylist = $this->m->get( $nskey, null, $cas_token ); + while( !$success && $i++ < 10 && self::$m->getResultCode() == Memcached::RES_DATA_EXISTS ) { + $keylist = self::$m->get( $nskey, null, $cas_token ); if ( $keylist === false ) return false; if ( isset($keylist[$ourkey]) ) break; $keylist[$ourkey] = 1; - $success = $this->m->cas( $cas_token, $nskey, $keylist ); + $success = self::$m->cas( $cas_token, $nskey, $keylist ); } if ( !$success ) return false; } } else { $keylist = array( $ourkey => 1 ); - $this->m->set( $nskey, $keylist ); + self::$m->set( $nskey, $keylist ); } // var_dump($value); // dbg_error_log('Cache', 'Setting value for cache key "'.$ourkey.'" - '.strlen(serialize($value)).' bytes'); - return $this->m->set( $ourkey, $value, $expiry ); + return self::$m->set( $ourkey, $value, $expiry ); } /** @@ -112,18 +114,18 @@ class AwlCache { * @param $key */ function delete( $namespace, $key ) { - if ( !$this->working ) return false; + if ( !self::$working ) return false; $nskey = self::nskey($namespace,$key); dbg_error_log('Cache', 'Deleting from cache key "'.$nskey.'"'); if ( isset($key) ) { - $this->m->delete( $nskey ); + self::$m->delete( $nskey ); } else { - $keylist = $this->m->get( $nskey, null, $cas_token ); + $keylist = self::$m->get( $nskey, null, $cas_token ); if ( isset($keylist) ) { - $this->m->delete( $nskey ); + self::$m->delete( $nskey ); if ( is_array($keylist) ) { - foreach( $keylist AS $k => $v ) $this->m->delete( $k ); + foreach( $keylist AS $k => $v ) self::$m->delete( $k ); } } } @@ -133,9 +135,9 @@ class AwlCache { * Flush the entire cache */ function flush( ) { - if ( !$this->working ) return false; + if ( !self::$working ) return false; dbg_error_log('Cache', 'Flushing cache'); - $this->m->flush(); + self::$m->flush(); } } -- 2.11.4.GIT