|
|
@@ -2,9 +2,12 @@
|
|
|
|
|
|
namespace App\Services\V3;
|
|
|
|
|
|
+use App\Facades\RedisCache;
|
|
|
+use App\Http\Resources\V3\GlossaryEntryResource;
|
|
|
use App\Models\DhammaTerm;
|
|
|
use App\Services\ChannelService as ChannelLookup;
|
|
|
use Illuminate\Support\Collection;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
/**
|
|
|
* 术语词汇表(只读):语法术语表与社区译名表,客户端启动时整表拉取后本地查。
|
|
|
@@ -25,6 +28,15 @@ class GlossaryService
|
|
|
|
|
|
private const FALLBACK_LANG = 'en';
|
|
|
|
|
|
+ /**
|
|
|
+ * 响应结构的版本,拼进 ETag。{@see GlossaryEntryResource} 的字段一改就加 1,
|
|
|
+ * 否则浏览器带着旧 ETag 来会拿到 304、继续用旧结构的缓存。
|
|
|
+ */
|
|
|
+ private const SCHEMA_VERSION = 2;
|
|
|
+
|
|
|
+ /** ETag 在 Redis 里的存活时间;本任务每天都会换新,过期了也只是让客户端多拉一次 */
|
|
|
+ private const ETAG_TTL = 30 * 24 * 60 * 60;
|
|
|
+
|
|
|
public function __construct(private readonly ChannelLookup $channels) {}
|
|
|
|
|
|
/**
|
|
|
@@ -34,13 +46,7 @@ class GlossaryService
|
|
|
*/
|
|
|
public function entries(string $kind, string $lang): array
|
|
|
{
|
|
|
- $prefix = self::CHANNEL_PREFIX[$kind];
|
|
|
- $served = $lang;
|
|
|
- $channel = $this->channels->systemUid($prefix.strtolower($lang).'_');
|
|
|
- if ($channel === false) {
|
|
|
- $served = self::FALLBACK_LANG;
|
|
|
- $channel = $this->channels->systemUid($prefix.self::FALLBACK_LANG.'_');
|
|
|
- }
|
|
|
+ [$channel, $served] = $this->channelOf($kind, $lang);
|
|
|
|
|
|
$items = $channel === false
|
|
|
? collect()
|
|
|
@@ -54,4 +60,54 @@ class GlossaryService
|
|
|
|
|
|
return ['items' => $items, 'lang' => $served];
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 某种词汇表当前的 ETag;没有 ETag 的表(grammar)或表不存在时返回 null。
|
|
|
+ *
|
|
|
+ * 只有 community 有:它的 channel 只由 `upgrade:community.term` 维护,任务跑完调
|
|
|
+ * {@see rotateEtag()} 换一个新 uuid,所以不用查整表、不用算 hash。有人绕过任务手工改了这个
|
|
|
+ * channel,ETag 不会变,客户端要等下次任务跑完才看得到。grammar 是人工编辑的,暂不支持。
|
|
|
+ */
|
|
|
+ public function etag(string $kind, string $lang): ?string
|
|
|
+ {
|
|
|
+ if ($kind !== 'community') {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ [$channel] = $this->channelOf($kind, $lang);
|
|
|
+ if ($channel === false) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // key 不存在(刚部署、Redis 清过、过期)就现生成一个
|
|
|
+ $token = RedisCache::remember(self::etagKey($channel), self::ETAG_TTL, fn () => (string) Str::uuid());
|
|
|
+
|
|
|
+ return $token.'.'.self::SCHEMA_VERSION;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 词汇表 channel 的内容变了:换一个新 ETag,客户端下次请求会拿到 200 与新数据 */
|
|
|
+ public function rotateEtag(string $channel): void
|
|
|
+ {
|
|
|
+ RedisCache::put(self::etagKey($channel), (string) Str::uuid(), self::ETAG_TTL);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function etagKey(string $channel): string
|
|
|
+ {
|
|
|
+ return "/glossary/{$channel}/etag";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实际要读的 channel 与语言:该语言没有这张表时回退到英文(与 v2 相同)。
|
|
|
+ *
|
|
|
+ * @return array{0: string|false, 1: string}
|
|
|
+ */
|
|
|
+ private function channelOf(string $kind, string $lang): array
|
|
|
+ {
|
|
|
+ $prefix = self::CHANNEL_PREFIX[$kind];
|
|
|
+ $channel = $this->channels->systemUid($prefix.strtolower($lang).'_');
|
|
|
+ if ($channel !== false) {
|
|
|
+ return [$channel, $lang];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$this->channels->systemUid($prefix.self::FALLBACK_LANG.'_'), self::FALLBACK_LANG];
|
|
|
+ }
|
|
|
}
|