diff options
-rw-r--r-- | board.cpp | 37 | ||||
-rw-r--r-- | board.h | 12 | ||||
-rw-r--r-- | global.h | 3 |
3 files changed, 46 insertions, 6 deletions
@@ -72,6 +72,14 @@ void Board::enqueueAction(Action *action) QString Board::getConfig(const QString &key) const { + // Try config cache first + QHash<QString, QString>::const_iterator i = _config.find(key); + if (i != _config.end()) { + // Cache hit + return i.value(); + } + + // Try database QSqlQuery query(_db); query.prepare("SELECT key, value FROM config WHERE key = :key"); query.bindValue(":key", key); @@ -80,13 +88,27 @@ QString Board::getConfig(const QString &key) const return QString(); } if (query.next()) { - return query.value(1).toString(); + QString value = query.value(1).toString(); + _config[key] = value; // Store in cache + return value; } return QString(); } void Board::setConfig(const QString &key, const QString &value) { + + // Try config cache first + QHash<QString, QString>::const_iterator i = _config.find(key); + if (i != _config.end()) { + QString old_value = i.value(); + if (old_value == value) { + // It's in the cache, and it's the same value: don't change. + return; + } + } + + // Update value in DB QSqlQuery query(_db); query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :value)"); query.bindValue(":key", key); @@ -94,7 +116,8 @@ void Board::setConfig(const QString &key, const QString &value) if (!query.exec()) { qWarning() << "Could not set configuration key" << key << ":" << query.lastError().text(); } - notifyConfigChanged(); + _config.insert(key, value); + notifyConfigChanged(key); } QString Board::removeHtml(QString text) const @@ -141,9 +164,15 @@ QString Board::renderHumanTime(const QDateTime &dateTime) return dateTime.toLocalTime().time().toString(Qt::DefaultLocaleShortDate); } -void Board::notifyConfigChanged() +void Board::notifyConfigChanged(const QString& key) { - emit configChanged(); + if (!key.isEmpty()) { + _config.remove(key); + } else { + // Must assume all keys were changed + _config.clear(); + } + emit configChanged(key); } void Board::notifyForumsChanged() @@ -2,6 +2,7 @@ #define BOARD_H #include <QtCore/QDateTime> +#include <QtCore/QHash> #include <QtCore/QObject> #include <QtCore/QPair> #include <QtCore/QQueue> @@ -38,14 +39,16 @@ public: QString renderHumanTime(const QDateTime& dateTime); // These functions wrap emitting the signals below - void notifyConfigChanged(); + void notifyConfigChanged(const QString& key = QString()); void notifyForumsChanged(); + void notifySmiliesChanged(); void notifyForumTopicsChanged(int forumId, int start, int end); void notifyTopicPostsChanged(int topicId, int start, int end); signals: - void configChanged(); + void configChanged(const QString& key); void forumsChanged(); + void smiliesChanged(); void forumTopicsChanged(int forumId, int start, int end); void topicPostsChanged(int topicId, int start, int end); @@ -60,8 +63,10 @@ private: bool removeFromActionQueue(Action *action); void executeActionFromQueue(); void initializeBbCode(); + void initializeSmilies(); void fetchConfigIfOutdated(); void fetchForumsIfOutdated(); + void fetchSmiliesIfOutdated(); private slots: void handleActionFinished(Action *action); @@ -73,7 +78,10 @@ private: QSqlDatabase _db; XmlRpcInterface *_iface; QQueue<Action*> _queue; + /** Configuration cache */ + mutable QHash<QString, QString> _config; QList< QPair<QRegExp, QString> > _bbcodes; + QList< QPair<QString, QString> > _smilies; }; inline bool Board::busy() const @@ -6,6 +6,9 @@ /** Time the forum config settings should be considered up to date, in days. */ #define BOARD_CONFIG_TTL 2 +/** Time the forum smilies list should be considered up to date, in days. */ +#define SMILIES_LIST_TTL BOARD_CONFIG_TLL + /** Time the list of forums should be considered up to date, in days. */ #define BOARD_LIST_TTL 2 |