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) {
35 $i = Memcached_DataObject::getcached($cls, $k, $v);
39 $i = DB_DataObject::staticGet($cls, $k, $v);
48 $result = parent::insert();
55 function update($orig=NULL) {
56 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
57 $orig->decache(); # might be different keys
59 $result = parent::update($orig);
67 $this->decache(); # while we still have the values!
68 return parent::delete();
71 static function memcache() {
72 return common_memcache();
75 static function cacheKey($cls, $k, $v) {
76 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
79 static function getcached($cls, $k, $v) {
80 $c = Memcached_DataObject::memcache();
84 return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
89 global $_DB_DATAOBJECT;
90 if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
91 $this->databaseStructure();
94 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
98 $c = $this->memcache();
103 $types = $this->keyTypes();
105 foreach ($types as $key => $type) {
111 $c->set($this->cacheKey($this->tableName(), $key, $v),
116 # XXX: figure out what to do with compound pkeys
117 if (count($primary) == 1) {
119 $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
126 $c = $this->memcache();
131 $types = $this->keyTypes();
133 foreach ($types as $key => $type) {
135 $primary[] = $this->$key;
137 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
140 # XXX: figure out what to do with compound pkeys
141 if (count($primary) == 1) {
143 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));