litefs.cache package
- class litefs.cache.CacheBackend[源代码]
基类:
object缓存后端类型
- DATABASE = 'database'
- MEMCACHE = 'memcache'
- MEMORY = 'memory'
- REDIS = 'redis'
- TREE = 'tree'
- class litefs.cache.CacheFactory[源代码]
基类:
object缓存工厂
根据配置创建不同类型的缓存实例
- static create_cache(backend: str = 'memory', **kwargs) MemoryCache | TreeCache | RedisCache | DatabaseCache | MemcacheCache[源代码]
创建缓存实例
- 参数:
backend -- 缓存后端类型(memory, tree, redis, database, memcache)
**kwargs -- 缓存配置参数
- 返回:
缓存实例
- 抛出:
ValueError -- 不支持的缓存后端
ImportError -- Redis 或 Memcache 包未安装
- static create_from_config(config) MemoryCache | TreeCache | RedisCache | DatabaseCache | MemcacheCache[源代码]
从配置对象创建缓存实例
- 参数:
config -- 配置对象,应包含 cache_backend 和相关配置
- 返回:
缓存实例
- class litefs.cache.CacheManager[源代码]
基类:
object全局缓存管理器(单例模式)
确保缓存对象在应用生命周期内常驻内存,不会因为 Litefs 实例的 创建和销毁而丢失数据。
- 使用示例:
# 获取缓存实例(自动创建) cache = CacheManager.get_cache()
# 获取指定类型的缓存 session_cache = CacheManager.get_cache(
backend='memory', max_size=1000000, cache_key='sessions'
)
# 重置缓存(谨慎使用) CacheManager.reset_cache()
- classmethod get_cache(backend: str = 'tree', cache_key: str | None = None, **kwargs) MemoryCache | TreeCache | RedisCache | DatabaseCache | MemcacheCache[源代码]
获取缓存实例(单例模式)
如果缓存实例不存在,则自动创建。同一 cache_key 的缓存实例 在整个应用生命周期内保持唯一。
- 参数:
backend -- 缓存后端类型
cache_key -- 缓存实例标识,None 使用默认缓存
**kwargs -- 缓存配置参数
- 返回:
缓存实例
- classmethod get_file_cache(**kwargs)[源代码]
获取文件缓存实例
- 参数:
**kwargs -- 配置参数,支持 clean_period, expiration_time
- 返回:
TreeCache 实例
- class litefs.cache.DatabaseCache(db_path: str = ':memory:', table_name: str = 'cache', expiration_time: int = 3600, **kwargs)[源代码]
基类:
object数据库缓存实现
使用 SQLite 数据库作为缓存后端,提供持久化的缓存支持
- expire(key: str, expiration: int) bool[源代码]
设置键的过期时间
- 参数:
key -- 缓存键
expiration -- 过期时间(秒)
- 返回:
是否设置成功
- put(key: str, val: Any, expiration: int | None = None) None[源代码]
存储值到缓存
- 参数:
key -- 缓存键
val -- 缓存值
expiration -- 过期时间(秒),如果为 None 则使用默认过期时间
- class litefs.cache.FileEventHandler(app)[源代码]
基类:
FileSystemEventHandler- on_created(event)[源代码]
Called when a file or directory is created.
- 参数:
event (
DirCreatedEventorFileCreatedEvent) -- Event representing file/directory creation.
- on_deleted(event)[源代码]
Called when a file or directory is deleted.
- 参数:
event (
DirDeletedEventorFileDeletedEvent) -- Event representing file/directory deletion.
- class litefs.cache.FormCache(max_size: int = 1000, default_ttl: int = 300)[源代码]
基类:
object表单数据缓存
使用 LRU (Least Recently Used) 策略管理缓存 支持自动过期和容量限制
- class litefs.cache.MemcacheCache(memcache_client=None, servers: list = ['localhost:11211'], key_prefix: str = 'litefs:', expiration_time: int = 3600, **kwargs)[源代码]
基类:
objectMemcache 缓存实现
使用 Memcache 作为缓存后端,提供高性能的分布式缓存支持
- delete_pattern(pattern: str) int[源代码]
删除匹配模式的键
注意:Memcache 不支持模式匹配,此方法仅返回 0
- 参数:
pattern -- 键模式
- 返回:
删除的键数量(始终为 0)
- expire(key: str, expiration: int) bool[源代码]
设置键的过期时间
- 参数:
key -- 缓存键
expiration -- 过期时间(秒)
- 返回:
是否设置成功
- put(key: str, val: Any, expiration: int | None = None) None[源代码]
存储值到缓存
- 参数:
key -- 缓存键
val -- 缓存值
expiration -- 过期时间(秒),如果为 None 则使用默认过期时间
- class litefs.cache.RedisCache(redis_client=None, host: str = 'localhost', port: int = 6379, db: int = 0, password: str | None = None, key_prefix: str = 'litefs:', expiration_time: int = 3600, **kwargs)[源代码]
基类:
objectRedis 缓存实现
使用 Redis 作为缓存后端,提供高性能的分布式缓存支持
- expire(key: str, expiration: int) bool[源代码]
设置键的过期时间
- 参数:
key -- 缓存键
expiration -- 过期时间(秒)
- 返回:
是否设置成功
- put(key: str, val: Any, expiration: int | None = None) None[源代码]
存储值到缓存
- 参数:
key -- 缓存键
val -- 缓存值
expiration -- 过期时间(秒),如果为 None 则使用默认过期时间
- litefs.cache.get_global_cache(backend: str = 'tree', **kwargs) MemoryCache | TreeCache | RedisCache | DatabaseCache | MemcacheCache[源代码]
获取全局缓存实例的便捷函数
- 参数:
backend -- 缓存后端类型
**kwargs -- 缓存配置参数
- 返回:
缓存实例
Submodules
litefs.cache.cache module
- class litefs.cache.cache.FileEventHandler(app)[源代码]
基类:
FileSystemEventHandler- on_created(event)[源代码]
Called when a file or directory is created.
- 参数:
event (
DirCreatedEventorFileCreatedEvent) -- Event representing file/directory creation.
- on_deleted(event)[源代码]
Called when a file or directory is deleted.
- 参数:
event (
DirDeletedEventorFileDeletedEvent) -- Event representing file/directory deletion.