DiskCachePlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * A plugin to cache data on local disk
  37. *
  38. * @category Cache
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2009 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class DiskCachePlugin extends Plugin
  46. {
  47. const PLUGIN_VERSION = '2.0.0';
  48. var $root = '/tmp';
  49. function keyToFilename($key)
  50. {
  51. return $this->root . '/' . str_replace(':', '/', $key);
  52. }
  53. /**
  54. * Get a value associated with a key
  55. *
  56. * The value should have been set previously.
  57. *
  58. * @param string &$key in; Lookup key
  59. * @param mixed &$value out; value associated with key
  60. *
  61. * @return boolean hook success
  62. */
  63. function onStartCacheGet(&$key, &$value)
  64. {
  65. $filename = $this->keyToFilename($key);
  66. if (file_exists($filename)) {
  67. $data = file_get_contents($filename);
  68. if ($data !== false) {
  69. $value = unserialize($data);
  70. }
  71. }
  72. Event::handle('EndCacheGet', array($key, &$value));
  73. return false;
  74. }
  75. /**
  76. * Associate a value with a key
  77. *
  78. * @param string &$key in; Key to use for lookups
  79. * @param mixed &$value in; Value to associate
  80. * @param integer &$flag in; Flag (passed through to Memcache)
  81. * @param integer &$expiry in; Expiry (passed through to Memcache)
  82. * @param boolean &$success out; Whether the set was successful
  83. *
  84. * @return boolean hook success
  85. */
  86. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  87. {
  88. $filename = $this->keyToFilename($key);
  89. $parent = dirname($filename);
  90. $sofar = '';
  91. foreach (explode('/', $parent) as $part) {
  92. if (empty($part)) {
  93. continue;
  94. }
  95. $sofar .= '/' . $part;
  96. if (!is_dir($sofar)) {
  97. $this->debug("Creating new directory '$sofar'");
  98. $success = mkdir($sofar, 0750);
  99. if (!$success) {
  100. $this->log(LOG_ERR, "Can't create directory '$sofar'");
  101. return false;
  102. }
  103. }
  104. }
  105. if (is_dir($filename)) {
  106. $success = false;
  107. return false;
  108. }
  109. // Write to a temp file and move to destination
  110. $tempname = tempnam(null, 'statusnetdiskcache');
  111. $result = file_put_contents($tempname, serialize($value));
  112. if ($result === false) {
  113. $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
  114. return false;
  115. }
  116. $result = rename($tempname, $filename);
  117. if (!$result) {
  118. $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
  119. @unlink($tempname);
  120. return false;
  121. }
  122. Event::handle('EndCacheSet', array($key, $value, $flag,
  123. $expiry));
  124. return false;
  125. }
  126. /**
  127. * Delete a value associated with a key
  128. *
  129. * @param string &$key in; Key to lookup
  130. * @param boolean &$success out; whether it worked
  131. *
  132. * @return boolean hook success
  133. */
  134. function onStartCacheDelete(&$key, &$success)
  135. {
  136. $filename = $this->keyToFilename($key);
  137. if (file_exists($filename) && !is_dir($filename)) {
  138. unlink($filename);
  139. }
  140. Event::handle('EndCacheDelete', array($key));
  141. return false;
  142. }
  143. function onPluginVersion(array &$versions)
  144. {
  145. $versions[] = array('name' => 'DiskCache',
  146. 'version' => self::PLUGIN_VERSION,
  147. 'author' => 'Evan Prodromou',
  148. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DiskCache',
  149. 'rawdescription' =>
  150. // TRANS: Plugin description.
  151. _m('Plugin to implement cache interface with disk files.'));
  152. return true;
  153. }
  154. }