DiskCachePlugin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. var $root = '/tmp';
  48. function keyToFilename($key)
  49. {
  50. return $this->root . '/' . str_replace(':', '/', $key);
  51. }
  52. /**
  53. * Get a value associated with a key
  54. *
  55. * The value should have been set previously.
  56. *
  57. * @param string &$key in; Lookup key
  58. * @param mixed &$value out; value associated with key
  59. *
  60. * @return boolean hook success
  61. */
  62. function onStartCacheGet(&$key, &$value)
  63. {
  64. $filename = $this->keyToFilename($key);
  65. if (file_exists($filename)) {
  66. $data = file_get_contents($filename);
  67. if ($data !== false) {
  68. $value = unserialize($data);
  69. }
  70. }
  71. Event::handle('EndCacheGet', array($key, &$value));
  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. Event::handle('EndCacheSet', array($key, $value, $flag,
  122. $expiry));
  123. return false;
  124. }
  125. /**
  126. * Delete a value associated with a key
  127. *
  128. * @param string &$key in; Key to lookup
  129. * @param boolean &$success out; whether it worked
  130. *
  131. * @return boolean hook success
  132. */
  133. function onStartCacheDelete(&$key, &$success)
  134. {
  135. $filename = $this->keyToFilename($key);
  136. if (file_exists($filename) && !is_dir($filename)) {
  137. unlink($filename);
  138. }
  139. Event::handle('EndCacheDelete', array($key));
  140. return false;
  141. }
  142. function onPluginVersion(array &$versions)
  143. {
  144. $versions[] = array('name' => 'DiskCache',
  145. 'version' => GNUSOCIAL_VERSION,
  146. 'author' => 'Evan Prodromou',
  147. 'homepage' => 'http://status.net/wiki/Plugin:DiskCache',
  148. 'rawdescription' =>
  149. // TRANS: Plugin description.
  150. _m('Plugin to implement cache interface with disk files.'));
  151. return true;
  152. }
  153. }