]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Memcached_DataObject.php
lost paren in Notice
[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                 if ($result) {
50                         $this->encache();
51                 }
52                 return $result;
53         }
54         
55         function update($orig=NULL) {
56                 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
57                         $orig->decache(); # might be different keys
58                 }
59                 $result = parent::update($orig);
60                 if ($result) {
61                         $this->encache();
62                 }
63                 return $result;
64         }
65         
66         function delete() {
67                 $this->decache(); # while we still have the values!
68                 return parent::delete();
69         }
70         
71         static function memcache() {
72                 return common_memcache();
73         }
74         
75         static function cacheKey($cls, $k, $v) {
76                 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
77         }
78         
79         static function getcached($cls, $k, $v) {
80                 $c = Memcached_DataObject::memcache();
81                 if (!$c) {
82                         return false;
83                 } else {
84                         return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
85                 }
86         }
87
88         function keyTypes() {
89                 global $_DB_DATAOBJECT;
90         if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
91                         $this->databaseStructure();
92
93         }
94                 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
95         }
96         
97         function encache() {
98                 $c = $this->memcache();
99                 if (!$c) {
100                         return false;
101                 } else {
102                         $primary = array();
103                         $types = $this->keyTypes();
104                         ksort($types);
105                         foreach ($types as $key => $type) {
106                                 if ($type == 'K') {
107                                         $primary[] = $key;
108                                 } else {
109                                         $v = $this->$key;
110                                         if (!is_null($v)) {
111                                                 $c->set($this->cacheKey($this->tableName(), $key, $v),
112                                                                 $this);
113                                         }
114                                 }
115                         }
116                         # XXX: figure out what to do with compound pkeys
117                         if (count($primary) == 1) {
118                                 $key = $primary[0];
119                                 $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
120                                                 $this);
121                         }
122                 }
123         }
124         
125         function decache() {
126                 $c = $this->memcache();
127                 if (!$c) {
128                         return false;
129                 } else {
130                         $primary = array();
131                         $types = $this->keyTypes();
132                         ksort($types);
133                         foreach ($types as $key => $type) {
134                                 if ($type == 'K') {
135                                         $primary[] = $this->$key;
136                                 } else {
137                                         $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
138                                 }
139                         }
140                         # XXX: figure out what to do with compound pkeys
141                         if (count($primary) == 1) {
142                                 $key = $primary[0];
143                                 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
144                         }
145                 }
146         }
147 }