RedisCachePlugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * GNU social - a federating social network
  4. *
  5. * Plguin inplementing Redis based caching
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Files
  21. * @package GNUsocial
  22. * @author Stéphane Bérubé <chimo@chromic.org>
  23. * @author Miguel Dantas <biodantas@gmail.com>
  24. * @copyright 2018, 2019 Free Software Foundation http://fsf.org
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link https://www.gnu.org/software/social/
  27. */
  28. defined('GNUSOCIAL') || die();
  29. use Predis\Client;
  30. use Predis\PredisException;
  31. class RedisCachePlugin extends Plugin
  32. {
  33. const PLUGIN_VERSION = '0.0.1';
  34. // settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
  35. public $server = null;
  36. public $defaultExpiry = 86400; // 24h
  37. protected $client = null;
  38. function onInitializePlugin()
  39. {
  40. $this->_ensureConn();
  41. return true;
  42. }
  43. private function _ensureConn()
  44. {
  45. if ($this->client === null) {
  46. $this->client = new Client($this->server);
  47. }
  48. }
  49. function onStartCacheGet(&$key, &$value)
  50. {
  51. try {
  52. $this->_ensureConn();
  53. $ret = $this->client->get($key);
  54. } catch(PredisException $e) {
  55. common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
  56. return true;
  57. }
  58. // Hit, overwrite "value" and return false
  59. // to indicate we took care of this
  60. if ($ret !== null) {
  61. $value = unserialize($ret);
  62. return false;
  63. }
  64. // Miss, let GS do its thing
  65. return true;
  66. }
  67. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  68. {
  69. if ($expiry === null) {
  70. $expiry = $this->defaultExpiry;
  71. }
  72. try {
  73. $this->_ensureConn();
  74. $ret = $this->client->setex($key, $expiry, serialize($value));
  75. } catch(PredisException $e) {
  76. common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
  77. return true;
  78. }
  79. if ($ret->getPayload() === "OK") {
  80. $success = true;
  81. return false;
  82. }
  83. return true;
  84. }
  85. function onStartCacheDelete($key)
  86. {
  87. if ($key === null) {
  88. return true;
  89. }
  90. try {
  91. $this->_ensureConn();
  92. $ret = $this->client->del($key);
  93. } catch(PredisException $e) {
  94. common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
  95. }
  96. // Let other caches delete stuff if we didn't succeed
  97. return isset($ret) && $ret === 1;
  98. }
  99. function onStartCacheIncrement(&$key, &$step, &$value)
  100. {
  101. try {
  102. $this->_ensureConn();
  103. $this->client->incrby($key, $step);
  104. } catch(PredisException $e) {
  105. common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
  106. return true;
  107. }
  108. return false;
  109. }
  110. public function onPluginVersion(array &$versions): bool
  111. {
  112. $versions[] = array('name' => 'RedisCache',
  113. 'version' => self::VERSION,
  114. 'author' => 'chimo',
  115. 'homepage' => 'https://github.com/chimo/gs-rediscache',
  116. 'description' =>
  117. // TRANS: Plugin description.
  118. _m('Plugin implementing Redis as a backend for GNU social caching'));
  119. return true;
  120. }
  121. }