diff --git a/extra/redis/assoc/assoc.factor b/extra/redis/assoc/assoc.factor new file mode 100644 index 0000000000..e8bdbbb935 --- /dev/null +++ b/extra/redis/assoc/assoc.factor @@ -0,0 +1,22 @@ +! Copyright (C) 2009 Bruno Deferrari +! See http://factorcode.org/license.txt for BSD license. +USING: assocs kernel redis sequences ; +IN: redis.assoc + +INSTANCE: redis assoc + +M: redis at* [ redis-get dup >boolean ] with-redis ; + +M: redis assoc-size [ redis-dbsize ] with-redis ; + +M: redis >alist [ "*" redis-keys dup redis-mget zip ] with-redis ; + +M: redis set-at [ redis-set drop ] with-redis ; + +M: redis delete-at [ redis-del drop ] with-redis ; + +M: redis clear-assoc [ redis-flushdb drop ] with-redis ; + +M: redis equal? assoc= ; + +M: redis hashcode* assoc-hashcode ; diff --git a/extra/redis/assoc/authors.txt b/extra/redis/assoc/authors.txt new file mode 100644 index 0000000000..f4a8cb1dc2 --- /dev/null +++ b/extra/redis/assoc/authors.txt @@ -0,0 +1 @@ +Bruno Deferrari diff --git a/extra/redis/assoc/summary.txt b/extra/redis/assoc/summary.txt new file mode 100644 index 0000000000..72a76ab9f0 --- /dev/null +++ b/extra/redis/assoc/summary.txt @@ -0,0 +1 @@ +Assoc protocol implementation for Redis diff --git a/extra/redis/redis.factor b/extra/redis/redis.factor index 1f6d732407..466fdc9937 100644 --- a/extra/redis/redis.factor +++ b/extra/redis/redis.factor @@ -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 ; +USING: accessors io io.encodings.8-bit io.sockets +io.streams.duplex kernel redis.command-writer +redis.response-parser splitting ; IN: redis #! Connection @@ -23,7 +25,7 @@ IN: redis : redis-type ( key -- response ) type flush read-response ; #! Key space -: redis-keys ( pattern -- response ) keys flush read-response ; +: redis-keys ( pattern -- response ) keys flush read-response " " split ; : redis-randomkey ( -- response ) randomkey flush read-response ; : redis-rename ( newkey key -- response ) rename flush read-response ; : redis-renamenx ( newkey key -- response ) renamenx flush read-response ; @@ -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 new + "127.0.0.1" >>host + default-redis-port >>port + latin1 >>encoding ; + +: redis-do-connect ( redis -- stream ) + [ host>> ] [ port>> ] [ encoding>> ] tri + [ ] dip drop ; + +: with-redis ( redis quot -- ) + [ + [ redis-do-connect ] [ password>> ] bi + [ swap [ [ redis-auth drop ] with-stream* ] keep ] when* + ] dip with-stream ; inline