ReadingIndexService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Services;
  3. use App\Facades\RedisCache;
  4. use App\Models\Sentence;
  5. /**
  6. * 阅读的内容索引(纯能力,v3 阅读与三藏下载共用):某 channel 哪些书、哪些段有译文(句子 strlen > 0)。
  7. *
  8. * - **段号索引** `/read-index/{channel}/{book}`:这本书里有译文的段,存成连续区间;
  9. * - **书号索引** `/read-index/{channel}/books`:有译文的书。整 channel 重建要几百毫秒,只在
  10. * 「新出现一本书」或「可能少了一本书」时删。
  11. *
  12. * 只随句子失效({@see PaliContentService::paragraphChanged()},Sentence 的模型事件调用):
  13. * 只有「有译文的段」可能变了才删,改一句译文的内容不动索引。批注不影响索引。
  14. * **绕过模型事件的批量写入(`Sentence::where()->update()/delete()`、`DB::table('sentences')`)要自己调
  15. * paragraphChanged()**,否则会读到旧索引直到过期。
  16. */
  17. class ReadingIndexService
  18. {
  19. /** @var array<string, list<int>> */
  20. private array $paragraphs = [];
  21. /**
  22. * 有译文的书号,升序。
  23. *
  24. * @return list<int>
  25. */
  26. public function books(string $channel): array
  27. {
  28. return RedisCache::remember(
  29. self::booksKey($channel),
  30. (int) config('mint.cache.read_index_ttl'),
  31. fn () => Sentence::where('channel_uid', $channel)
  32. ->where('strlen', '>', 0)
  33. ->distinct()
  34. ->orderBy('book_id')
  35. ->pluck('book_id')
  36. ->map(fn ($book) => (int) $book)
  37. ->all()
  38. );
  39. }
  40. /**
  41. * 这本书里有译文的段落号,升序。
  42. *
  43. * @return list<int>
  44. */
  45. public function paragraphs(string $channel, int $book): array
  46. {
  47. $key = self::paragraphsKey($channel, $book);
  48. if (isset($this->paragraphs[$key])) {
  49. return $this->paragraphs[$key];
  50. }
  51. // 存成连续区间 [[起, 止], …]:整本译完的书只有一个区间,展开成段号列表要八十多 KB
  52. $ranges = RedisCache::remember(
  53. $key,
  54. (int) config('mint.cache.read_index_ttl'),
  55. fn () => self::toRanges(Sentence::where('channel_uid', $channel)
  56. ->where('book_id', $book)
  57. ->where('strlen', '>', 0)
  58. ->distinct()
  59. ->orderBy('paragraph')
  60. ->pluck('paragraph')
  61. ->map(fn ($para) => (int) $para)
  62. ->all())
  63. );
  64. $paras = [];
  65. foreach ($ranges as [$from, $to]) {
  66. array_push($paras, ...range($from, $to));
  67. }
  68. return $this->paragraphs[$key] = $paras;
  69. }
  70. /**
  71. * 这本书「有译文的段」可能变了:删段号索引;书号索引按两个参数决定删不删。
  72. *
  73. * $mayAddBook(这本书现在有了译文)时,只有它不在已缓存的书号里才删;
  74. * $mayRemoveBook(删了句子、strlen 变成 0、句子搬去了别的书或 channel)时一律删,下次重建。
  75. */
  76. public function forget(string $channel, int $book, bool $mayAddBook = false, bool $mayRemoveBook = false): void
  77. {
  78. $key = self::paragraphsKey($channel, $book);
  79. RedisCache::forget($key);
  80. unset($this->paragraphs[$key]);
  81. if ($mayRemoveBook) {
  82. RedisCache::forget(self::booksKey($channel));
  83. } elseif ($mayAddBook) {
  84. $books = RedisCache::get(self::booksKey($channel));
  85. if (is_array($books) && ! in_array($book, $books, true)) {
  86. RedisCache::forget(self::booksKey($channel));
  87. }
  88. }
  89. }
  90. /**
  91. * 升序段号 → 连续区间。
  92. *
  93. * @param list<int> $paras
  94. * @return list<array{int, int}>
  95. */
  96. public static function toRanges(array $paras): array
  97. {
  98. $ranges = [];
  99. foreach ($paras as $para) {
  100. $last = count($ranges) - 1;
  101. if ($last >= 0 && $ranges[$last][1] === $para - 1) {
  102. $ranges[$last][1] = $para;
  103. } else {
  104. $ranges[] = [$para, $para];
  105. }
  106. }
  107. return $ranges;
  108. }
  109. private static function booksKey(string $channel): string
  110. {
  111. return "/read-index/{$channel}/books";
  112. }
  113. private static function paragraphsKey(string $channel, int $book): string
  114. {
  115. return "/read-index/{$channel}/{$book}";
  116. }
  117. }