visuddhinanda пре 5 дана
родитељ
комит
c593acddde

+ 17 - 17
api-v13/app/Services/PaliContentService.php

@@ -655,14 +655,13 @@ class PaliContentService
         $level = $this->paragraphLevel($book, $para);
         // 缓存句子,段落外壳与标题级别有关,不进缓存
         $key = self::paragraphCacheKey($book, $para, $channelUid, $format);
-        $cached = Cache::tags([self::paragraphCacheTag($book, $para, $channelUid)])
-            ->remember(
-                $key,
-                config('mint.cache.expire'),
-                function () use ($book, $para, $channelUid, $format) {
-                    return $this->renderReadSentences($book, $para, $channelUid, $format);
-                }
-            );
+        $cached = Cache::remember(
+            $key,
+            config('mint.cache.expire'),
+            function () use ($book, $para, $channelUid, $format) {
+                return $this->renderReadSentences($book, $para, $channelUid, $format);
+            }
+        );
 
         $result = [
             'para' => $para,
@@ -700,19 +699,18 @@ class PaliContentService
     }
 
     /**
-     * 段落阅读模式缓存的 key
+     * 阅读模式支持的全部格式。forgetParagraph 需要逐格式清除缓存 key。
+     *
+     * @var array<int, string>
      */
-    public static function paragraphCacheKey(int $book, int $para, string $channelUid, string $format): string
-    {
-        return "/read-para/{$book}-{$para}/{$channelUid}/{$format}";
-    }
+    private const FORMATS = ['html', 'markdown', 'react', 'text'];
 
     /**
-     * 段落缓存的 tag。一个段落一个 channel 的全部格式共用一个 tag
+     * 段落阅读模式缓存的 key
      */
-    public static function paragraphCacheTag(int $book, int $para, string $channelUid): string
+    public static function paragraphCacheKey(int $book, int $para, string $channelUid, string $format): string
     {
-        return "read-para:{$book}-{$para}:{$channelUid}";
+        return "/read-para/{$book}-{$para}/{$channelUid}/{$format}";
     }
 
     /**
@@ -720,7 +718,9 @@ class PaliContentService
      */
     public static function forgetParagraph(int $book, int $para, string $channelUid): void
     {
-        Cache::tags([self::paragraphCacheTag($book, $para, $channelUid)])->flush();
+        foreach (self::FORMATS as $format) {
+            Cache::forget(self::paragraphCacheKey($book, $para, $channelUid, $format));
+        }
     }
 
     /**

+ 2 - 3
api-v13/tests/Feature/TipitakaReadParaTest.php

@@ -113,15 +113,14 @@ it('caches the paragraph and drops the cache when a sentence changes', function
     $url = "/api/v3/tipitaka-read-para/9001-1?channel={$channel}";
     $this->getJson($url)->assertOk();
 
-    $tag = PaliContentService::paragraphCacheTag(9001, 1, $channel);
     $key = PaliContentService::paragraphCacheKey(9001, 1, $channel, 'html');
-    expect(Cache::tags([$tag])->has($key))->toBeTrue();
+    expect(Cache::has($key))->toBeTrue();
 
     $sentence = Sentence::where('book_id', 9001)->where('paragraph', 1)->orderBy('word_start')->first();
     $sentence->content = 'changed sentence';
     $sentence->save();
 
-    expect(Cache::tags([$tag])->has($key))->toBeFalse();
+    expect(Cache::has($key))->toBeFalse();
     expect($this->getJson($url)->json('data.display'))->toContain('changed sentence');
 });