refs #684 Added parametrised cache value key strings.

This commit is contained in:
Mathew Sutcliffe
2016-09-09 23:21:00 +01:00
committed by Roland Winklmeier
parent aeb15ea7be
commit 898b8e4078
3 changed files with 35 additions and 7 deletions

View File

@@ -292,9 +292,9 @@ namespace BlackMisc
CData(T *owner) :
CData::CCached(CDataCache::instance(), Trait::key(), Trait::humanReadable(), Trait::isValid, Trait::defaultValue(), owner)
{
if (Trait::timeToLive() >= 0) { CDataCache::instance()->setTimeToLive(Trait::key(), Trait::timeToLive()); }
if (Trait::isPinned()) { CDataCache::instance()->pinValue(Trait::key()); }
if (Trait::isDeferred()) { CDataCache::instance()->deferValue(Trait::key()); }
if (Trait::timeToLive() >= 0) { CDataCache::instance()->setTimeToLive(this->getKey(), Trait::timeToLive()); }
if (Trait::isPinned()) { CDataCache::instance()->pinValue(this->getKey()); }
if (Trait::isDeferred()) { CDataCache::instance()->deferValue(this->getKey()); }
static_assert(! (Trait::isPinned() && Trait::isDeferred()), "trait can not be both pinned and deferred");
}
@@ -311,14 +311,14 @@ namespace BlackMisc
//! \copydoc BlackMisc::CCached::set
CStatusMessage set(const typename Trait::type &value, qint64 timestamp = 0)
{
CDataCache::instance()->admitValue(Trait::key(), false);
CDataCache::instance()->admitValue(this->getKey(), false);
return CCached<typename Trait::type>::set(value, timestamp);
}
//! \copydoc BlackMisc::CCached::setProperty
CStatusMessage setProperty(const CPropertyIndex &index, const CVariant &value, qint64 timestamp = 0)
{
CDataCache::instance()->admitValue(Trait::key(), false);
CDataCache::instance()->admitValue(this->getKey(), false);
return CCached<typename Trait::type>::setProperty(index, value, timestamp);
}
@@ -342,7 +342,7 @@ namespace BlackMisc
}
//! If the value is load-deferred, trigger the deferred load (async).
void admit() { if (Trait::isDeferred()) { CDataCache::instance()->admitValue(Trait::key(), true); } }
void admit() { if (Trait::isDeferred()) { CDataCache::instance()->admitValue(this->getKey(), true); } }
//! If the value is currently being loaded, wait for it to finish loading, and call the notification slot, if any.
void synchronize()

View File

@@ -19,6 +19,7 @@
#include "blackmisc/logmessage.h"
#include <QByteArray>
#include <QCoreApplication>
#include <QDBusMetaType>
#include <QDir>
#include <QFile>
@@ -554,8 +555,14 @@ namespace BlackMisc
bool m_saved = false;
};
CValuePage::Element &CValuePage::createElement(const QString &key, const QString &name, int metaType, Validator validator, const CVariant &defaultValue)
CValuePage::Element &CValuePage::createElement(const QString &keyTemplate, const QString &name, int metaType, Validator validator, const CVariant &defaultValue)
{
auto *category = parent()->findChild<CValueCacheCategory *>();
QString key = keyTemplate;
key.replace("%Application%", QFileInfo(QCoreApplication::applicationFilePath()).completeBaseName(), Qt::CaseInsensitive);
key.replace("%OwnerClass%", QString(parent()->metaObject()->className()).replace("::", "/"), Qt::CaseInsensitive);
key.replace("%OwnerCategory%", category ? category->getCategory() : QString(parent()->metaObject()->className()).replace("::", "/"), Qt::CaseInsensitive);
Q_ASSERT_X(! m_elements.contains(key), "CValuePage", "Can't have two CCached in the same object referring to the same value");
Q_ASSERT_X(defaultValue.isValid() ? defaultValue.userType() == metaType : true, "CValuePage", "Metatype mismatch for default value");
Q_ASSERT_X(defaultValue.isValid() && validator ? validator(defaultValue) : true, "CValuePage", "Validator rejects default value");

View File

@@ -433,6 +433,27 @@ namespace BlackMisc
Private::CValuePage::Element &m_element; //!< \private
};
/*!
* Helper class for generating cache key variations from a key template.
*/
class BLACKMISC_EXPORT CValueCacheCategory : public QObject
{
Q_OBJECT
public:
//! Constructor.
CValueCacheCategory(QObject *parent, const QString &category) : QObject(parent), m_category(category)
{
Q_ASSERT_X(parent->findChildren<CValueCacheCategory *>().size() == 1, Q_FUNC_INFO, "Only one CValueCacheCategory per object allowed");
}
//! Get category string.
const QString &getCategory() const { return m_category; }
private:
const QString m_category;
};
/*!
* RAII object returned by CValueCache::batchChanges. Applies deferred changes when it is destroyed.
*/