cachingnoticestream.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. const CACHE_WINDOW = 200;
  38. public $stream = null;
  39. public $cachekey = null;
  40. public $useLast = true;
  41. public $alwaysCheck = true;
  42. public function __construct(
  43. NoticeStream $stream,
  44. string $cachekey,
  45. bool $useLast = true,
  46. bool $alwaysCheck = false
  47. ) {
  48. $this->stream = $stream;
  49. $this->cachekey = $cachekey;
  50. $this->useLast = $useLast;
  51. $this->alwaysCheck = $alwaysCheck;
  52. }
  53. private function getCacheNoticeIds(
  54. Cache $cache,
  55. string $idkey,
  56. bool $check = false
  57. ): ?array {
  58. $id_str = $cache->get($idkey);
  59. if ($id_str === false) {
  60. return null;
  61. }
  62. $ids = explode(',', $id_str);
  63. if ($check) {
  64. $latest_id = $ids[0];
  65. $new_ids = $this->stream->getNoticeIds(
  66. 0,
  67. self::CACHE_WINDOW,
  68. $latest_id,
  69. null
  70. );
  71. $ids = array_merge($new_ids, $ids);
  72. $ids = array_slice($ids, 0, self::CACHE_WINDOW);
  73. $new_id_str = implode(',', $ids);
  74. if ($id_str !== $new_id_str) {
  75. $cache->set($idkey, $new_id_str);
  76. }
  77. }
  78. return $ids;
  79. }
  80. public function getNoticeIds($offset, $limit, $sinceId, $maxId)
  81. {
  82. $cache = Cache::instance();
  83. // We cache self::CACHE_WINDOW elements at the tip of the stream.
  84. // If the cache won't be hit, just generate directly.
  85. if (empty($cache) ||
  86. $sinceId != 0 || $maxId != 0 ||
  87. is_null($limit) ||
  88. ($offset + $limit) > self::CACHE_WINDOW) {
  89. return $this->stream->getNoticeIds($offset, $limit, $sinceId, $maxId);
  90. }
  91. // Check the cache to see if we have the stream.
  92. $idkey = Cache::key($this->cachekey);
  93. $ids = $this->getCacheNoticeIds($cache, $idkey, $this->alwaysCheck);
  94. if (!is_null($ids)) {
  95. // Cache hit! Woohoo!
  96. return array_slice($ids, $offset, $limit);
  97. }
  98. if ($this->useLast) {
  99. // Check the cache to see if we have a "last-known-good" version.
  100. // The actual cache gets blown away when new notices are added, but
  101. // the "last" value holds a lot of info. We might need to only generate
  102. // a few at the "tip", which can bound our queries and save lots
  103. // of time.
  104. $ids = $this->getCacheNoticeIds($cache, $idkey . ';last', true);
  105. if (!is_null($ids)) {
  106. // Set the actual cache value as well
  107. $id_str = implode(',', $ids);
  108. $cache->set($idkey, $id_str);
  109. return array_slice($ids, $offset, $limit);
  110. }
  111. }
  112. // No cache hits :( Generate directly and stick the results
  113. // into the cache. Note we generate the full cache window.
  114. $window = $this->stream->getNoticeIds(0, self::CACHE_WINDOW, null, null);
  115. $windowstr = implode(',', $window);
  116. $cache->set($idkey, $windowstr);
  117. if ($this->useLast) {
  118. $cache->set($idkey . ';last', $windowstr);
  119. }
  120. // Return just the slice that was requested
  121. return array_slice($window, $offset, $limit);
  122. }
  123. }