modify: 清除diskcache依赖
This commit is contained in:
+31
-5
@@ -5,6 +5,7 @@ redis 后端优先连接真实服务(REDIS_HOST/REDIS_PORT),不可用时
|
||||
"""
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import threading
|
||||
import time
|
||||
|
||||
@@ -57,7 +58,7 @@ def _shutdown_fake_redis():
|
||||
@pytest.fixture
|
||||
def disk_cache(tmp_path):
|
||||
cache = Cache.from_config(
|
||||
{"backend": "diskcache", "cache_store_path": str(tmp_path / "cache")}
|
||||
{"backend": "sqlite", "path": str(tmp_path / "cache.db")}
|
||||
)
|
||||
yield cache
|
||||
cache.close()
|
||||
@@ -116,7 +117,7 @@ def test_expire_disk(disk_cache):
|
||||
disk_cache.set("e", 1, expire=1)
|
||||
assert "e" in disk_cache
|
||||
time.sleep(1.2)
|
||||
assert "e" not in disk_cache # diskcache 读时惰性淘汰
|
||||
assert "e" not in disk_cache # sqlite 读时惰性淘汰
|
||||
assert disk_cache.get("e", "miss") == "miss"
|
||||
|
||||
|
||||
@@ -130,14 +131,14 @@ def test_touch_disk(disk_cache):
|
||||
|
||||
def test_context_manager(tmp_path):
|
||||
with Cache.from_config(
|
||||
{"backend": "diskcache", "cache_store_path": str(tmp_path / "cm")}
|
||||
{"backend": "sqlite", "path": str(tmp_path / "cm.db")}
|
||||
) as cache:
|
||||
cache["x"] = 1
|
||||
assert cache["x"] == 1
|
||||
|
||||
|
||||
def test_from_config_defaults(tmp_path, monkeypatch):
|
||||
# 缺省 backend 时默认 diskcache;缺省 cache_store_path 时用 ./cache_data
|
||||
# 缺省 backend 时默认 sqlite;缺省 path 时用 ./cache_data/cache.db
|
||||
monkeypatch.chdir(tmp_path)
|
||||
cache = Cache.from_config({})
|
||||
try:
|
||||
@@ -149,7 +150,32 @@ def test_from_config_defaults(tmp_path, monkeypatch):
|
||||
|
||||
def test_invalid_backend(tmp_path):
|
||||
with pytest.raises(ValueError):
|
||||
Cache.from_config({"backend": "memcached", "cache_store_path": str(tmp_path)})
|
||||
Cache.from_config({"backend": "memcached", "path": str(tmp_path)})
|
||||
|
||||
|
||||
def test_sqlite_json_interop_and_multi_instance(tmp_path):
|
||||
"""SQLite 落盘是纯 JSON 文本(Rust 可读),且两个后端实例共享同一文件。"""
|
||||
db = str(tmp_path / "shared.db")
|
||||
a = Cache.from_config({"backend": "sqlite", "path": db})
|
||||
b = Cache.from_config({"backend": "sqlite", "path": db})
|
||||
try:
|
||||
# 待共享值:纯 JSON 类型,应原样以 JSON 文本落盘
|
||||
a.set("app:latest", {"msg": "hello", "n": 42})
|
||||
# 共享文件可跨实例读取(多进程安全语义)
|
||||
assert b.get("app:latest") == {"msg": "hello", "n": 42}
|
||||
# 直接查库:v 列为 JSON 文本而非 pickle 字节
|
||||
row = sqlite3.connect(db).execute(
|
||||
"SELECT v FROM cache WHERE k='app:latest'"
|
||||
).fetchone()
|
||||
assert row is not None
|
||||
assert row[0] == '{"msg":"hello","n":42}'
|
||||
# TTL 过期后跨实例同样读不到
|
||||
a.set("exp", 1, expire=1)
|
||||
time.sleep(1.1)
|
||||
assert b.get("exp") is None
|
||||
finally:
|
||||
a.close()
|
||||
b.close()
|
||||
|
||||
|
||||
# ── redis 后端 ──────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user