GlossaryService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Facades\RedisCache;
  4. use App\Http\Resources\V3\GlossaryEntryResource;
  5. use App\Models\DhammaTerm;
  6. use App\Services\ChannelService as ChannelLookup;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Str;
  9. /**
  10. * 术语词汇表(只读):语法术语表与社区译名表,客户端启动时整表拉取后本地查。
  11. *
  12. * 取代 v2 `term-vocabulary`(`TermVocabularyController`)。数据就是两个系统 channel 里的术语:
  13. * - grammar:`_System_Grammar_Term_{lang}_`
  14. * - community:`_community_term_{lang}_`
  15. * 该语言的 channel 不存在时回退到英文(与 v2 相同)。注意它与批注的 `glossary` 类型(外挂术语)不是一回事。
  16. */
  17. class GlossaryService
  18. {
  19. public const KINDS = ['grammar', 'community'];
  20. private const CHANNEL_PREFIX = [
  21. 'grammar' => '_System_Grammar_Term_',
  22. 'community' => '_community_term_',
  23. ];
  24. private const FALLBACK_LANG = 'en';
  25. /**
  26. * 响应结构的版本,拼进 ETag。{@see GlossaryEntryResource} 的字段一改就加 1,
  27. * 否则浏览器带着旧 ETag 来会拿到 304、继续用旧结构的缓存。
  28. */
  29. private const SCHEMA_VERSION = 2;
  30. /** ETag 在 Redis 里的存活时间;本任务每天都会换新,过期了也只是让客户端多拉一次 */
  31. private const ETAG_TTL = 30 * 24 * 60 * 60;
  32. public function __construct(private readonly ChannelLookup $channels) {}
  33. /**
  34. * 某种词汇表的全部条目(按词排序),以及实际用到的语言。
  35. *
  36. * @return array{items: Collection<int, DhammaTerm>, lang: string}
  37. */
  38. public function entries(string $kind, string $lang): array
  39. {
  40. [$channel, $served] = $this->channelOf($kind, $lang);
  41. $items = $channel === false
  42. ? collect()
  43. : DhammaTerm::query()
  44. ->where('channal', $channel)
  45. ->whereNotNull('word')
  46. ->where('word', '<>', '')
  47. ->orderBy('word')
  48. ->orderBy('guid')
  49. ->get(['guid', 'word', 'tag', 'meaning', 'other_meaning']);
  50. return ['items' => $items, 'lang' => $served];
  51. }
  52. /**
  53. * 某种词汇表当前的 ETag;没有 ETag 的表(grammar)或表不存在时返回 null。
  54. *
  55. * 只有 community 有:它的 channel 只由 `upgrade:community.term` 维护,任务跑完调
  56. * {@see rotateEtag()} 换一个新 uuid,所以不用查整表、不用算 hash。有人绕过任务手工改了这个
  57. * channel,ETag 不会变,客户端要等下次任务跑完才看得到。grammar 是人工编辑的,暂不支持。
  58. */
  59. public function etag(string $kind, string $lang): ?string
  60. {
  61. if ($kind !== 'community') {
  62. return null;
  63. }
  64. [$channel] = $this->channelOf($kind, $lang);
  65. if ($channel === false) {
  66. return null;
  67. }
  68. // key 不存在(刚部署、Redis 清过、过期)就现生成一个
  69. $token = RedisCache::remember(self::etagKey($channel), self::ETAG_TTL, fn () => (string) Str::uuid());
  70. return $token.'.'.self::SCHEMA_VERSION;
  71. }
  72. /** 词汇表 channel 的内容变了:换一个新 ETag,客户端下次请求会拿到 200 与新数据 */
  73. public function rotateEtag(string $channel): void
  74. {
  75. RedisCache::put(self::etagKey($channel), (string) Str::uuid(), self::ETAG_TTL);
  76. }
  77. private static function etagKey(string $channel): string
  78. {
  79. return "/glossary/{$channel}/etag";
  80. }
  81. /**
  82. * 实际要读的 channel 与语言:该语言没有这张表时回退到英文(与 v2 相同)。
  83. *
  84. * @return array{0: string|false, 1: string}
  85. */
  86. private function channelOf(string $kind, string $lang): array
  87. {
  88. $prefix = self::CHANNEL_PREFIX[$kind];
  89. $channel = $this->channels->systemUid($prefix.strtolower($lang).'_');
  90. if ($channel !== false) {
  91. return [$channel, $lang];
  92. }
  93. return [$this->channels->systemUid($prefix.self::FALLBACK_LANG.'_'), self::FALLBACK_LANG];
  94. }
  95. }