| | |
| | | DEFAULT_NEW_PASSWORD = 'Baoyi@1341' |
| | | PROTECTED_USER_ID = 1 |
| | | |
| | | # Redis配置 |
| | | REDIS_HOST = '192.168.6.168' |
| | | REDIS_PORT = 6002 |
| | | REDIS_PASSWORD = None |
| | | |
| | | |
| | | class RedisRefresher: |
| | | """Redis缓存刷新器""" |
| | | |
| | | def __init__(self): |
| | | self.redis_client = None |
| | | |
| | | 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 |
| | | ) |
| | | # 测试连接 |
| | | 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 |
| | | |
| | | def refresh_all_redis(self): |
| | | """刷新所有Redis数据库""" |
| | | try: |
| | | if not self.redis_client: |
| | | if not self.connect_redis(): |
| | | return False, "无法连接Redis服务器" |
| | | |
| | | # 获取所有数据库数量 |
| | | try: |
| | | # 尝试获取配置信息 |
| | | config = 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('ping') |
| | | temp_client.send_command('SELECT', db_index) |
| | | temp_client.read_response() |
| | | |
| | | # 获取当前数据库的key数量 |
| | | temp_client.send_command('DBSIZE') |
| | | key_count = temp_client.read_response() |
| | | |
| | | if key_count > 0: |
| | | # 清空当前数据库 |
| | | temp_client.send_command('FLUSHDB') |
| | | temp_client.read_response() |
| | | refreshed_dbs += 1 |
| | | total_keys += key_count |
| | | print(f"已刷新数据库 {db_index}: 清除了 {key_count} 个键") |
| | | |
| | | 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)}" |
| | | |
| | | |
| | | class DatabaseUpdater(QThread): |
| | | """数据库更新器类,运行在单独的线程中""" |
| | |
| | | self.updaters = [] |
| | | self.log_records = [] |
| | | self.is_running = True |
| | | self.redis_refresher = RedisRefresher() |
| | | |
| | | def run(self): |
| | | """主运行方法""" |
| | |
| | | else: |
| | | self.log_signal.emit("无任何更新记录生成") |
| | | |
| | | # 数据库更新完成后,自动刷新Redis缓存 |
| | | self.log_signal.emit("开始刷新Redis缓存...") |
| | | success, message = self.redis_refresher.refresh_all_redis() |
| | | if success: |
| | | self.log_signal.emit(f"✅ {message}") |
| | | else: |
| | | self.log_signal.emit(f"❌ {message}") |
| | | |
| | | except Exception as e: |
| | | self.error_signal.emit(f"运行过程中发生错误: {str(e)}") |
| | | finally: |