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