Files
FastapiAdmin/backend/app/api/v1/module_monitor/cache/service.py
T
zhangtao fce6333722 style: 优化代码格式和导入顺序
refactor: 重构数据库连接模块,提高可维护性

docs: 更新README.md项目描述

style: 调整日志输出格式和内容

refactor: 优化中间件日志记录逻辑

style: 统一代码中的空行和导入顺序

refactor: 分离数据库引擎和会话创建逻辑

style: 清理未使用的导入和代码

refactor: 重命名数据库会话变量

style: 调整jinja2模板变量命名

refactor: 优化异常处理和日志记录

style: 统一字符串格式化方式

refactor: 优化redis连接错误处理

style: 调整注释格式和位置

refactor: 优化数据库连接配置

style: 清理多余空行和注释
2025-11-16 18:43:30 +08:00

148 lines
4.3 KiB
Python

# -*- coding: utf-8 -*-
from redis.asyncio.client import Redis
from app.common.enums import RedisInitKeyConfig
from app.core.redis_crud import RedisCURD
from .schema import CacheMonitorSchema, CacheInfoSchema
class CacheService:
"""
缓存监控模块服务层
"""
@classmethod
async def get_cache_monitor_statistical_info_service(cls, redis: Redis)->dict:
"""
获取缓存监控信息。
参数:
- redis (Redis): Redis 对象。
返回:
- dict: 缓存监控信息字典。
"""
info = await RedisCURD(redis).info()
db_size = await RedisCURD(redis).db_size()
command_stats_dict = await RedisCURD(redis).commandstats()
command_stats = [
dict(name=key.split('_')[1], value=str(value.get('calls'))) for key, value in command_stats_dict.items()
]
result = CacheMonitorSchema(command_stats=command_stats, db_size=db_size, info=info)
return result.model_dump()
@classmethod
async def get_cache_monitor_cache_name_service(cls)->list:
"""
获取缓存名称列表信息。
返回:
- list: 缓存名称列表信息。
"""
name_list = []
for key_config in RedisInitKeyConfig:
name_list.append(
CacheInfoSchema(
cache_key='',
cache_name=key_config.key,
cache_value='',
remark=key_config.remark,
).model_dump()
)
return name_list
@classmethod
async def get_cache_monitor_cache_key_service(cls, redis: Redis, cache_name: str)->list:
"""
获取缓存键名列表信息。
参数:
- redis (Redis): Redis 对象。
- cache_name (str): 缓存名称。
返回:
- list: 缓存键名列表信息。
"""
cache_keys = await RedisCURD(redis).get_keys(f'{cache_name}*')
cache_key_list = [key.split(':', 1)[1] for key in cache_keys if key.startswith(f'{cache_name}:')]
return cache_key_list
@classmethod
async def get_cache_monitor_cache_value_service(cls, redis: Redis, cache_name: str, cache_key: str)->dict:
"""
获取缓存内容信息。
参数:
- redis (Redis): Redis 对象。
- cache_name (str): 缓存名称。
- cache_key (str): 缓存键名。
返回:
- dict: 缓存内容信息字典。
"""
cache_value = await RedisCURD(redis).get(f'{cache_name}:{cache_key}')
return CacheInfoSchema(cache_key=cache_key, cache_name=cache_name, cache_value=cache_value, remark='').model_dump()
@classmethod
async def clear_cache_monitor_cache_name_service(cls, redis: Redis, cache_name: str)->bool:
"""
清除指定缓存名称对应的所有键值。
参数:
- redis (Redis): Redis 对象。
- cache_name (str): 缓存名称。
返回:
- bool: 是否清理成功。
"""
cache_keys = await RedisCURD(redis).get_keys(f'{cache_name}*')
if cache_keys:
await RedisCURD(redis).delete(*cache_keys)
return True
@classmethod
async def clear_cache_monitor_cache_key_service(cls, redis: Redis, cache_key: str)->bool:
"""
清除匹配指定键名的所有键值。
参数:
- redis (Redis): Redis 对象。
- cache_key (str): 缓存键名。
返回:
- bool: 是否清理成功。
"""
cache_keys = await RedisCURD(redis).get_keys(f'*{cache_key}')
if cache_keys:
await RedisCURD(redis).delete(*cache_keys)
return True
@classmethod
async def clear_cache_monitor_all_service(cls, redis: Redis)->bool:
"""
清除所有缓存。
参数:
- redis (Redis): Redis 对象。
返回:
- bool: 是否清理成功。
"""
cache_keys = await RedisCURD(redis).get_keys()
if cache_keys:
await RedisCURD(redis).delete(*cache_keys)
return True
# 避免清除所有的缓存,而采用上面的方式,只清除本系统内指定的所有缓存
# return await RedisCURD(redis).clear()