extra.redis: Replace 'redis-assoc' with the more general 'redis' type (now in the redis vocab), add support for automatic authentification when calling 'with-redis'

db4
Bruno Deferrari 2009-05-11 00:44:09 -03:00
parent 462b66a696
commit 7edcc65159
2 changed files with 34 additions and 30 deletions

View File

@ -1,41 +1,22 @@
! Copyright (C) 2009 Bruno Deferrari
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs io.encodings.8-bit io.sockets
io.streams.duplex kernel redis sequences ;
USING: assocs kernel redis sequences ;
IN: redis.assoc
TUPLE: redis-assoc host port encoding password ;
INSTANCE: redis assoc
CONSTANT: default-redis-port 6379
M: redis at* [ redis-get dup >boolean ] with-redis ;
: <redis-assoc> ( -- redis-assoc )
redis-assoc new
"127.0.0.1" >>host
default-redis-port >>port
latin1 >>encoding ;
M: redis assoc-size [ redis-dbsize ] with-redis ;
INSTANCE: redis-assoc assoc
M: redis >alist [ "*" redis-keys dup redis-mget zip ] with-redis ;
: with-redis-assoc ( redis-assoc quot -- )
[
[ host>> ] [ port>> ] [ encoding>> ] tri
[ <inet> ] dip <client> drop
] dip with-stream ; inline
M: redis set-at [ redis-set drop ] with-redis ;
M: redis-assoc at* [ redis-get dup >boolean ] with-redis-assoc ;
M: redis delete-at [ redis-del drop ] with-redis ;
M: redis-assoc assoc-size [ redis-dbsize ] with-redis-assoc ;
M: redis clear-assoc [ "*" redis-keys [ redis-del drop ] each ] with-redis ;
M: redis-assoc >alist
[ "*" redis-keys dup redis-mget zip ] with-redis-assoc ;
M: redis equal? assoc= ;
M: redis-assoc set-at [ redis-set drop ] with-redis-assoc ;
M: redis-assoc delete-at [ redis-del drop ] with-redis-assoc ;
M: redis-assoc clear-assoc
[ "*" redis-keys [ redis-del drop ] each ] with-redis-assoc ;
M: redis-assoc equal? assoc= ;
M: redis-assoc hashcode* assoc-hashcode ;
M: redis hashcode* assoc-hashcode ;

View File

@ -1,6 +1,8 @@
! Copyright (C) 2009 Bruno Deferrari
! See http://factorcode.org/license.txt for BSD license.
USING: io redis.response-parser redis.command-writer splitting ;
USING: accessors io io.encodings.8-bit io.sockets
io.streams.duplex kernel redis.command-writer
redis.response-parser splitting ;
IN: redis
#! Connection
@ -72,3 +74,24 @@ IN: redis
#! Remote server control
: redis-info ( -- response ) info flush read-response ;
: redis-monitor ( -- response ) monitor flush read-response ;
#! Redis object
TUPLE: redis host port encoding password ;
CONSTANT: default-redis-port 6379
: <redis> ( -- redis )
redis new
"127.0.0.1" >>host
default-redis-port >>port
latin1 >>encoding ;
: redis-do-connect ( redis -- stream )
[ host>> ] [ port>> ] [ encoding>> ] tri
[ <inet> ] dip <client> drop ;
: with-redis ( redis quot -- )
[
[ redis-do-connect ] [ password>> ] bi
[ swap [ [ redis-auth drop ] with-stream* ] keep ] when*
] dip with-stream ; inline