diff options
Diffstat (limited to 'board.cpp')
-rw-r--r-- | board.cpp | 37 |
1 files changed, 33 insertions, 4 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() |