You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					125 lines
				
				4.2 KiB
			
		
		
			
		
	
	
					125 lines
				
				4.2 KiB
			| 
											2 years ago
										 | from fastapi import Request | ||
| 
											1 year ago
										 | from config.enums import RedisInitKeyConfig | ||
| 
											2 years ago
										 | from config.get_redis import RedisUtil | ||
| 
											1 year ago
										 | from module_admin.entity.vo.cache_vo import CacheInfoModel, CacheMonitorModel | ||
| 
											2 years ago
										 | from module_admin.entity.vo.common_vo import CrudResponseModel | ||
|  | 
 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 | class CacheService: | ||
|  |     """
 | ||
|  |     缓存监控模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def get_cache_monitor_statistical_info_services(cls, request: Request): | ||
|  |         """
 | ||
|  |         获取缓存监控信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :return: 缓存监控信息 | ||
|  |         """
 | ||
|  |         info = await request.app.state.redis.info() | ||
|  |         db_size = await request.app.state.redis.dbsize() | ||
|  |         command_stats_dict = await request.app.state.redis.info('commandstats') | ||
| 
											1 year ago
										 |         command_stats = [ | ||
|  |             dict(name=key.split('_')[1], value=str(value.get('calls'))) for key, value in command_stats_dict.items() | ||
|  |         ] | ||
|  |         result = CacheMonitorModel(commandStats=command_stats, dbSize=db_size, info=info) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_cache_monitor_cache_name_services(cls): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取缓存名称列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :return: 缓存名称列表信息 | ||
|  |         """
 | ||
|  |         name_list = [] | ||
| 
											1 year ago
										 |         for key_config in RedisInitKeyConfig: | ||
|  |             name_list.append( | ||
|  |                 CacheInfoModel( | ||
|  |                     cacheKey='', | ||
|  |                     cacheName=key_config.key, | ||
|  |                     cacheValue='', | ||
|  |                     remark=key_config.remark, | ||
| 
											2 years ago
										 |                 ) | ||
| 
											1 year ago
										 |             ) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return name_list | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def get_cache_monitor_cache_key_services(cls, request: Request, cache_name: str): | ||
|  |         """
 | ||
|  |         获取缓存键名列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param cache_name: 缓存名称 | ||
|  |         :return: 缓存键名列表信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         cache_keys = await request.app.state.redis.keys(f'{cache_name}*') | ||
|  |         cache_key_list = [key.split(':', 1)[1] for key in cache_keys if key.startswith(f'{cache_name}:')] | ||
| 
											2 years ago
										 | 
 | ||
|  |         return cache_key_list | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def get_cache_monitor_cache_value_services(cls, request: Request, cache_name: str, cache_key: str): | ||
|  |         """
 | ||
|  |         获取缓存内容信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param cache_name: 缓存名称 | ||
|  |         :param cache_key: 缓存键名 | ||
|  |         :return: 缓存内容信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         cache_value = await request.app.state.redis.get(f'{cache_name}:{cache_key}') | ||
| 
											2 years ago
										 | 
 | ||
| 
											1 year ago
										 |         return CacheInfoModel(cacheKey=cache_key, cacheName=cache_name, cacheValue=cache_value, remark='') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def clear_cache_monitor_cache_name_services(cls, request: Request, cache_name: str): | ||
|  |         """
 | ||
|  |         清除缓存名称对应所有键值service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param cache_name: 缓存名称 | ||
|  |         :return: 操作缓存响应信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         cache_keys = await request.app.state.redis.keys(f'{cache_name}*') | ||
| 
											2 years ago
										 |         if cache_keys: | ||
|  |             await request.app.state.redis.delete(*cache_keys) | ||
|  | 
 | ||
| 
											1 year ago
										 |         return CrudResponseModel(is_success=True, message=f'{cache_name}对应键值清除成功') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def clear_cache_monitor_cache_key_services(cls, request: Request, cache_key: str): | ||
|  |         """
 | ||
|  |         清除缓存名称对应所有键值service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param cache_key: 缓存键名 | ||
|  |         :return: 操作缓存响应信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         cache_keys = await request.app.state.redis.keys(f'*{cache_key}') | ||
| 
											2 years ago
										 |         if cache_keys: | ||
|  |             await request.app.state.redis.delete(*cache_keys) | ||
|  | 
 | ||
| 
											1 year ago
										 |         return CrudResponseModel(is_success=True, message=f'{cache_key}清除成功') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def clear_cache_monitor_all_services(cls, request: Request): | ||
|  |         """
 | ||
|  |         清除所有缓存service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :return: 操作缓存响应信息 | ||
|  |         """
 | ||
|  |         cache_keys = await request.app.state.redis.keys() | ||
|  |         if cache_keys: | ||
|  |             await request.app.state.redis.delete(*cache_keys) | ||
|  | 
 | ||
|  |         await RedisUtil.init_sys_dict(request.app.state.redis) | ||
|  |         await RedisUtil.init_sys_config(request.app.state.redis) | ||
|  | 
 | ||
| 
											1 year ago
										 |         return CrudResponseModel(is_success=True, message='所有缓存清除成功') |