]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Memcached_DataObject.php
Updates to the update_translations script
[quix0rs-gnu-social.git] / classes / Memcached_DataObject.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23
24 class Memcached_DataObject extends DB_DataObject 
25 {
26     function &staticGet($cls, $k, $v=NULL) {
27                 if (is_null($v)) {
28                         $v = $k;
29                         # XXX: HACK!
30                         $i = new $cls;
31                         $keys = $i->keys();
32                         $k = $keys[0];
33                         unset($i);
34                 }
35                 $i = Memcached_DataObject::getcached($cls, $k, $v);
36                 if ($i) {
37                         return $i;
38                 } else {
39                         $i = DB_DataObject::staticGet($cls, $k, $v);
40                         if ($i) {
41                                 $i->encache();
42                         }
43                         return $i;
44                 }
45         }
46         
47         function insert() {
48                 $result = parent::insert();
49                 return $result;
50         }
51         
52         function update($orig=NULL) {
53                 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
54                         $orig->decache(); # might be different keys
55                 }
56                 $result = parent::update($orig);
57                 if ($result) {
58                         $this->encache();
59                 }
60                 return $result;
61         }
62         
63         function delete() {
64                 $this->decache(); # while we still have the values!
65                 return parent::delete();
66         }
67         
68         static function memcache() {
69                 return common_memcache();
70         }
71         
72         static function cacheKey($cls, $k, $v) {
73                 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
74         }
75         
76         static function getcached($cls, $k, $v) {
77                 $c = Memcached_DataObject::memcache();
78                 if (!$c) {
79                         return false;
80                 } else {
81                         return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
82                 }
83         }
84
85         function keyTypes() {
86                 global $_DB_DATAOBJECT;
87         if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
88                         $this->databaseStructure();
89
90         }
91                 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
92         }
93         
94         function encache() {
95                 $c = $this->memcache();
96                 if (!$c) {
97                         return false;
98                 } else {
99                         $primary = array();
100                         $types = $this->keyTypes();
101                         ksort($types);
102                         foreach ($types as $key => $type) {
103                                 if ($type == 'K') {
104                                         $primary[] = $key;
105                                 } else {
106                                         $v = $this->$key;
107                                         if (!is_null($v)) {
108                                                 $c->set($this->cacheKey($this->tableName(), $key, $v),
109                                                                 $this);
110                                         }
111                                 }
112                         }
113                         # XXX: figure out what to do with compound pkeys
114                         if (count($primary) == 1) {
115                                 $key = $primary[0];
116                                 $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
117                                                 $this);
118                         }
119                 }
120         }
121         
122         function decache() {
123                 $c = $this->memcache();
124                 if (!$c) {
125                         return false;
126                 } else {
127                         $primary = array();
128                         $types = $this->keyTypes();
129                         ksort($types);
130                         foreach ($types as $key => $type) {
131                                 if ($type == 'K') {
132                                         $primary[] = $this->$key;
133                                 } else {
134                                         $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
135                                 }
136                         }
137                         # XXX: figure out what to do with compound pkeys
138                         if (count($primary) == 1) {
139                                 $key = $primary[0];
140                                 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
141                         }
142                 }
143         }
144 }