diff --git a/README.md b/README.md index 4ed3344..3e6ceed 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ Easy to use cache library written in Qt/C++ #include #include -#include "sqliteadapter.hpp" +#include "sqlitecacheadapter.hpp" -using namespace Bidstack; +using namespace Bidstack::Cache; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); - Cache::SqliteAdapter *cache = new Cache::SqliteAdapter("cache.sqlite"); + SqliteCacheAdapter *cache = new SqliteCacheAdapter("cache.sqlite"); qDebug() << "Has 'key_a'? -" << (cache->has("key_a") ? "yes" : "no"); qDebug() << "Has 'key_b'? -" << (cache->has("key_b") ? "yes" : "no"); diff --git a/abstractadapter.cpp b/abstractadapter.cpp deleted file mode 100644 index 3ad8e88..0000000 --- a/abstractadapter.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "abstractadapter.hpp" - -using namespace Bidstack::Cache; - -AbstractAdapter::AbstractAdapter(QObject *parent) : QObject(parent) { - -} diff --git a/abstractcacheadapter.cpp b/abstractcacheadapter.cpp new file mode 100644 index 0000000..a23a88e --- /dev/null +++ b/abstractcacheadapter.cpp @@ -0,0 +1,7 @@ +#include "abstractcacheadapter.hpp" + +using namespace Bidstack::Cache; + +AbstractCacheAdapter::AbstractCacheAdapter(QObject *parent) : QObject(parent) { + +} diff --git a/abstractadapter.hpp b/abstractcacheadapter.hpp similarity index 72% rename from abstractadapter.hpp rename to abstractcacheadapter.hpp index b6b60dd..396be33 100644 --- a/abstractadapter.hpp +++ b/abstractcacheadapter.hpp @@ -1,16 +1,16 @@ -#ifndef BIDSTACK_CACHE_ABSTRACTADAPTER_HPP -#define BIDSTACK_CACHE_ABSTRACTADAPTER_HPP +#ifndef BIDSTACK_CACHE_ABSTRACTCACHEADAPTER_HPP +#define BIDSTACK_CACHE_ABSTRACTCACHEADAPTER_HPP #include namespace Bidstack { namespace Cache { - class AbstractAdapter : public QObject { + class AbstractCacheAdapter : public QObject { Q_OBJECT public: - AbstractAdapter(QObject *parent = 0); + AbstractCacheAdapter(QObject *parent = 0); public: virtual bool store(QString key, QString data) =0; diff --git a/devnulladapter.cpp b/devnulladapter.cpp deleted file mode 100644 index 676cd9c..0000000 --- a/devnulladapter.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "devnulladapter.hpp" - -using namespace Bidstack::Cache; - -DevNullAdapter::DevNullAdapter(QObject *parent) : AbstractAdapter(parent) { - -} - -bool DevNullAdapter::store(QString key, QString data) { - return store(key, data, 0); -} - -bool DevNullAdapter::store(QString key, QString data, int ttl) { - Q_UNUSED(key); - Q_UNUSED(data); - Q_UNUSED(ttl); - return true; -} - -bool DevNullAdapter::has(QString key) { - Q_UNUSED(key); - return false; -} - -bool DevNullAdapter::remove(QString key) { - Q_UNUSED(key); - return true; -} - -bool DevNullAdapter::clear() { - return true; -} - -QString DevNullAdapter::fetch(QString key) { - Q_UNUSED(key); - return ""; -} - -QString DevNullAdapter::fetch(QString key, QString defaultValue) { - Q_UNUSED(key); - Q_UNUSED(defaultValue); - return defaultValue; -} diff --git a/devnullcacheadapter.cpp b/devnullcacheadapter.cpp new file mode 100644 index 0000000..7b73b15 --- /dev/null +++ b/devnullcacheadapter.cpp @@ -0,0 +1,43 @@ +#include "devnullcacheadapter.hpp" + +using namespace Bidstack::Cache; + +DevNullCacheAdapter::DevNullCacheAdapter(QObject *parent) : AbstractCacheAdapter(parent) { + +} + +bool DevNullCacheAdapter::store(QString key, QString data) { + return store(key, data, 0); +} + +bool DevNullCacheAdapter::store(QString key, QString data, int ttl) { + Q_UNUSED(key); + Q_UNUSED(data); + Q_UNUSED(ttl); + return true; +} + +bool DevNullCacheAdapter::has(QString key) { + Q_UNUSED(key); + return false; +} + +bool DevNullCacheAdapter::remove(QString key) { + Q_UNUSED(key); + return true; +} + +bool DevNullCacheAdapter::clear() { + return true; +} + +QString DevNullCacheAdapter::fetch(QString key) { + Q_UNUSED(key); + return ""; +} + +QString DevNullCacheAdapter::fetch(QString key, QString defaultValue) { + Q_UNUSED(key); + Q_UNUSED(defaultValue); + return defaultValue; +} diff --git a/devnulladapter.hpp b/devnullcacheadapter.hpp similarity index 64% rename from devnulladapter.hpp rename to devnullcacheadapter.hpp index fdbc7c1..6aeb300 100644 --- a/devnulladapter.hpp +++ b/devnullcacheadapter.hpp @@ -1,18 +1,18 @@ -#ifndef BIDSTACK_CACHE_DEVNULLADAPTER_HPP -#define BIDSTACK_CACHE_DEVNULLADAPTER_HPP +#ifndef BIDSTACK_CACHE_DEVNULLCACHEADAPTER_HPP +#define BIDSTACK_CACHE_DEVNULLCACHEADAPTER_HPP #include -#include "abstractadapter.hpp" +#include "abstractcacheadapter.hpp" namespace Bidstack { namespace Cache { - class DevNullAdapter : public AbstractAdapter { + class DevNullCacheAdapter : public AbstractCacheAdapter { Q_OBJECT public: - DevNullAdapter(QObject *parent = 0); + DevNullCacheAdapter(QObject *parent = 0); public: bool store(QString key, QString data); diff --git a/sqliteadapter.cpp b/sqlitecacheadapter.cpp similarity index 77% rename from sqliteadapter.cpp rename to sqlitecacheadapter.cpp index e7cd859..f5b85c1 100644 --- a/sqliteadapter.cpp +++ b/sqlitecacheadapter.cpp @@ -1,16 +1,16 @@ -#include "sqliteadapter.hpp" +#include "sqlitecacheadapter.hpp" #include using namespace Bidstack::Cache; -SqliteAdapter::SqliteAdapter(QString filename, QObject *parent) : AbstractAdapter(parent) { +SqliteCacheAdapter::SqliteCacheAdapter(QString filename, QObject *parent) : AbstractCacheAdapter(parent) { if (!init(filename)) { throw "Could not initialize database connection!"; } } -bool SqliteAdapter::init(QString filename) { +bool SqliteCacheAdapter::init(QString filename) { m_connection = QSqlDatabase::addDatabase("QSQLITE", "bidstack-cache"); m_connection.setDatabaseName(filename); @@ -31,11 +31,11 @@ bool SqliteAdapter::init(QString filename) { return !stmt.lastError().isValid(); } -bool SqliteAdapter::store(QString key, QString data) { +bool SqliteCacheAdapter::store(QString key, QString data) { return store(key, data, 0); } -bool SqliteAdapter::store(QString key, QString data, int ttl) { +bool SqliteCacheAdapter::store(QString key, QString data, int ttl) { const QString sql = "INSERT OR REPLACE INTO cache (key, data, expires_at) " "VALUES (:key, :data, :expires_at)"; @@ -50,11 +50,11 @@ bool SqliteAdapter::store(QString key, QString data, int ttl) { return !stmt.lastError().isValid(); } -bool SqliteAdapter::has(QString key) { +bool SqliteCacheAdapter::has(QString key) { return !fetch(key).isEmpty(); } -bool SqliteAdapter::remove(QString key) { +bool SqliteCacheAdapter::remove(QString key) { const QString sql = "DELETE FROM cache WHERE key = :key"; QSqlQuery stmt(m_connection); @@ -65,7 +65,7 @@ bool SqliteAdapter::remove(QString key) { return !stmt.lastError().isValid(); } -bool SqliteAdapter::clear() { +bool SqliteCacheAdapter::clear() { const QString sql = "DELETE FROM cache"; QSqlQuery stmt(m_connection); @@ -75,7 +75,7 @@ bool SqliteAdapter::clear() { return !stmt.lastError().isValid(); } -QString SqliteAdapter::fetch(QString key) { +QString SqliteCacheAdapter::fetch(QString key) { const QString sql = "SELECT expires_at, data FROM cache WHERE key = :key"; QSqlQuery stmt(m_connection); @@ -98,10 +98,10 @@ QString SqliteAdapter::fetch(QString key) { return ""; } -QString SqliteAdapter::fetch(QString key, QString defaultValue) { +QString SqliteCacheAdapter::fetch(QString key, QString defaultValue) { return has(key) ? fetch(key) : defaultValue; } -uint SqliteAdapter::now() { +uint SqliteCacheAdapter::now() { return QDateTime::currentDateTime().toTime_t(); } diff --git a/sqliteadapter.hpp b/sqlitecacheadapter.hpp similarity index 72% rename from sqliteadapter.hpp rename to sqlitecacheadapter.hpp index ce9c462..4d28770 100644 --- a/sqliteadapter.hpp +++ b/sqlitecacheadapter.hpp @@ -1,5 +1,5 @@ -#ifndef BIDSTACK_CACHE_SQLITEADAPTER_HPP -#define BIDSTACK_CACHE_SQLITEADAPTER_HPP +#ifndef BIDSTACK_CACHE_SQLITECACHEADAPTER_HPP +#define BIDSTACK_CACHE_SQLITECACHEADAPTER_HPP #include #include @@ -7,16 +7,16 @@ #include #include -#include "abstractadapter.hpp" +#include "abstractcacheadapter.hpp" namespace Bidstack { namespace Cache { - class SqliteAdapter : public AbstractAdapter { + class SqliteCacheAdapter : public AbstractCacheAdapter { Q_OBJECT public: - SqliteAdapter(QString filename, QObject *parent = 0); + SqliteCacheAdapter(QString filename, QObject *parent = 0); public: bool store(QString key, QString data);