Back to index

awl  0.53
Public Member Functions | Private Member Functions | Static Private Attributes
AwlCache Class Reference

A simple Memcached wrapper supporting namespacing of stored values. More...

List of all members.

Public Member Functions

 __construct ()
 Initialise the cache connection.
 isActive ()
 So we can find out if we are actually using the cache.
 get ($namespace, $key)
 get a value from the specified namespace / key
 set ($namespace, $key, $value, $expiry=864000)
 Set a value for the specified namespace/key, perhaps with an expiry (default 10 days)
 delete ($namespace, $key)
 Delete a value from a namespace/key, or for everything in a namespace if a 'null' key is supplied.
 flush ()
 Flush the entire cache.
 acquireLock ($something, $wait_for=5)
 Acquire a lock on something.
 releaseLock ($something)
 Release a lock.

Private Member Functions

 nskey ($namespace, $key)
 Construct a string from the namespace & key.

Static Private Attributes

static $m
static $servers
static $working

Detailed Description

A simple Memcached wrapper supporting namespacing of stored values.

Author:
Andrew McMillan LGPL v2 or later

Definition at line 10 of file AwlCache.php.


Constructor & Destructor Documentation

Initialise the cache connection.

We use getpid() to give us a persistent connection.

Definition at line 18 of file AwlCache.php.

                         {
    global $c;

    if ( isset(self::$working) ) return;

    self::$working = false;
    if ( isset($c->memcache_servers) && class_exists('Memcached') ) {
      dbg_error_log('Cache', 'Using Memcached interface connection');
      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]) )
          self::$m->addServer($server[0],$server[1],$server[2]);
        else
          self::$m->addServer($server[0],$server[1]);
      }
      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();
    }
    else {
      dbg_error_log('Cache', 'Using NoCache dummy interface');
    }
  }

Here is the call graph for this function:


Member Function Documentation

AwlCache::acquireLock ( something,
wait_for = 5 
)

Acquire a lock on something.

Definition at line 147 of file AwlCache.php.

                                                    {
    if ( !self::$working ) return $something;
    $wait_until = time() + $wait_for;
    while( self::$m->add('_lock_'+$something,1,5) === false && time() < $wait_until ) {
      usleep(10000);
    }
    return $something;
  }
AwlCache::delete ( namespace,
key 
)

Delete a value from a namespace/key, or for everything in a namespace if a 'null' key is supplied.

Parameters:
$namespace
$key

Definition at line 116 of file AwlCache.php.

                                      {
    if ( !self::$working ) return false;
    $nskey = self::nskey($namespace,$key);
    dbg_error_log('Cache', 'Deleting from cache key "'.$nskey.'"');
    if ( isset($key) ) {
      self::$m->delete( $nskey );
    }
    else {
      $keylist = self::$m->get( $nskey, null, $cas_token );
      if ( isset($keylist) ) {
      self::$m->delete( $nskey );
        if ( is_array($keylist) ) {
          foreach( $keylist AS $k => $v ) self::$m->delete( $k );
        } 
      }
    }
  }

Here is the call graph for this function:

Flush the entire cache.

Definition at line 137 of file AwlCache.php.

                    {
    if ( !self::$working ) return false;
    dbg_error_log('Cache', 'Flushing cache');
    self::$m->flush();
  }

Here is the caller graph for this function:

AwlCache::get ( namespace,
key 
)

get a value from the specified namespace / key

Parameters:
$namespace
$key

Definition at line 66 of file AwlCache.php.

                                   {
    if ( !self::$working ) return false;
    $ourkey = self::nskey($namespace,$key);
    $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;
  }

Here is the call graph for this function:

So we can find out if we are actually using the cache.

Definition at line 48 of file AwlCache.php.

                      {
    return self::$working;
  }
AwlCache::nskey ( namespace,
key 
) [private]

Construct a string from the namespace & key.

Parameters:
unknown_type$namespace
unknown_type$key

Definition at line 57 of file AwlCache.php.

                                             {
    return str_replace(' ', '%20', $namespace . (isset($key) ? '~~' . $key: '')); // for now.
  }

Here is the caller graph for this function:

AwlCache::releaseLock ( something)

Release a lock.

Definition at line 160 of file AwlCache.php.

                                     {
    if ( !self::$working ) return;
    self::$m->delete('_lock_'+$something);        
  }
AwlCache::set ( namespace,
key,
value,
expiry = 864000 
)

Set a value for the specified namespace/key, perhaps with an expiry (default 10 days)

Parameters:
$namespace
$key
$value
$expiry

Definition at line 82 of file AwlCache.php.

                                                           {
    if ( !self::$working ) return false;
    $ourkey = self::nskey($namespace,$key);
    $nskey = self::nskey($namespace,null);
    $keylist = self::$m->get( $nskey, null, $cas_token );
    if ( isset($keylist) && is_array($keylist) ) {
      if ( !isset($keylist[$ourkey]) ) {
        $keylist[$ourkey] = 1;
        $success = self::$m->cas( $cas_token, $nskey, $keylist );
        $i=0;
        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 = self::$m->cas( $cas_token, $nskey, $keylist );
        }
        if ( !$success ) return false;
      }
    } 
    else {
      $keylist = array( $ourkey => 1 );      
      self::$m->set( $nskey, $keylist );
    }
//    var_dump($value);
//    dbg_error_log('Cache', 'Setting value for cache key "'.$ourkey.'" - '.strlen(serialize($value)).' bytes');
    return self::$m->set( $ourkey, $value, $expiry );
  }

Here is the call graph for this function:


Member Data Documentation

AwlCache::$m [static, private]

Definition at line 11 of file AwlCache.php.

AwlCache::$servers [static, private]

Definition at line 12 of file AwlCache.php.

AwlCache::$working [static, private]

Definition at line 13 of file AwlCache.php.


The documentation for this class was generated from the following file: