Skip to content

Commit

Permalink
added expiration functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
janpieper committed Feb 5, 2015
1 parent 7b63a72 commit e49df59
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ int main(int argc, char *argv[]) {
qDebug() << "Value for 'key_a':" << cache->fetch("key_a");
qDebug() << "Value for 'key_b':" << cache->fetch("key_b");

cache->store("key_c", "a special value", 1);

qDebug() << "Has 'key_c' before sleep? -" << (cache->has("key_c") ? "yes" : "no");

sleep(2);

qDebug() << "Has 'key_c' after sleep? -" << (cache->has("key_c") ? "yes" : "no");

return 0;
}
```
1 change: 1 addition & 0 deletions abstractadapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Bidstack {

public:
virtual bool store(QString key, QString data) =0;
virtual bool store(QString key, QString data, int ttl) =0;
virtual bool has(QString key) =0;
virtual bool remove(QString key) =0;
virtual bool clear() =0;
Expand Down
5 changes: 5 additions & 0 deletions devnulladapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ 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;
}

Expand Down
1 change: 1 addition & 0 deletions devnulladapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Bidstack {

public:
bool store(QString key, QString data);
bool store(QString key, QString data, int ttl);
bool has(QString key);
bool remove(QString key);
bool clear();
Expand Down
30 changes: 26 additions & 4 deletions sqliteadapter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "sqliteadapter.hpp"

#include <QDateTime>

using namespace Bidstack::Cache;

SqliteAdapter::SqliteAdapter(QString filename, QObject *parent) : AbstractAdapter(parent) {
Expand All @@ -19,7 +21,8 @@ bool SqliteAdapter::init(QString filename) {
const QString sql =
"CREATE TABLE IF NOT EXISTS cache ("
"key CHAR(32) PRIMARY KEY NOT NULL,"
"data TEXT NOT NULL"
"data TEXT NOT NULL,"
"expires_at INTEGER"
")";

QSqlQuery stmt(sql, m_connection);
Expand All @@ -29,12 +32,19 @@ bool SqliteAdapter::init(QString filename) {
}

bool SqliteAdapter::store(QString key, QString data) {
const QString sql = "INSERT OR REPLACE INTO cache (key, data) VALUES (:key, :data)";
return store(key, data, 0);
}

bool SqliteAdapter::store(QString key, QString data, int ttl) {
const QString sql =
"INSERT OR REPLACE INTO cache (key, data, expires_at) "
"VALUES (:key, :data, :expires_at)";

QSqlQuery stmt(m_connection);
stmt.prepare(sql);
stmt.bindValue(":key", key);
stmt.bindValue(":data", data);
stmt.bindValue(":expires_at", now() + ttl);
stmt.exec();

return !stmt.lastError().isValid();
Expand Down Expand Up @@ -66,15 +76,23 @@ bool SqliteAdapter::clear() {
}

QString SqliteAdapter::fetch(QString key) {
const QString sql = "SELECT data FROM cache WHERE key = :key";
const QString sql = "SELECT expires_at, data FROM cache WHERE key = :key";

QSqlQuery stmt(m_connection);
stmt.prepare(sql);
stmt.bindValue(":key", key);
stmt.exec();

if (!stmt.lastError().isValid() && stmt.next()) {
return stmt.value(0).toString();
uint expires_at = stmt.value(0).toUInt();

if ((expires_at == 0) || (expires_at >= now())) {
return stmt.value(1).toString();
}

if (!remove(key)) {
throw "Could not expire key '" + key + "'";
}
}

return "";
Expand All @@ -83,3 +101,7 @@ QString SqliteAdapter::fetch(QString key) {
QString SqliteAdapter::fetch(QString key, QString defaultValue) {
return has(key) ? fetch(key) : defaultValue;
}

uint SqliteAdapter::now() {
return QDateTime::currentDateTime().toTime_t();
}
2 changes: 2 additions & 0 deletions sqliteadapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Bidstack {

public:
bool store(QString key, QString data);
bool store(QString key, QString data, int ttl);
bool has(QString key);
bool remove(QString key);
bool clear();
Expand All @@ -28,6 +29,7 @@ namespace Bidstack {

private:
bool init(QString filename);
uint now();

private:
QSqlDatabase m_connection;
Expand Down

0 comments on commit e49df59

Please sign in to comment.