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.
		
		
		
		
			
				
					78 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					78 lines
				
				2.4 KiB
			| 
											2 years ago
										 | from redis import asyncio as aioredis | ||
|  | from redis.exceptions import AuthenticationError, TimeoutError, RedisError | ||
| 
											1 year ago
										 | from config.database import AsyncSessionLocal | ||
| 
											1 year ago
										 | from config.env import RedisConfig | ||
|  | from module_admin.service.config_service import ConfigService | ||
|  | from module_admin.service.dict_service import DictDataService | ||
| 
											2 years ago
										 | from utils.log_util import logger | ||
|  | 
 | ||
|  | 
 | ||
|  | class RedisUtil: | ||
|  |     """
 | ||
|  |     Redis相关方法 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def create_redis_pool(cls) -> aioredis.Redis: | ||
|  |         """
 | ||
|  |         应用启动时初始化redis连接 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :return: Redis连接对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         logger.info('开始连接redis...') | ||
| 
											2 years ago
										 |         redis = await aioredis.from_url( | ||
| 
											1 year ago
										 |             url=f'redis://{RedisConfig.redis_host}', | ||
| 
											2 years ago
										 |             port=RedisConfig.redis_port, | ||
|  |             username=RedisConfig.redis_username, | ||
|  |             password=RedisConfig.redis_password, | ||
|  |             db=RedisConfig.redis_database, | ||
| 
											1 year ago
										 |             encoding='utf-8', | ||
|  |             decode_responses=True, | ||
| 
											2 years ago
										 |         ) | ||
| 
											2 years ago
										 |         try: | ||
|  |             connection = await redis.ping() | ||
|  |             if connection: | ||
| 
											1 year ago
										 |                 logger.info('redis连接成功') | ||
| 
											2 years ago
										 |             else: | ||
| 
											1 year ago
										 |                 logger.error('redis连接失败') | ||
| 
											2 years ago
										 |         except AuthenticationError as e: | ||
| 
											1 year ago
										 |             logger.error(f'redis用户名或密码错误,详细错误信息:{e}') | ||
| 
											2 years ago
										 |         except TimeoutError as e: | ||
| 
											1 year ago
										 |             logger.error(f'redis连接超时,详细错误信息:{e}') | ||
| 
											2 years ago
										 |         except RedisError as e: | ||
| 
											1 year ago
										 |             logger.error(f'redis连接错误,详细错误信息:{e}') | ||
| 
											2 years ago
										 |         return redis | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def close_redis_pool(cls, app): | ||
|  |         """
 | ||
|  |         应用关闭时关闭redis连接 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param app: fastapi对象 | ||
|  |         :return: | ||
|  |         """
 | ||
|  |         await app.state.redis.close() | ||
| 
											1 year ago
										 |         logger.info('关闭redis连接成功') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def init_sys_dict(cls, redis): | ||
|  |         """
 | ||
|  |         应用启动时缓存字典表 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param redis: redis对象 | ||
|  |         :return: | ||
|  |         """
 | ||
| 
											1 year ago
										 |         async with AsyncSessionLocal() as session: | ||
|  |             await DictDataService.init_cache_sys_dict_services(session, redis) | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def init_sys_config(cls, redis): | ||
|  |         """
 | ||
|  |         应用启动时缓存参数配置表 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param redis: redis对象 | ||
|  |         :return: | ||
|  |         """
 | ||
| 
											1 year ago
										 |         async with AsyncSessionLocal() as session: | ||
|  |             await ConfigService.init_cache_sys_config_services(session, redis) |