]> gitweb.factorcode.org Git - factor.git/blob - extra/redis/redis.factor
98e89dca535cbfa656b71ad5ebe17c0b6803ec35
[factor.git] / extra / redis / redis.factor
1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar io io.sockets io.streams.duplex
4 io.timeouts kernel redis.command-writer redis.response-parser
5 io.encodings.utf8 ;
6 IN: redis
7
8 ! Connection
9 : redis-quit ( -- ) quit flush ;
10 : redis-ping ( -- response ) ping flush read-response ;
11 : redis-auth ( password -- response ) auth flush read-response ;
12
13 ! String values
14 : redis-set ( value key -- ) set flush check-response ;
15 : redis-get ( key -- response ) get flush read-response ;
16 : redis-getset ( value key -- response ) getset flush read-response ;
17 : redis-mget ( keys -- response ) mget flush read-response ;
18 : redis-setnx ( value key -- response ) setnx flush read-response ;
19 : redis-incr ( key -- response ) incr flush read-response ;
20 : redis-incrby ( integer key -- response ) incrby flush read-response ;
21 : redis-decr ( key -- response ) decr flush read-response ;
22 : redis-decrby ( integer key -- response ) decrby flush read-response ;
23 : redis-exists ( key -- response ) exists flush read-response ;
24 : redis-del ( key -- response ) del flush read-response ;
25 : redis-type ( key -- response ) type flush read-response ;
26
27 ! Key space
28 : redis-keys ( pattern -- response ) keys flush read-response ;
29 : redis-randomkey ( -- response ) randomkey flush read-response ;
30 : redis-rename ( newkey key -- response ) rename flush read-response ;
31 : redis-renamenx ( newkey key -- response ) renamenx flush read-response ;
32 : redis-dbsize ( -- response ) dbsize flush read-response ;
33 : redis-expire ( integer key -- response ) expire flush read-response ;
34
35 ! Lists
36 : redis-rpush ( value key -- response ) rpush flush read-response ;
37 : redis-lpush ( value key -- response ) lpush flush read-response ;
38 : redis-llen ( key -- response ) llen flush read-response ;
39 : redis-lrange ( start end key -- response ) lrange flush read-response ;
40 : redis-ltrim ( start end key -- ) ltrim flush check-response ;
41 : redis-lindex ( integer key -- response ) lindex flush read-response ;
42 : redis-lset ( value index key -- ) lset flush check-response ;
43 : redis-lrem ( value amount key -- response ) lrem flush read-response ;
44 : redis-lpop ( key -- response ) lpop flush read-response ;
45 : redis-rpop ( key -- response ) rpop flush read-response ;
46
47 ! Sets
48 : redis-sadd ( member key -- response ) sadd flush read-response ;
49 : redis-srem  ( member key -- response ) srem flush read-response ;
50 : redis-smove ( member newkey key -- response ) smove flush read-response ;
51 : redis-scard ( key -- response ) scard flush read-response ;
52 : redis-sismember ( member key -- response ) sismember flush read-response ;
53 : redis-sinter ( keys -- response ) sinter flush read-response ;
54 : redis-sinterstore ( keys destkey -- response ) sinterstore flush read-response ;
55 : redis-sunion ( keys -- response ) sunion flush read-response ;
56 : redis-sunionstore ( keys destkey -- response ) sunionstore flush read-response ;
57 : redis-smembers ( key -- response ) smembers flush read-response ;
58
59 ! Hashes
60 : redis-hdel ( field key -- response ) hdel flush read-response ;
61 : redis-hexists ( field key -- response ) hexists flush read-response ;
62 : redis-hget ( field key -- response ) hget flush read-response ;
63 : redis-hgetall ( key -- response ) hgetall flush read-response ;
64 : redis-hincrby ( integer field key -- response ) hincrby flush read-response ;
65 : redis-hincrbyfloat (  float field key -- response ) hincrbyfloat flush read-response ;
66 : redis-hkeys ( key -- response ) hkeys flush read-response ;
67 : redis-hlen ( key -- response ) hlen flush read-response ;
68 : redis-hmget ( seq key  -- response ) hmget flush read-response ;
69 : redis-hmset ( assoc key -- ) hmset flush check-response ;
70 : redis-hset ( value field key -- response ) hset flush read-response ;
71 : redis-hsetnx ( value field key -- response ) hsetnx flush read-response ;
72 : redis-hvals ( key -- response ) hvals flush read-response ;
73
74 ! Multiple db
75 : redis-select ( integer -- ) select flush check-response ;
76 : redis-move ( integer key -- response ) move flush read-response ;
77 : redis-flushdb ( -- ) flushdb flush check-response ;
78 : redis-flushall ( -- ) flushall flush check-response ;
79
80 ! Sorting
81 ! sort
82
83 ! Persistence control
84 : redis-save ( -- ) save flush check-response ;
85 : redis-bgsave ( -- ) bgsave flush check-response ;
86 : redis-lastsave ( -- response ) lastsave flush read-response ;
87 : redis-shutdown ( -- ) shutdown flush check-response ;
88
89 ! Remote server control
90 : redis-info ( -- response ) info flush read-response ;
91 : redis-monitor ( -- response ) monitor flush read-response ;
92
93 ! Redis object
94 TUPLE: redis host port encoding password ;
95
96 CONSTANT: default-redis-port 6379
97
98 : <redis> ( -- redis )
99     redis new
100         "127.0.0.1" >>host
101         default-redis-port >>port
102         utf8 >>encoding ;
103
104 : redis-do-connect ( redis -- stream )
105     [ host>> ] [ port>> ] [ encoding>> ] tri
106     [ <inet> ] dip <client> drop
107     1 minutes over set-timeout ;
108
109 : with-redis ( redis quot -- )
110     [
111         [ redis-do-connect ] [ password>> ] bi
112         [ swap [ [ redis-auth drop ] with-stream* ] keep ] when*
113     ] dip with-stream ; inline