DiskCachePlugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return false;
  73. }
  74. /**
  75. * Associate a value with a key
  76. *
  77. * @param string &$key in; Key to use for lookups
  78. * @param mixed &$value in; Value to associate
  79. * @param integer &$flag in; Flag (passed through to Memcache)
  80. * @param integer &$expiry in; Expiry (passed through to Memcache)
  81. * @param boolean &$success out; Whether the set was successful
  82. *
  83. * @return boolean hook success
  84. */
  85. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  86. {
  87. $filename = $this->keyToFilename($key);
  88. $parent = dirname($filename);
  89. $sofar = '';
  90. foreach (explode('/', $parent) as $part) {
  91. if (empty($part)) {
  92. continue;
  93. }
  94. $sofar .= '/' . $part;
  95. if (!is_dir($sofar)) {
  96. $this->debug("Creating new directory '$sofar'");
  97. $success = mkdir($sofar, 0750);
  98. if (!$success) {
  99. $this->log(LOG_ERR, "Can't create directory '$sofar'");
  100. return false;
  101. }
  102. }
  103. }
  104. if (is_dir($filename)) {
  105. $success = false;
  106. return false;
  107. }
  108. // Write to a temp file and move to destination
  109. $tempname = tempnam(null, 'statusnetdiskcache');
  110. $result = file_put_contents($tempname, serialize($value));
  111. if ($result === false) {
  112. $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
  113. return false;
  114. }
  115. $result = rename($tempname, $filename);
  116. if (!$result) {
  117. $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
  118. @unlink($tempname);
  119. return false;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Delete a value associated with a key
  125. *
  126. * @param string &$key in; Key to lookup
  127. * @param boolean &$success out; whether it worked
  128. *
  129. * @return boolean hook success
  130. */
  131. function onStartCacheDelete(&$key, &$success)
  132. {
  133. $filename = $this->keyToFilename($key);
  134. if (file_exists($filename) && !is_dir($filename)) {
  135. unlink($filename);
  136. }
  137. return false;
  138. }
  139. public function onPluginVersion(array &$versions): bool
  140. {
  141. $versions[] = array('name' => 'DiskCache',
  142. 'version' => self::PLUGIN_VERSION,
  143. 'author' => 'Evan Prodromou',
  144. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DiskCache',
  145. 'rawdescription' =>
  146. // TRANS: Plugin description.
  147. _m('Plugin to implement cache interface with disk files.'));
  148. return true;
  149. }
  150. }