#ifndef DBCACHE_H #define DBCACHE_H #include #include #include #include #include #include #include class DBCache : public QObject { Q_OBJECT public: explicit DBCache(QObject *parent = nullptr); ~DBCache(); // 初始化数据库连接 bool initMySQL(const QString &host, int port, const QString &user, const QString &password, const QString &database); bool initRedis(const QString &host, int port, const QString &password = ""); // 执行查询,优先从Redis获取 QVector> query(const QString &sql); // 清除缓存 void clearCache(); private: // Redis连接 redisContext *m_redisContext; // MySQL连接 QSqlDatabase m_mysqlDB; // 生成查询的哈希键 QString generateCacheKey(const QString &sql) const; // 从Redis获取数据 QVector> getFromRedis(const QString &key); // 将数据存入Redis bool setToRedis(const QString &key, const QVector> &data); // 从MySQL查询数据 QVector> queryMySQL(const QString &sql); }; #endif // DBCACHE_H