]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Memcached_DataObject.php
fix nudge typo and add emailnotifynudge to laconica.ini
[quix0rs-gnu-social.git] / classes / Memcached_DataObject.php
index 2c44b734af80ae5f97f6454306662502fc17bc02..f8e1b9accc6871495a2197680f24087ac3d42483 100644 (file)
@@ -23,35 +23,59 @@ require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
 class Memcached_DataObject extends DB_DataObject 
 {
-    static function &staticGet($cls, $k, $v=NULL) {
-               $i = $this->getcached($cls, $k, $v);
-               if (!is_null($i)) {
+    function &staticGet($cls, $k, $v=NULL) {
+               if (is_null($v)) {
+                       $v = $k;
+                       # XXX: HACK!
+                       $i = new $cls;
+                       $keys = $i->keys();
+                       $k = $keys[0];
+                       unset($i);
+               }
+               $i = Memcached_DataObject::getcached($cls, $k, $v);
+               if ($i) {
                        return $i;
                } else {
                        $i = DB_DataObject::staticGet($cls, $k, $v);
-                       if (!is_null($i)) {
+                       if ($i) {
                                $i->encache();
                        }
                        return $i;
                }
        }
-       
+
+       function &pkeyGet($cls, $kv) {
+               $i = Memcached_DataObject::multicache($cls, $kv);
+               if ($i) {
+                       return $i;
+               } else {
+                       $i = new $cls();
+                       foreach ($kv as $k => $v) {
+                               $i->$k = $v;
+                       }
+                       if ($i->find(true)) {
+                               $i->encache();
+                               return $i;
+                       } else {
+                               return NULL;
+                       }
+               }
+       }
+
        function insert() {
                $result = parent::insert();
-               if ($result) {
-                       $this->encache();
-               }
                return $result;
        }
        
        function update($orig=NULL) {
-               if (!is_null($orig)) {
+               if (is_object($orig) && $orig instanceof Memcached_DataObject) {
                        $orig->decache(); # might be different keys
                }
                $result = parent::update($orig);
                if ($result) {
                        $this->encache();
                }
+               return $result;
        }
        
        function delete() {
@@ -60,20 +84,11 @@ class Memcached_DataObject extends DB_DataObject
        }
        
        static function memcache() {
-               if (!common_config('memcached', 'enabled')) {
-                       return NULL;
-               } else {
-                       $cache = new Memcache();
-                       $res = $cache->connect(common_config('memcached', 'server'), 
-                                                                  common_config('memcached', 'port'));
-                       return ($res) ? $cache : NULL;
-               }
+               return common_memcache();
        }
        
        static function cacheKey($cls, $k, $v) {
-               return common_cache_key(strtolower($cls) . ':' .
-                                                               $k . ':' .
-                                                               $v);
+               return common_cache_key(strtolower($cls).':'.$k.':'.$v);
        }
        
        static function getcached($cls, $k, $v) {
@@ -99,22 +114,22 @@ class Memcached_DataObject extends DB_DataObject
                if (!$c) {
                        return false;
                } else {
-                       $primary = array();
-                       $types = ksort($this->keyTypes());
+                       $pkey = array();
+                       $pval = array();                        
+                       $types = $this->keyTypes();
+                       ksort($types);
                        foreach ($types as $key => $type) {
                                if ($type == 'K') {
-                                       $primary[] = $key;
+                                       $pkey[] = $key;
+                                       $pval[] = $this->$key;
                                } else {
-                                       $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
-                                                       $this);
+                                       $c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
                                }
                        }
-                       # XXX: figure out what to do with compound pkeys
-                       if (count($primary) == 1) {
-                               $key = $primary[0];
-                               $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
-                                               $this);
-                       }
+                       # XXX: should work for both compound and scalar pkeys
+                       $pvals = implode(',', $pval);
+                       $pkeys = implode(',', $pkey);
+                       $c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
                }
        }
        
@@ -123,22 +138,35 @@ class Memcached_DataObject extends DB_DataObject
                if (!$c) {
                        return false;
                } else {
-                       $primary = array();
-                       $types = ksort($this->keyTypes());
+                       $pkey = array();
+                       $pval = array();                        
+                       $types = $this->keyTypes();
+                       ksort($types);
                        foreach ($types as $key => $type) {
                                if ($type == 'K') {
-                                       $primary[] = $this->$key;
+                                       $pkey[] = $key;
+                                       $pval[] = $this->$key;
                                } else {
-                                       $c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
-                                                          $this);
+                                       $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
                                }
                        }
-                       # XXX: figure out what to do with compound pkeys
-                       if (count($primary) == 1) {
-                               $key = $primary[0];
-                               $c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
-                                                  $this);
-                       }
+                       # should work for both compound and scalar pkeys
+                       # XXX: comma works for now but may not be safe separator for future keys
+                       $pvals = implode(',', $pval);
+                       $pkeys = implode(',', $pkey);
+                       $c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
+               }
+       }
+
+       function multicache($cls, $kv) {
+               ksort($kv);
+               $c = Memcached_DataObject::memcache();
+               if (!$c) {
+                       return false;
+               } else {
+                       $pkeys = implode(',', array_keys($kv));
+                       $pvals = implode(',', array_values($kv));
+                       return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
                }
        }
 }