summaryrefslogtreecommitdiffhomepage
path: root/source/storage.d
blob: 2cfea93e4f28ae53892402545702d93a6f9fb179 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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, in bool collision = false) {
        isDown;
        if(collision) enforceHTTP(! client.exists(key), HTTPStatus.unprocessableEntity, key ~ " already exists.");
        client.set(key, data);
    }
}