+ if (class_exists('Memcache')) {
+ self::$cache = new Memcache();
+
+ // If we're a parent command-line process we need
+ // to be able to close out the connection after
+ // forking, so disable persistence.
+ //
+ // We'll turn it back on again the second time
+ // through which will either be in a child process,
+ // or a single-process script which is switching
+ // configurations.
+ $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
+ if (is_array($servers)) {
+ foreach($servers as $server) {
+ self::$cache->addServer($server, 11211, $persist);
+ }
+ } else {
+ self::$cache->addServer($servers, 11211, $persist);
+ }
+ self::$cacheInitialized = true;
+ }
+
+ self::$base = $dbname;
+ }
+
+ static function cacheKey($k, $v) {
+ return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
+ }
+
+ static function memGet($k, $v)
+ {
+ if (!self::$cache) {
+ return self::staticGet($k, $v);
+ }
+
+ $ck = self::cacheKey($k, $v);
+
+ $sn = self::$cache->get($ck);
+
+ if (empty($sn)) {
+ $sn = self::staticGet($k, $v);
+ if (!empty($sn)) {
+ self::$cache->set($ck, clone($sn));
+ }
+ }
+
+ return $sn;
+ }
+
+ function decache()
+ {
+ if (self::$cache) {
+ $keys = array('nickname', 'hostname', 'pathname');
+ foreach ($keys as $k) {
+ $ck = self::cacheKey($k, $this->$k);
+ self::$cache->delete($ck);
+ }
+ }
+ }
+
+ function update($orig=null)
+ {
+ if (is_object($orig)) {
+ $orig->decache(); # might be different keys
+ }
+ return parent::update($orig);
+ }
+
+ function delete()
+ {
+ $this->decache(); # while we still have the values!
+ return parent::delete();