/usr/share/awl/inc/AwlCache.php is in libawl-php 0.56-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <?php
/**
* A simple Memcached wrapper supporting namespacing of stored values.
*
* @author Andrew McMillan
* @license LGPL v2 or later
*/
class AwlCache {
private static $m;
private static $servers;
private static $working;
/**
* Initialise the cache connection. We use getpid() to give us a persistent connection.
*/
function __construct() {
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');
}
}
/**
* So we can find out if we are actually using the cache.
*/
function isActive() {
return self::$working;
}
/**
* Construct a string from the namespace & key
* @param unknown_type $namespace
* @param unknown_type $key
*/
private function nskey( $namespace, $key ) {
return str_replace(' ', '%20', $namespace . (isset($key) ? '~~' . $key: '')); // for now.
}
/**
* get a value from the specified namespace / key
* @param $namespace
* @param $key
*/
function get( $namespace, $key ) {
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;
}
/**
* Set a value for the specified namespace/key, perhaps with an expiry (default 10 days)
* @param $namespace
* @param $key
* @param $value
* @param $expiry
*/
function set( $namespace, $key, $value, $expiry=864000 ) {
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 );
}
/**
* Delete a value from a namespace/key, or for everything in a namespace if a 'null' key is supplied.
* @param $namespace
* @param $key
*/
function delete( $namespace, $key ) {
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 );
}
}
}
}
/**
* Flush the entire cache
*/
function flush( ) {
if ( !self::$working ) return false;
dbg_error_log('Cache', 'Flushing cache');
self::$m->flush();
}
/**
* Acquire a lock on something
*/
function acquireLock( $something, $wait_for = 5 ) {
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;
}
/**
* Release a lock
*/
function releaseLock( $something ) {
if ( !self::$working ) return;
self::$m->delete('_lock_'+$something);
}
}
function getCacheInstance() {
static $ourCacheInstance;
if ( !isset($ourCacheInstance) ) $ourCacheInstance = new AWLCache('Memcached');
return $ourCacheInstance;
}
|