cachingnoticestream.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * A stream of notices
  18. *
  19. * @category Stream
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Class for notice streams
  28. *
  29. * @category Stream
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2011 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class CachingNoticeStream extends NoticeStream
  36. {
  37. public $widgetOpts;
  38. public $scoped;
  39. const CACHE_WINDOW = 200;
  40. public $stream = null;
  41. public $cachekey = null;
  42. public $useLast = true;
  43. public $alwaysCheck = true;
  44. public function __construct(
  45. NoticeStream $stream,
  46. string $cachekey,
  47. bool $useLast = true,
  48. bool $alwaysCheck = false
  49. ) {
  50. $this->stream = $stream;
  51. $this->cachekey = $cachekey;
  52. $this->useLast = $useLast;
  53. $this->alwaysCheck = $alwaysCheck;
  54. }
  55. private function getCacheNoticeIds(
  56. Cache $cache,
  57. string $idkey,
  58. bool $check = false
  59. ): ?array {
  60. $id_str = $cache->get($idkey);
  61. if ($id_str === false) {
  62. return null;
  63. }
  64. $ids = explode(',', $id_str);
  65. if ($check) {
  66. $latest_id = $ids[0];
  67. $new_ids = $this->stream->getNoticeIds(
  68. 0,
  69. self::CACHE_WINDOW,
  70. $latest_id,
  71. null
  72. );
  73. $ids = array_merge($new_ids, $ids);
  74. $ids = array_slice($ids, 0, self::CACHE_WINDOW);
  75. $new_id_str = implode(',', $ids);
  76. if ($id_str !== $new_id_str) {
  77. $cache->set($idkey, $new_id_str);
  78. }
  79. }
  80. return $ids;
  81. }
  82. public function getNoticeIds($offset, $limit, $sinceId, $maxId)
  83. {
  84. $cache = Cache::instance();
  85. // We cache self::CACHE_WINDOW elements at the tip of the stream.
  86. // If the cache won't be hit, just generate directly.
  87. if (empty($cache) ||
  88. $sinceId != 0 || $maxId != 0 ||
  89. is_null($limit) ||
  90. ($offset + $limit) > self::CACHE_WINDOW) {
  91. return $this->stream->getNoticeIds($offset, $limit, $sinceId, $maxId);
  92. }
  93. // Check the cache to see if we have the stream.
  94. $idkey = Cache::key($this->cachekey);
  95. $ids = $this->getCacheNoticeIds($cache, $idkey, $this->alwaysCheck);
  96. if (!is_null($ids)) {
  97. // Cache hit! Woohoo!
  98. return array_slice($ids, $offset, $limit);
  99. }
  100. if ($this->useLast) {
  101. // Check the cache to see if we have a "last-known-good" version.
  102. // The actual cache gets blown away when new notices are added, but
  103. // the "last" value holds a lot of info. We might need to only generate
  104. // a few at the "tip", which can bound our queries and save lots
  105. // of time.
  106. $ids = $this->getCacheNoticeIds($cache, $idkey . ';last', true);
  107. if (!is_null($ids)) {
  108. // Set the actual cache value as well
  109. $id_str = implode(',', $ids);
  110. $cache->set($idkey, $id_str);
  111. return array_slice($ids, $offset, $limit);
  112. }
  113. }
  114. // No cache hits :( Generate directly and stick the results
  115. // into the cache. Note we generate the full cache window.
  116. $window = $this->stream->getNoticeIds(0, self::CACHE_WINDOW, null, null);
  117. $windowstr = implode(',', $window);
  118. $cache->set($idkey, $windowstr);
  119. if ($this->useLast) {
  120. $cache->set($idkey . ';last', $windowstr);
  121. }
  122. // Return just the slice that was requested
  123. return array_slice($window, $offset, $limit);
  124. }
  125. }