Commits

Nikhil Marathe committed 0b787d7

Added basic redis implementation.

This supports connecting to and sending messages to the server.
It can handle single line replies and return to the caller.
It's roughly based on fictorial's redis-node-client (http://github.com/fictorial/redis-node-client).
I will be attempting to use macro's for function generation

Comments (0)

Files changed (1)

+;; @module redis
+;; @description newLISP bindings for the Redis database ( http://code.google.com/p/redis )
+;; @version 0.1
+;; @author Nikhil Marathe <nsm.nikhil@gmail.com> 2010
+;; @homepage @link http://bitbucket.org/nikhilm/newlisp-redis http://bitbucket.org/nikhilm/newlisp-redis
+;;
+;; This module provides bindings to Redis. It communicates
+;; with the Redis server and returns values in appropriate
+;; lisp-y types.
+;;
+;; @example
+;; (setf 'redis (redis:connect))
+;; (redis:ping redis)
+;; => "+PONG"
+
+(context 'redis)
+
+(define (split str)
+  (parse str redis:CRLF))
+
+(context MAIN)
+
+;; @syntax (redis <host> <port>)
+;; Creates a new connection to a Redis instance
+;; @return a new connection object or nil on error
+(define (redis:redis (host "localhost") (port 6379))
+  (let (sock (net-connect host port))
+    (if (nil? sock)
+      nil
+      (list redis sock ""))))
+
+(define redis:CRLF "\r\n")
+(define redis:BUFSIZE 4096)
+
+(define (redis:query r command-str)
+  (net-send (r 1) (append command-str redis:CRLF))
+  (net-receive (r 1) buf redis:BUFSIZE)
+  (write-buffer (r 2) buf)
+  (:handle-reply r))
+
+(define (redis:handle-reply r , reply)
+  (while (> (length (r 2)) 0)
+    (setf reply
+        (case (first (r 2))
+          ("+" (:handle-single-reply r))
+          ("-" (:handle-error r))
+          ("$" (:handle-bulk-reply r))
+          ("*" (:handle-multi-bulk-reply r))
+          (":" (:handle-integer-reply r))
+          (true (:handle-unknown-reply r))))
+    (setf (r 2) ((reply 1) (r 2)))
+    (reply 0)))
+
+(define (redis:handle-single-reply r)
+  (let (pos (find redis:CRLF (r 2)))
+    (if (nil? pos)
+      nil
+      (list (0 pos (r 2)) (+ pos (length redis:CRLF))))))
+
+(define (redis:ping r)
+  (:query r "PING"))
+
+(define (redis:bgsave r)
+  (:query r "BGSAVE"))