3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * Plugin to implement cache interface with disk files
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * A plugin to cache data on local disk
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/
48 class DiskCachePlugin extends Plugin
52 function keyToFilename($key)
54 return $this->root . '/' . str_replace(':', '/', $key);
58 * Get a value associated with a key
60 * The value should have been set previously.
62 * @param string &$key in; Lookup key
63 * @param mixed &$value out; value associated with key
65 * @return boolean hook success
68 function onStartCacheGet(&$key, &$value)
70 $filename = $this->keyToFilename($key);
72 if (file_exists($filename)) {
73 $data = file_get_contents($filename);
74 if ($data !== false) {
75 $value = unserialize($data);
79 Event::handle('EndCacheGet', array($key, &$value));
84 * Associate a value with a key
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
92 * @return boolean hook success
95 function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
97 $filename = $this->keyToFilename($key);
98 $parent = dirname($filename);
102 foreach (explode('/', $parent) as $part) {
106 $sofar .= '/' . $part;
107 if (!is_dir($sofar)) {
108 $this->debug("Creating new directory '$sofar'");
109 $success = mkdir($sofar, 0750);
111 $this->log(LOG_ERR, "Can't create directory '$sofar'");
117 if (is_dir($filename)) {
122 // Write to a temp file and move to destination
124 $tempname = tempnam(null, 'statusnetdiskcache');
126 $result = file_put_contents($tempname, serialize($value));
128 if ($result === false) {
129 $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
133 $result = rename($tempname, $filename);
136 $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
141 Event::handle('EndCacheSet', array($key, $value, $flag,
148 * Delete a value associated with a key
150 * @param string &$key in; Key to lookup
151 * @param boolean &$success out; whether it worked
153 * @return boolean hook success
156 function onStartCacheDelete(&$key, &$success)
158 $filename = $this->keyToFilename($key);
160 if (file_exists($filename) && !is_dir($filename)) {
164 Event::handle('EndCacheDelete', array($key));
168 function onPluginVersion(&$versions)
170 $versions[] = array('name' => 'DiskCache',
171 'version' => STATUSNET_VERSION,
172 'author' => 'Evan Prodromou',
173 'homepage' => 'http://status.net/wiki/Plugin:DiskCache',
175 _m('Plugin to implement cache interface with disk files.'));