]> gitweb.factorcode.org Git - factor.git/blob - extra/redis/redis.factor
Merge branch 'redis' of git://www.tiodante.com/git/factor
[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 io io.encodings.8-bit io.sockets
4 io.streams.duplex kernel redis.command-writer
5 redis.response-parser splitting ;
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 -- response ) set flush read-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 " " split ;
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 -- response ) ltrim flush read-response ;
41 : redis-lindex ( integer key -- response ) lindex flush read-response ;
42 : redis-lset ( value index key -- response ) lset flush read-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 #! Multiple db
60 : redis-select ( integer -- response ) select flush read-response ;
61 : redis-move ( integer key -- response ) move flush read-response ;
62 : redis-flushdb ( -- response ) flushdb flush read-response ;
63 : redis-flushall ( -- response ) flushall flush read-response ;
64
65 #! Sorting
66 ! sort
67
68 #! Persistence control
69 : redis-save ( -- response ) save flush read-response ;
70 : redis-bgsave ( -- response ) bgsave flush read-response ;
71 : redis-lastsave ( -- response ) lastsave flush read-response ;
72 : redis-shutdown ( -- response ) shutdown flush read-response ;
73
74 #! Remote server control
75 : redis-info ( -- response ) info flush read-response ;
76 : redis-monitor ( -- response ) monitor flush read-response ;
77
78 #! Redis object
79 TUPLE: redis host port encoding password ;
80
81 CONSTANT: default-redis-port 6379
82
83 : <redis> ( -- redis )
84     redis new
85         "127.0.0.1" >>host
86         default-redis-port >>port
87         latin1 >>encoding ;
88
89 : redis-do-connect ( redis -- stream )
90     [ host>> ] [ port>> ] [ encoding>> ] tri
91     [ <inet> ] dip <client> drop ;
92
93 : with-redis ( redis quot -- )
94     [
95         [ redis-do-connect ] [ password>> ] bi
96         [ swap [ [ redis-auth drop ] with-stream* ] keep ] when*
97     ] dip with-stream ; inline