]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Memcached_DataObject.php
PostgreSQL - added the rest of the recently added tables for blocking, notice inboxes...
[quix0rs-gnu-social.git] / classes / Memcached_DataObject.php
index 84529ce9a6e7c5eb43dd45d3e293ff8c86f91b0d..7a33e158dd1e2dffa9ee0f4300b11bf5704a7237 100644 (file)
@@ -43,17 +43,32 @@ class Memcached_DataObject extends DB_DataObject
                        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();
+                       } else {
+                               $i = NULL;
+                       }
+            return $i;
+               }
+       }
+
        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);
@@ -99,26 +114,22 @@ class Memcached_DataObject extends DB_DataObject
                if (!$c) {
                        return false;
                } else {
-                       $primary = array();
+                       $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 {
-                                       $v = $this->$key;
-                                       if (!is_null($v)) {
-                                               $c->set($this->cacheKey($this->tableName(), $key, $v),
-                                                               $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);
                }
        }
        
@@ -127,21 +138,57 @@ class Memcached_DataObject extends DB_DataObject
                if (!$c) {
                        return false;
                } else {
-                       $primary = array();
+                       $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));
                                }
                        }
-                       # 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));
-                       }
+                       # 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));
+               }
+       }
+
+    function getSearchEngine($table) {
+        require_once INSTALLDIR.'/lib/search_engines.php';
+        static $search_engine;
+        if (!isset($search_engine)) {
+                $connected = false;
+                if (common_config('sphinx', 'enabled')) {
+                    $search_engine = new SphinxSearch($this, $table);
+                    $connected = $search_engine->is_connected();
+                }
+
+                // unable to connect to sphinx' search daemon
+                if (!$connected) {
+                    if ('mysql' === common_config('db', 'type')) {
+                        $search_engine = new MySQLSearch($this, $table);
+                    } else {
+                        $search_engine = new PGSearch($this, $table);
+                    }
+                }
+        }
+        return $search_engine;
+    }
 }