PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/client-libraries/erlang/src/erldis.erl

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