]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Memcached_DataObject.php
path correct in require_once for memcached
[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     static function &staticGet($cls, $k, $v=NULL) {
27                 $i = $this->getcached($cls, $k, $v);
28                 if (!is_null($i)) {
29                         return $i;
30                 } else {
31                         $i = parent::staticGet($k, $v);
32                         if (!is_null($i)) {
33                                 $i->encache();
34                         }
35                         return $i;
36                 }
37         }
38         
39         function insert() {
40                 $result = parent::insert();
41                 if ($result) {
42                         $this->encache();
43                 }
44                 return $result;
45         }
46         
47         function update($orig=NULL) {
48                 if (!is_null($orig)) {
49                         $orig->decache(); # might be different keys
50                 }
51                 $result = parent::update($orig);
52                 if ($result) {
53                         $this->encache();
54                 }
55         }
56         
57         function delete() {
58                 $this->decache(); # while we still have the values!
59                 return parent::delete();
60         }
61         
62         static function memcache() {
63                 if (!common_config('memcached', 'enabled')) {
64                         return NULL;
65                 } else {
66                         $cache = new Memcache();
67                         $res = $cache->connect(common_config('memcached', 'server'), 
68                                                                    common_config('memcached', 'port'));
69                         return ($res) ? $cache : NULL;
70                 }
71         }
72         
73         static function cacheKey($cls, $k, $v) {
74                 return common_cache_key(strtolower($cls) . ':' .
75                                                                 $k . ':' .
76                                                                 $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 = ksort($this->keyTypes());
104                         foreach ($types as $key => $type) {
105                                 if ($type == 'K') {
106                                         $primary[] = $key;
107                                 } else {
108                                         $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
109                                                         $this);
110                                 }
111                         }
112                         # XXX: figure out what to do with compound pkeys
113                         if (count($primary) == 1) {
114                                 $key = $primary[0];
115                                 $c->set($this->cacheKey($this->tableName(), $key, $this->$key),
116                                                 $this);
117                         }
118                 }
119         }
120         
121         function decache() {
122                 $c = $this->memcache();
123                 if (!$c) {
124                         return false;
125                 } else {
126                         $primary = array();
127                         $types = ksort($this->keyTypes());
128                         foreach ($types as $key => $type) {
129                                 if ($type == 'K') {
130                                         $primary[] = $this->$key;
131                                 } else {
132                                         $c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
133                                                            $this);
134                                 }
135                         }
136                         # XXX: figure out what to do with compound pkeys
137                         if (count($primary) == 1) {
138                                 $key = $primary[0];
139                                 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
140                                                    $this);
141                         }
142                 }
143         }
144 }