فهرست منبع

⚰️ (api-v13): 删掉 RedisCache 的二进制方法 putBinary / getBinary / binaryLength / getBinaryRange

只有三藏下载用过,文件改存 S3 临时区后没有调用方;连同 RedisCacheFake 的 length / range 原语、
门面声明与 4 条测试,退回到加入之前(371c0f04d)的版本。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 21 ساعت پیش
والد
کامیت
4d19e19fc6

+ 0 - 4
api-v13/app/Facades/RedisCache.php

@@ -14,10 +14,6 @@ use Illuminate\Support\Facades\Facade;
  * @method static bool has(string $key)
  * @method static bool forget(string $key)
  * @method static int increment(string $key, int $value = 1)
- * @method static bool putBinary(string $key, string $bytes, \DateTimeInterface|\DateInterval|int|null $ttl = null)
- * @method static string|null getBinary(string $key)
- * @method static int|null binaryLength(string $key)
- * @method static string getBinaryRange(string $key, int $start, int $end)
  *
  * @see \App\Services\RedisCache
  */

+ 1 - 64
api-v13/app/Services/RedisCache.php

@@ -28,12 +28,6 @@ use RedisCluster;
  *
  * 集群与 Laravel Cache 用的单机 Redis 不是同一个库,旧缓存不迁移,切换后冷启动。
  *
- * 二进制(导出的 zip、epub 等)走另一组方法 putBinary / getBinary / binaryLength / getBinaryRange:
- * 原样存字节,不经 JSON。同一个 key 只能用一组方法读写——用 get() 读二进制 key 会抛 JsonException,
- * 所以二进制 key 要放在自己的命名空间下(如 `/tipitaka-export/file/…`)。大文件用 getBinaryRange
- * 分块读(GETRANGE),不必把整个值读进 PHP 内存,也正好支持 HTTP Range。
- * 2026-11-21 在 3 主 3 从集群上实测 40MB:SET 95ms、GET 76ms、1MB×40 次 GETRANGE 共 74ms,字节完整。
- *
  * 业务代码通过门面 App\Facades\RedisCache 调用;测试用 RedisCache::fake()
  * 换成内存实现(见 RedisCacheFake)。
  */
@@ -127,44 +121,6 @@ class RedisCache
         return $this->incrementBy($this->key($key), $value);
     }
 
-    /**
-     * 原样写入字节(不经 JSON)。TTL 语义同 put()。
-     */
-    public function putBinary(string $key, string $bytes, DateTimeInterface|DateInterval|int|null $ttl = null): bool
-    {
-        $seconds = $this->secondsUntil($ttl ?? static::$defaultTtl);
-
-        if ($seconds <= 0) {
-            return $this->forget($key);
-        }
-
-        return $this->write($this->key($key), $bytes, $seconds);
-    }
-
-    /**
-     * 原样读回 putBinary() 写入的字节;不存在返回 null。
-     */
-    public function getBinary(string $key): ?string
-    {
-        return $this->read($this->key($key));
-    }
-
-    /**
-     * 值的字节数(STRLEN);不存在返回 null。
-     */
-    public function binaryLength(string $key): ?int
-    {
-        return $this->length($this->key($key));
-    }
-
-    /**
-     * 读字节闭区间 [$start, $end](GETRANGE),越界部分截掉;不存在返回空串。
-     */
-    public function getBinaryRange(string $key, int $start, int $end): string
-    {
-        return $this->range($this->key($key), $start, $end);
-    }
-
     /**
      * 前缀与 key 之间用 / 隔开:`dev` + `foo` → `dev/foo`;`/task/1` 本身带 /,得 `dev/task/1`。
      */
@@ -174,7 +130,7 @@ class RedisCache
     }
 
     /**
-     * 以下六个是仅有的 Redis 存取原语,RedisCacheFake 只需覆盖它们。
+     * 以下四个是仅有的 Redis 存取原语,RedisCacheFake 只需覆盖它们。
      * 参数 $key 已带前缀。
      */
     protected function read(string $key): ?string
@@ -204,25 +160,6 @@ class RedisCache
         return (int) $this->redis()->incrby($key, $value);
     }
 
-    /**
-     * STRLEN 对不存在的 key 返回 0,与空值分不清,所以先看 EXISTS。
-     */
-    protected function length(string $key): ?int
-    {
-        if (! $this->redis()->exists($key)) {
-            return null;
-        }
-
-        return (int) $this->redis()->strlen($key);
-    }
-
-    protected function range(string $key, int $start, int $end): string
-    {
-        $value = $this->redis()->getRange($key, $start, $end);
-
-        return is_string($value) ? $value : '';
-    }
-
     /**
      * 第一个参数 null 表示不读 php.ini 里的具名集群,直接用种子节点。
      */

+ 2 - 19
api-v13/app/Services/RedisCacheFake.php

@@ -5,8 +5,8 @@ namespace App\Services;
 /**
  * RedisCache 的内存实现,供测试使用(相当于 Laravel Cache 的 array store)。
  *
- * 只替换存取原语,序列化、TTL 换算、null 语义仍走 RedisCache 本身,
- * 所以测试覆盖到的是真实逻辑。二进制的 length / range 两个原语也在这里覆盖。通过 App\Facades\RedisCache::fake() 启用。
+ * 只替换四个存取原语,序列化、TTL 换算、null 语义仍走 RedisCache 本身,
+ * 所以测试覆盖到的是真实逻辑。通过 App\Facades\RedisCache::fake() 启用。
  */
 class RedisCacheFake extends RedisCache
 {
@@ -60,21 +60,4 @@ class RedisCacheFake extends RedisCache
 
         return $next;
     }
-
-    protected function length(string $key): ?int
-    {
-        $value = $this->read($key);
-
-        return $value === null ? null : strlen($value);
-    }
-
-    protected function range(string $key, int $start, int $end): string
-    {
-        $value = $this->read($key) ?? '';
-        if ($start >= strlen($value) || $end < $start) {
-            return '';
-        }
-
-        return substr($value, $start, $end - $start + 1);
-    }
 }

+ 0 - 41
api-v13/tests/Feature/RedisCacheTest.php

@@ -131,30 +131,6 @@ test('increment 从 0 开始累加,返回新值', function () {
         ->and(RedisCache::get('c'))->toBe(6);
 });
 
-test('二进制原样往返,长度与区间读取正确', function () {
-    $bytes = "\x00\xff\x80PK\x03\x04".random_bytes(1000);
-    RedisCache::putBinary('/bin/a', $bytes, 60);
-
-    expect(RedisCache::getBinary('/bin/a'))->toBe($bytes)
-        ->and(RedisCache::binaryLength('/bin/a'))->toBe(strlen($bytes))
-        ->and(RedisCache::getBinaryRange('/bin/a', 2, 5))->toBe(substr($bytes, 2, 4))
-        ->and(RedisCache::getBinaryRange('/bin/a', 1000, 9999))->toBe(substr($bytes, 1000))
-        ->and(RedisCache::getBinaryRange('/bin/a', 99999, 100000))->toBe('');
-});
-
-test('二进制 key 不存在时读到 null,TTL <= 0 等同 forget', function () {
-    RedisCache::putBinary('/bin/b', 'x', 60);
-    RedisCache::putBinary('/bin/b', 'y', 0);
-
-    expect(RedisCache::getBinary('/bin/b'))->toBeNull()
-        ->and(RedisCache::binaryLength('/bin/b'))->toBeNull()
-        ->and(RedisCache::getBinaryRange('/bin/b', 0, 10))->toBe('');
-});
-
-test('非 UTF-8 字节用 put 存不了,必须走 putBinary', function () {
-    expect(fn () => RedisCache::put('/bin/c', "\xff\xfe"))->toThrow(JsonException::class);
-});
-
 /*
 | 第二部分:契约。连真实 Redis Cluster(mint.redis_cluster),证明 key 名带前缀、
 | 值是 JSON、整数是纯文本(ai-translate 用 int() 读 /task/{id}/pointer)、TTL 与写值一条命令落地,
@@ -233,20 +209,3 @@ test('不存在的 key 读到 null / 默认值,forget 返回 false', function
         ->and($cache->get('missing', 'd'))->toBe('d')
         ->and($cache->forget('missing'))->toBeFalse();
 })->skip(fn () => ! realRedisAvailable(), '没有可用的 Redis Cluster');
-
-test('二进制在集群里原样落地、带 TTL,可按区间读', function () {
-    $cache = new RedisCacheService(prefix: redisCacheTestPrefix());
-    $bytes = random_bytes(3 * 1024 * 1024);
-    $cache->putBinary('bin', $bytes, 60);
-
-    $chunks = '';
-    for ($offset = 0; $offset < strlen($bytes); $offset += 1048576) {
-        $chunks .= $cache->getBinaryRange('bin', $offset, $offset + 1048575);
-    }
-
-    expect(rawRedisCluster()->get(redisCacheTestPrefix().'/bin'))->toBe($bytes)
-        ->and(rawRedisCluster()->ttl(redisCacheTestPrefix().'/bin'))->toBeGreaterThan(0)->toBeLessThanOrEqual(60)
-        ->and($cache->binaryLength('bin'))->toBe(strlen($bytes))
-        ->and($chunks)->toBe($bytes)
-        ->and($cache->binaryLength('missing-bin'))->toBeNull();
-})->skip(fn () => ! realRedisAvailable(), '没有可用的 Redis Cluster');