123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class DiskCachePlugin extends Plugin
- {
- var $root = '/tmp';
- function keyToFilename($key)
- {
- return $this->root . '/' . str_replace(':', '/', $key);
- }
-
- function onStartCacheGet(&$key, &$value)
- {
- $filename = $this->keyToFilename($key);
- if (file_exists($filename)) {
- $data = file_get_contents($filename);
- if ($data !== false) {
- $value = unserialize($data);
- }
- }
- Event::handle('EndCacheGet', array($key, &$value));
- return false;
- }
-
- function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
- {
- $filename = $this->keyToFilename($key);
- $parent = dirname($filename);
- $sofar = '';
- foreach (explode('/', $parent) as $part) {
- if (empty($part)) {
- continue;
- }
- $sofar .= '/' . $part;
- if (!is_dir($sofar)) {
- $this->debug("Creating new directory '$sofar'");
- $success = mkdir($sofar, 0750);
- if (!$success) {
- $this->log(LOG_ERR, "Can't create directory '$sofar'");
- return false;
- }
- }
- }
- if (is_dir($filename)) {
- $success = false;
- return false;
- }
-
- $tempname = tempnam(null, 'statusnetdiskcache');
- $result = file_put_contents($tempname, serialize($value));
- if ($result === false) {
- $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
- return false;
- }
- $result = rename($tempname, $filename);
- if (!$result) {
- $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
- @unlink($tempname);
- return false;
- }
- Event::handle('EndCacheSet', array($key, $value, $flag,
- $expiry));
- return false;
- }
-
- function onStartCacheDelete(&$key, &$success)
- {
- $filename = $this->keyToFilename($key);
- if (file_exists($filename) && !is_dir($filename)) {
- unlink($filename);
- }
- Event::handle('EndCacheDelete', array($key));
- return false;
- }
- function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'DiskCache',
- 'version' => GNUSOCIAL_VERSION,
- 'author' => 'Evan Prodromou',
- 'homepage' => 'http://status.net/wiki/Plugin:DiskCache',
- 'rawdescription' =>
-
- _m('Plugin to implement cache interface with disk files.'));
- return true;
- }
- }
|