]>
Commit | Line | Data |
---|---|---|
1 | -module(erldis). | |
2 | ||
3 | -compile(export_all). | |
4 | -define(EOL, "\r\n"). | |
5 | ||
6 | %% helpers | |
7 | flatten({error, Message}) -> | |
8 | {error, Message}; | |
9 | flatten(List) when is_list(List)-> | |
10 | lists:flatten(List). | |
11 | ||
12 | %% exposed API | |
13 | connect(Host) -> | |
14 | client:connect(Host). | |
15 | ||
16 | quit(Client) -> | |
17 | client:asend(Client, "QUIT"), | |
18 | client:disconnect(Client). | |
19 | ||
20 | %% Commands operating on string values | |
21 | internal_set_like(Client, Command, Key, Value) -> | |
22 | client:send(Client, Command, [[Key, length(Value)], | |
23 | [Value]]). | |
24 | ||
25 | get_all_results(Client) -> client:get_all_results(Client). | |
26 | ||
27 | set(Client, Key, Value) -> internal_set_like(Client, set, Key, Value). | |
28 | setnx(Client, Key, Value) -> internal_set_like(Client, setnx, Key, Value). | |
29 | incr(Client, Key) -> client:ssend(Client, incr, [Key]). | |
30 | incrby(Client, Key, By) -> client:ssend(Client, incrby, [Key, By]). | |
31 | decr(Client, Key) -> client:ssend(Client, decr, [Key]). | |
32 | decrby(Client, Key, By) -> client:ssend(Client, decrby, [Key, By]). | |
33 | get(Client, Key) -> client:ssend(Client, get, [Key]). | |
34 | mget(Client, Keys) -> client:ssend(Client, mget, Keys). | |
35 | ||
36 | %% Commands operating on every value | |
37 | exists(Client, Key) -> client:ssend(Client, exists, [Key]). | |
38 | del(Client, Key) -> client:ssend(Client, del, [Key]). | |
39 | type(Client, Key) -> client:ssend(Client, type, [Key]). | |
40 | keys(Client, Pattern) -> client:ssend(Client, keys, [Pattern]). | |
41 | randomkey(Client, Key) -> client:ssend(Client, randomkey, [Key]). | |
42 | rename(Client, OldKey, NewKey) -> client:ssend(Client, rename, [OldKey, NewKey]). | |
43 | renamenx(Client, OldKey, NewKey) -> client:ssend(Client, renamenx, [OldKey, NewKey]). | |
44 | ||
45 | %% Commands operating on both lists and sets | |
46 | sort(Client, Key) -> client:ssend(Client, sort, [Key]). | |
47 | sort(Client, Key, Extra) -> client:ssend(Client, sort, [Key, Extra]). | |
48 | ||
49 | %% Commands operating on lists | |
50 | rpush(Client, Key, Value) -> internal_set_like(Client, rpush, Key, Value). | |
51 | lpush(Client, Key, Value) -> internal_set_like(Client, lpush, Key, Value). | |
52 | llen(Client, Key) -> client:ssend(Client, llen, [Key]). | |
53 | lrange(Client, Key, Start, End) -> client:ssend(Client, lrange, [Key, Start, End]). | |
54 | ltrim(Client, Key, Start, End) -> client:ssend(Client, ltrim, [Key, Start, End]). | |
55 | lindex(Client, Key, Index) -> client:ssend(Client, lindex, [Key, Index]). | |
56 | lpop(Client, Key) -> client:ssend(Client, lpop, [Key]). | |
57 | rpop(Client, Key) -> client:ssend(Client, rpop, [Key]). | |
58 | lrem(Client, Key, Number, Value) -> | |
59 | client:send(Client, lrem, [[Key, Number, length(Value)], | |
60 | [Value]]). | |
61 | lset(Client, Key, Index, Value) -> | |
62 | client:send(Client, lset, [[Key, Index, length(Value)], | |
63 | [Value]]). | |
64 | ||
65 | %% Commands operating on sets | |
66 | sadd(Client, Key, Value) -> internal_set_like(Client, sadd, Key, Value). | |
67 | srem(Client, Key, Value) -> internal_set_like(Client, srem, Key, Value). | |
68 | scard(Client, Key) -> client:ssend(Client, scard, [Key]). | |
69 | sismember(Client, Key, Value) -> internal_set_like(Client, sismember, Key, Value). | |
70 | sintersect(Client, Keys) -> client:ssend(Client, sinter, Keys). | |
71 | smembers(Client, Key) -> client:ssend(Client, smembers, [Key]). | |
72 | ||
73 | ||
74 | %% Multiple DB commands | |
75 | flushdb(Client) -> client:ssend(Client, flushdb). | |
76 | flushall(Client) -> client:ssend(Client, flushall). | |
77 | select(Client, Index) -> client:ssend(Client, select, [Index]). | |
78 | move(Client, Key, DBIndex) -> client:ssend(Client, move, [Key, DBIndex]). | |
79 | save(Client) -> client:ssend(Client, save). | |
80 | bgsave(Client) -> client:ssend(Client, bgsave). | |
81 | lastsave(Client) -> client:ssend(Client, lastsave). | |
82 | shutdown(Client) -> client:asend(Client, shutdown). |