3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
24 class Memcached_DataObject extends DB_DataObject
26 function &staticGet($cls, $k, $v=null)
36 $i = Memcached_DataObject::getcached($cls, $k, $v);
40 $i = DB_DataObject::staticGet($cls, $k, $v);
48 function &pkeyGet($cls, $kv)
50 $i = Memcached_DataObject::multicache($cls, $kv);
55 foreach ($kv as $k => $v) {
69 $result = parent::insert();
73 function update($orig=null)
75 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
76 $orig->decache(); # might be different keys
78 $result = parent::update($orig);
87 $this->decache(); # while we still have the values!
88 return parent::delete();
91 static function memcache() {
92 return common_memcache();
95 static function cacheKey($cls, $k, $v) {
96 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
99 static function getcached($cls, $k, $v) {
100 $c = Memcached_DataObject::memcache();
104 return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
110 global $_DB_DATAOBJECT;
111 if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
112 $this->databaseStructure();
115 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
120 $c = $this->memcache();
126 $types = $this->keyTypes();
128 foreach ($types as $key => $type) {
131 $pval[] = $this->$key;
133 $c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
136 # XXX: should work for both compound and scalar pkeys
137 $pvals = implode(',', $pval);
138 $pkeys = implode(',', $pkey);
139 $c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
145 $c = $this->memcache();
151 $types = $this->keyTypes();
153 foreach ($types as $key => $type) {
156 $pval[] = $this->$key;
158 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
161 # should work for both compound and scalar pkeys
162 # XXX: comma works for now but may not be safe separator for future keys
163 $pvals = implode(',', $pval);
164 $pkeys = implode(',', $pkey);
165 $c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
169 function multicache($cls, $kv)
172 $c = Memcached_DataObject::memcache();
176 $pkeys = implode(',', array_keys($kv));
177 $pvals = implode(',', array_values($kv));
178 return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
182 function getSearchEngine($table)
184 require_once INSTALLDIR.'/lib/search_engines.php';
185 static $search_engine;
186 if (!isset($search_engine)) {
188 if (common_config('sphinx', 'enabled')) {
189 $search_engine = new SphinxSearch($this, $table);
190 $connected = $search_engine->is_connected();
193 // unable to connect to sphinx' search daemon
195 if ('mysql' === common_config('db', 'type')) {
196 $search_engine = new MySQLSearch($this, $table);
198 $search_engine = new PGSearch($this, $table);
202 return $search_engine;
205 static function cachedQuery($cls, $qry, $expiry=3600)
207 $c = Memcached_DataObject::memcache();
213 $key_part = common_keyize($cls).':'.md5($qry);
214 $ckey = common_cache_key($key_part);
215 $stored = $c->get($ckey);
217 return new ArrayWrapper($stored);
223 while ($inst->fetch()) {
224 $cached[] = clone($inst);
227 $c->set($ckey, $cached, MEMCACHE_COMPRESSED, $expiry);
228 return new ArrayWrapper($cached);