diff options
Diffstat (limited to 'source/storage.d')
| -rw-r--r-- | source/storage.d | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/source/storage.d b/source/storage.d new file mode 100644 index 0000000..730c5ab --- /dev/null +++ b/source/storage.d @@ -0,0 +1,41 @@ +module brpaste.storage; + +import vibe.vibe; + +class RedisStorage { + private RedisDatabase client; + + this(URL url = URL("redis://127.0.0.1")) { + client = connectRedisDB(url); + } + + void isDown() { + enforceHTTP(healthy, HTTPStatus.serviceUnavailable, "Redis is down."); + } + + bool healthy() { + try { + client.client.ping; + } catch (Exception e) { + return false; + } + return true; + } + + auto get(in string key) { + isDown; + enforceHTTP(client.exists(key), HTTPStatus.notFound, key ~ " not found."); + return client.get(key); + } + + void put(in string key, in string data) { + isDown; + client.set(key, data); + } + + void putCollision(in string key, in string data) { + isDown; + enforceHTTP(! client.exists(key), HTTPStatus.unprocessableEntity, key ~ " already exists."); + put(key, data); + } +} |
