]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DiskCachePlugin.php
Disk cache plugin
[quix0rs-gnu-social.git] / plugins / DiskCachePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * Plugin to implement cache interface with disk files
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * A plugin to cache data on local disk
39  *
40  * @category  Cache
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2009 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link      http://status.net/
46  */
47
48 class DiskCachePlugin extends Plugin
49 {
50     var $root = '/tmp';
51
52     function keyToFilename($key)
53     {
54         return $this->root . '/' . str_replace(':', '/', $key);
55     }
56
57     /**
58      * Get a value associated with a key
59      *
60      * The value should have been set previously.
61      *
62      * @param string &$key   in; Lookup key
63      * @param mixed  &$value out; value associated with key
64      *
65      * @return boolean hook success
66      */
67
68     function onStartCacheGet(&$key, &$value)
69     {
70         $filename = $this->keyToFilename($key);
71         if (file_exists($filename)) {
72             $this->log(LOG_INFO, "Cache hit on key '$key'");
73             $data = file_get_contents($filename);
74             $value = unserialize($data);
75         } else {
76             $this->log(LOG_INFO, "Cache miss on key '$key'");
77         }
78
79         Event::handle('EndCacheGet', array($key, &$value));
80         return false;
81     }
82
83     /**
84      * Associate a value with a key
85      *
86      * @param string  &$key     in; Key to use for lookups
87      * @param mixed   &$value   in; Value to associate
88      * @param integer &$flag    in; Flag (passed through to Memcache)
89      * @param integer &$expiry  in; Expiry (passed through to Memcache)
90      * @param boolean &$success out; Whether the set was successful
91      *
92      * @return boolean hook success
93      */
94
95     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
96     {
97         $this->log(LOG_INFO, "Setting value for key '$key'");
98
99         $filename = $this->keyToFilename($key);
100         $parent = dirname($filename);
101
102         $sofar = '';
103
104         foreach (explode('/', $parent) as $part) {
105             if (empty($part)) {
106                 continue;
107             }
108             $sofar .= '/' . $part;
109             if (!is_dir($sofar)) {
110                 $this->debug("Creating new directory '$sofar'");
111                 $success = mkdir($sofar, 0750);
112                 if (!$success) {
113                     $this->log(LOG_ERR, "Can't create directory '$sofar'");
114                     return false;
115                 }
116             }
117         }
118
119         if (is_dir($filename)) {
120             $success = false;
121             return false;
122         }
123
124         file_put_contents($filename, serialize($value));
125
126         Event::handle('EndCacheSet', array($key, $value, $flag,
127                                            $expiry));
128
129         return false;
130     }
131
132     /**
133      * Delete a value associated with a key
134      *
135      * @param string  &$key     in; Key to lookup
136      * @param boolean &$success out; whether it worked
137      *
138      * @return boolean hook success
139      */
140
141     function onStartCacheDelete(&$key, &$success)
142     {
143         $this->log(LOG_INFO, "Deleting value for key '$key'");
144
145         $filename = $this->keyToFilename($key);
146
147         if (file_exists($filename) && !is_dir($filename)) {
148             unlink($filename);
149         }
150
151         Event::handle('EndCacheDelete', array($key));
152         return false;
153     }
154 }
155