agentic_huge_data_base / wiki
页面 Cognee · 5.6 缓存层·DeepWiki 中文全文译文

5.6 · 缓存层

记忆管道与知识图谱构建 · 聚焦本章的模块关系、源码依据与实现要点。

项目Cognee 章节5.6 状态全文译文 模块系统架构、存储与持久化、界面与交互、测试、发布与运维
源码线索
  • cognee/infrastructure/databases/cache/__init__.py
  • cognee/infrastructure/databases/cache/cache_db_interface.py
  • cognee/infrastructure/databases/cache/config.py
  • cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py
  • cognee/infrastructure/databases/cache/get_cache_engine.py
  • cognee/infrastructure/databases/cache/models.py
  • cognee/infrastructure/databases/cache/redis/RedisAdapter.py
  • cognee/infrastructure/databases/cache/tapes/TapesCacheAdapter.py
  • cognee/infrastructure/databases/cache/tapes/__init__.py
  • cognee/modules/data/deletion/prune_data.py
模块标签
  • 系统架构
  • 存储与持久化
  • 界面与交互
  • 测试、发布与运维
  • 配置治理

章节正文

缓存层

缓存层

相关源文件

本章引用的主要源码文件:

  • cognee/infrastructure/databases/cache/__init__.py
  • cognee/infrastructure/databases/cache/cache_db_interface.py
  • cognee/infrastructure/databases/cache/config.py
  • cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py
  • cognee/infrastructure/databases/cache/get_cache_engine.py
  • cognee/infrastructure/databases/cache/models.py
  • cognee/infrastructure/databases/cache/redis/RedisAdapter.py
  • cognee/infrastructure/databases/cache/tapes/TapesCacheAdapter.py
  • cognee/infrastructure/databases/cache/tapes/__init__.py
  • cognee/modules/data/deletion/prune_data.py
  • cognee/tests/integration/infrastructure/session/test_tapes_cache_adapter.py
  • cognee/tests/test_usage_logger_e2e.py
  • cognee/tests/unit/infrastructure/databases/cache/test_cache_config.py
  • cognee/tests/unit/infrastructure/databases/cache/test_fs_adapter_crud.py
  • cognee/tests/unit/infrastructure/databases/cache/test_redis_adapter_crud.py
  • cognee/tests/unit/modules/data/deletion/test_prune_data.py

Cognee 中的缓存层为分布式锁、会话持久化、代理追踪存储和用量日志记录提供了基础设施。它支持多种后端:Redis 用于需要分布式协调的生产环境,文件系统(diskcache) 用于本地开发,以及 Tapes 用于将交互镜像到语义搜索表面 cognee/infrastructure/databases/cache/config.py:25-25, cognee/infrastructure/databases/cache/tapes/TapesCacheAdapter.py:1-9

缓存架构

缓存系统基于 CacheDBInterface 协议构建,该协议定义了会话管理、代理追踪和锁定的标准操作 cognee/infrastructure/databases/cache/cache_db_interface.py:8-12

缓存系统组件
组件职责
CacheConfig管理后端选择、TTL(生存时间)和连接详情等设置 cognee/infrastructure/databases/cache/config.py:7-45
CacheDBInterface抽象基类,定义了问答会话、代理追踪、锁和用量日志记录的方法 cognee/infrastructure/databases/cache/cache_db_interface.py:8-216
RedisAdapterRedis 的实现,支持分布式锁和异步会话/追踪存储 cognee/infrastructure/databases/cache/redis/RedisAdapter.py:21-37
FSCacheAdapter使用 diskcache 的本地文件系统实现 cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py:22-36
TapesCacheAdapter基于文件系统的适配器,将新的问答条目镜像到 Tapes 入库服务器,用于 Merkle DAG 存储 cognee/infrastructure/databases/cache/tapes/TapesCacheAdapter.py:23-41
代码到系统的映射:会话和追踪流

此图展示了会话数据和代理追踪如何从代码实体流入底层的缓存存储。

Cognee · 代码到系统的映射:会话和追踪流 · 图 1
Cognee · 代码到系统的映射:会话和追踪流 · 图 1

来源: cognee/infrastructure/databases/cache/cache_db_interface.py:70-89, cognee/infrastructure/databases/cache/cache_db_interface.py:163-180, cognee/infrastructure/databases/cache/models.py:1-20, cognee/infrastructure/databases/cache/redis/RedisAdapter.py:21-36, cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py:22-36

实现细节

缓存引擎工厂

get_cache_engine 函数作为入口点,利用 create_cache_engine 根据 cache_backend 配置返回相应的适配器 cognee/infrastructure/databases/cache/get_cache_engine.py:85-109。该工厂使用 lru_cache 确保每组参数只返回一个单例实例 cognee/infrastructure/databases/cache/get_cache_engine.py:10-26

会话和代理追踪管理

Cognee 区分了两种类型的会话数据:

  1. 问答会话: 用户问题、上下文和大语言模型回答的历史记录。通过 create_qa_entry 管理 cognee/infrastructure/databases/cache/cache_db_interface.py:70-82。条目包含 used_graph_element_ids 等元数据,用于追踪哪些图节点/边影响了回答 cognee/infrastructure/databases/cache/redis/RedisAdapter.py:105-118
  2. 代理追踪: 代理逻辑的详细执行步骤(例如,工具调用、错误)。通过 append_agent_trace_step 管理 cognee/infrastructure/databases/cache/cache_db_interface.py:163-180
分布式锁

分布式锁协调多进程环境中对共享资源的访问,专门保护 Kuzu(旧版)或 Ladybug 等图数据库。

  • Redis 实现: 使用 sync_redis.lock 并设置 thread_local=False,允许锁在不同于获取它的线程中释放 cognee/infrastructure/databases/cache/redis/RedisAdapter.py:207-220。它使用可配置的 agentic_lock_expireagentic_lock_timeout cognee/infrastructure/databases/cache/config.py:34-35
  • 文件系统实现: 不支持锁定,会抛出 SharedLadybugLockRequiresRedisError 异常 cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py:179-183
用量日志记录

当启用 usage_logging 时,系统会将 API 端点调用和 MCP 工具调用记录到缓存中 cognee/infrastructure/databases/cache/config.py:38-39。这些日志以可配置的 TTL(默认 7 天)存储 cognee/infrastructure/databases/cache/config.py:39-39RedisAdapter 通过 rpush 将日志追加到由 log_key 标识的列表键中来实现此功能 cognee/infrastructure/databases/cache/redis/RedisAdapter.py:226-250

数据流:分布式锁

此图展示了使用 Redis 适配器保护共享资源的锁定机制。

Cognee · 数据流:分布式锁 · 图 2
Cognee · 数据流:分布式锁 · 图 2

来源: cognee/infrastructure/databases/cache/cache_db_interface.py:40-49, cognee/infrastructure/databases/cache/redis/RedisAdapter.py:207-220, cognee/infrastructure/databases/cache/get_cache_engine.py:85-109

配置参数

设置通过 CacheConfig(Pydantic 设置)管理,支持环境变量覆盖 cognee/infrastructure/databases/cache/config.py:7-46

变量类型默认值描述
CACHE_BACKENDLiteral["redis", "fs", "tapes"]"fs"缓存的存储后端 cognee/infrastructure/databases/cache/config.py:25-25
CACHINGboolTrue完全启用或禁用缓存层 cognee/infrastructure/databases/cache/config.py:26-26
CACHE_HOSTstr"localhost"Redis 主机地址 cognee/infrastructure/databases/cache/config.py:30-30
CACHE_PORTint6379Redis 端口 cognee/infrastructure/databases/cache/config.py:31-31
SESSION_TTL_SECONDSint604800会话/追踪数据的过期时间(7 天) cognee/infrastructure/databases/cache/config.py:36-36
SHARED_LADYBUG_LOCKboolFalse为 Ladybug 启用分布式锁 cognee/infrastructure/databases/cache/config.py:28-28
USAGE_LOGGINGboolFalse启用 API 和 MCP 工具用量日志记录 cognee/infrastructure/databases/cache/config.py:38-38

来源:

  • cognee/infrastructure/databases/cache/config.py
  • cognee/infrastructure/databases/cache/cache_db_interface.py
  • cognee/infrastructure/databases/cache/get_cache_engine.py
  • cognee/infrastructure/databases/cache/redis/RedisAdapter.py
  • cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py
  • cognee/infrastructure/databases/cache/tapes/TapesCacheAdapter.py