| | |
| | | USER_COUNT = -1 # 每库更新前 N 个用户,-1 表示全部 |
| | | PROTECTED_USER_ID = 1 # 需要排除的用户ID |
| | | |
| | | # Redis配置 |
| | | REDIS_HOST = '192.168.6.168' |
| | | REDIS_PORT = 6002 |
| | | REDIS_PASSWORD = None |
| | | |
| | | # === 日志列表与锁 === |
| | | log_records = [] |
| | | log_lock = asyncio.Lock() |
| | | |
| | | |
| | | class RedisRefresher: |
| | | """Redis缓存刷新器""" |
| | | |
| | | def __init__(self): |
| | | self.redis_client = None |
| | | |
| | | async def connect_redis(self): |
| | | """连接Redis服务器""" |
| | | try: |
| | | # 尝试导入redis库 |
| | | import redis |
| | | self.redis_client = redis.Redis( |
| | | host=REDIS_HOST, |
| | | port=REDIS_PORT, |
| | | password=REDIS_PASSWORD, |
| | | decode_responses=True |
| | | ) |
| | | # 测试连接 |
| | | await asyncio.to_thread(self.redis_client.ping) |
| | | return True |
| | | except ImportError: |
| | | print("错误: 未安装redis库,请使用 'pip install redis' 安装") |
| | | return False |
| | | except Exception as e: |
| | | print(f"连接Redis失败: {str(e)}") |
| | | return False |
| | | |
| | | async def refresh_all_redis(self): |
| | | """刷新所有Redis数据库""" |
| | | try: |
| | | if not self.redis_client: |
| | | if not await self.connect_redis(): |
| | | return False, "无法连接Redis服务器" |
| | | |
| | | # 获取所有数据库数量 |
| | | try: |
| | | # 尝试获取配置信息 |
| | | config = await asyncio.to_thread(self.redis_client.config_get, 'databases') |
| | | db_count = int(config.get('databases', 16)) |
| | | except: |
| | | # 如果无法获取配置,默认使用16个数据库 |
| | | db_count = 16 |
| | | |
| | | refreshed_dbs = 0 |
| | | total_keys = 0 |
| | | |
| | | # 遍历所有数据库并刷新 |
| | | for db_index in range(db_count): |
| | | try: |
| | | # 切换到指定数据库 |
| | | temp_client = self.redis_client.connection_pool.get_connection() |
| | | await asyncio.to_thread(temp_client.send_command, 'SELECT', db_index) |
| | | await asyncio.to_thread(temp_client.read_response) |
| | | |
| | | # 获取当前数据库的key数量 |
| | | await asyncio.to_thread(temp_client.send_command, 'DBSIZE') |
| | | key_count = await asyncio.to_thread(temp_client.read_response) |
| | | |
| | | if key_count > 0: |
| | | # 清空当前数据库 |
| | | await asyncio.to_thread(temp_client.send_command, 'FLUSHDB') |
| | | await asyncio.to_thread(temp_client.read_response) |
| | | refreshed_dbs += 1 |
| | | total_keys += key_count |
| | | print(f"已刷新数据库 {db_index}: 清除了 {key_count} 个键") |
| | | |
| | | await asyncio.to_thread(self.redis_client.connection_pool.release, temp_client) |
| | | |
| | | except Exception as e: |
| | | print(f"刷新数据库 {db_index} 时出错: {str(e)}") |
| | | continue |
| | | |
| | | return True, f"成功刷新 {refreshed_dbs} 个Redis数据库,共清除 {total_keys} 个键" |
| | | |
| | | except Exception as e: |
| | | return False, f"刷新Redis缓存时发生错误: {str(e)}" |
| | | |
| | | |
| | | def sanitize_filename(name: str) -> str: |
| | |
| | | else: |
| | | print("⚠️ 无任何更新记录生成") |
| | | |
| | | # 数据库更新完成后,自动刷新Redis缓存 |
| | | print("\n开始刷新Redis缓存...") |
| | | redis_refresher = RedisRefresher() |
| | | success, message = await redis_refresher.refresh_all_redis() |
| | | if success: |
| | | print(f"✅ {message}") |
| | | else: |
| | | print(f"❌ {message}") |
| | | |
| | | |
| | | # === 启动入口 === |
| | | if __name__ == '__main__': |