Guest User

Untitled

a guest
Jul 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. (define *redis* (make-parameter #f))
  2.  
  3. ;; Returns a new connection to the server
  4. (define redis-open open-net-io)
  5.  
  6. ;; Calls the procedure passing a connection to the server.
  7. ;; The connection is closed after proc returns.
  8. (define (with-redis-connection host port proc)
  9. (with-net-io host port
  10. (lambda (in out)
  11. (proc (list in out)))))
  12.  
  13. (define (make-bool value)
  14. (= 1 value))
  15.  
  16. ;; Reads and parses a response from Redis
  17. (define (redis-response in)
  18. (define (drop-crlf) (read-string 2 in))
  19.  
  20. ;; FIXME: error and reply should be different types
  21. (define (single-reply) (string->symbol (read-line in)))
  22.  
  23. (define error-reply single-reply)
  24.  
  25. (define (bulk-reply)
  26. (let ((len (integer-reply)))
  27. (cond ((= -1 len) #f)
  28. (else
  29. (let ((result (read-string len in)))
  30. (drop-crlf)
  31. result)))))
  32.  
  33. (define (multi-bulk-reply)
  34. (let loop ((count (integer-reply))
  35. (result '()))
  36. (cond ((= -1 count) #f)
  37. ((zero? count) (reverse result))
  38. (else (loop (- count 1)
  39. (cons (bulk-reply) result))))))
  40.  
  41. (define (integer-reply)
  42. (string->number (read-line in)))
  43.  
  44. (case (read-char in)
  45. ((#\+) (single-reply))
  46. ((#\-) (error-reply))
  47. ((#\$) (bulk-reply))
  48. ((#\*) (multi-bulk-reply))
  49. ((#\:) (integer-reply))))
  50.  
  51. ;; Sends a multi bulk command
  52. (define (multi-bulk-query out command)
  53. (define (send . args)
  54. (for-each (lambda (s) (display s out)) args))
  55.  
  56. (send "*" (length command) "\r\n")
  57. (for-each (lambda (s) (send "$" (string-length s) "\r\n" s "\r\n"))
  58. command)
  59. (flush-output out))
  60.  
  61. ;; Executes a command and parses the result
  62. (define (redis-command conn command . args)
  63. (multi-bulk-query (cadr conn) (cons command args))
  64. (redis-response (car conn)))
  65.  
  66. (define (redis-select db)
  67. (redis-command (*redis*) "SELECT" db))
  68.  
  69. (define (redis-info)
  70. (let ((info-string (redis-command (*redis*) "INFO")))
  71. info-string ;; TODO: parse
  72. ))
  73.  
  74. (define (redis-flushdb)
  75. (redis-command (*redis*) "FLUSHDB"))
  76.  
  77. (define (redis-save)
  78. (redis-command (*redis*) "SAVE"))
  79.  
  80. (define (redis-savebg)
  81. (redis-command (*redis*) "SAVEBG"))
  82.  
  83. (define (redis-get key)
  84. (redis-command (*redis*) "GET" key))
  85.  
  86. (define (redis-getset key value)
  87. (redis-command (*redis*) "GETSET" key value))
  88.  
  89. (define (redis-mget . keys)
  90. (apply redis-command (*redis*) (cons "MGET" keys)))
  91.  
  92. (define (redis-append key value)
  93. (redis-command (*redis*) "APPEND" key value))
  94.  
  95. (define (redis-substr key start stop)
  96. (redis-command (*redis*) "SUBSTR" key start stop))
  97.  
  98. ;; FIXME: return as hash/assoc
  99. (define (redis-hgetall key)
  100. (redis-command (*redis*) "HGETALL" key))
  101.  
  102. (define (redis-hget key field)
  103. (redis-command (*redis*) "HGET" key field))
  104.  
  105. (define (redis-hdel key field)
  106. (redis-command (*redis*) "HDEL" key field))
  107.  
  108. (define (redis-hkeys key)
  109. (redis-command (*redis*) "HKEYS" key))
  110.  
  111. (define (redis-randomkey)
  112. (redis-command (*redis*) "RANDOMKEY"))
  113.  
  114. (define (redis-echo value)
  115. (redis-command (*redis*) "ECHO" value))
  116.  
  117. (define (redis-ping)
  118. (redis-command (*redis*) "PING"))
  119.  
  120. (define (redis-lastsave)
  121. (redis-command (*redis*) "LASTSAVE"))
  122.  
  123. (define (redis-dbsize)
  124. (redis-command (*redis*) "DBSIZE"))
  125.  
  126. (define (redis-exists key)
  127. (make-bool (redis-command (*redis*) "EXISTS" key)))
  128.  
  129. (define (redis-llen key)
  130. (redis-command (*redis*) "LLEN" key))
  131.  
  132. (define (redis-lrange key start stop)
  133. (redis-command (*redis*) "LRANGE" key start stop))
  134.  
  135. (define (redis-ltrim key start stop)
  136. (redis-command (*redis*) "LTRIM" key start stop))
  137.  
  138. (define (redis-lindex key index)
  139. (redis-command (*redis*) "LINDEX" key index))
  140.  
  141. (define (redis-lset key index value)
  142. (redis-command (*redis*) "LSET" key index value))
  143.  
  144. (define (redis-lrem key count value)
  145. (redis-command (*redis*) "LREM" key count value))
  146.  
  147. (define (redis-rpush key value)
  148. (redis-command (*redis*) "RPUSH" key value))
  149.  
  150. (define (redis-lpush key value)
  151. (redis-command (*redis*) "LPUSH" key value))
  152.  
  153. (define (redis-rpop key)
  154. (redis-command (*redis*) "RPOP" key))
  155.  
  156. (define (redis-lpop key)
  157. (redis-command (*redis*) "LPOP" key))
  158.  
  159. (define (redis-brpop key timeout)
  160. (redis-command (*redis*) "BRPOP" key timeout))
  161.  
  162. (define (redis-blpop key timeout)
  163. (redis-command (*redis*) "BLPOP" key timeout))
  164.  
  165. (define (redis-rpoplpush source destination)
  166. (redis-command (*redis*) "RPOPLPUSH" source destination))
  167.  
  168. (define (redis-smembers key)
  169. (redis-command (*redis*) "SMEMBERS" key))
  170.  
  171. (define (redis-sismember key member)
  172. (make-bool (redis-command (*redis*) "SISMEMBER" key member)))
  173.  
  174. (define (redis-sadd key value)
  175. (make-bool (redis-command (*redis*) "SADD" key value)))
  176.  
  177. (define (redis-srem key value)
  178. (make-bool (redis-command (*redis*) "SREM" key value)))
  179.  
  180. (define (redis-smove source destination value)
  181. (make-bool (redis-command (*redis*) "SMOVE" source destination value)))
  182.  
  183. (define (redis-spop key)
  184. (redis-command (*redis*) "SPOP" key))
  185.  
  186. (define (redis-scard key)
  187. (redis-command (*redis*) "SCARD" key))
  188.  
  189. (define (redis-sinter . keys)
  190. (apply redis-command (*redis*) (cons "SINTER" keys)))
  191.  
  192. (define (redis-sinterstore destination . keys)
  193. (apply redis-command (*redis*) (cons "SINTERSTORE" (cons destination keys))))
  194.  
  195. (define (redis-sunion . keys)
  196. (apply redis-command (*redis*) (cons "SUNION" keys)))
  197.  
  198. (define (redis-sunionstore destination . keys)
  199. (apply redis-command (*redis*) (cons "SUNIONSTORE" (cons destination keys))))
  200.  
  201. (define (redis-sdiff . keys)
  202. (apply redis-command (*redis*) (cons "SDIFF" keys)))
  203.  
  204. (define (redis-sdiffstore destination . keys)
  205. (apply redis-command (*redis*) (cons "SDIFFSTORE" (cons destination keys))))
  206.  
  207. (define (redis-srandmember key)
  208. (redis-command (*redis*) "SRANDMEMBER" key))
  209.  
  210. (define (redis-zadd key score member)
  211. (make-bool (redis-command (*redis*) "ZADD" key score member)))
  212.  
  213. (define (redis-zrank key member)
  214. (redis-command (*redis*) "ZRANK" key member))
  215.  
  216. (define (redis-zrevrank key member)
  217. (redis-command (*redis*) "ZREVRANK" key member))
  218.  
  219. (define (redis-zincrby key increment member)
  220. (redis-command (*redis*) "ZINCRBY" key increment member))
  221.  
  222. (define (redis-zcard key)
  223. (redis-command (*redis*) "ZCARD" key))
  224.  
  225. ;; FIXME: options WITHSCORES
  226. (define (redis-zrange key start stop . options)
  227. (redis-command (*redis*) "ZRANGE" key start stop))
  228.  
  229. ;; FIXME: options LIMIT offset count WITHSCORES
  230. (define (redis-zrangebyscore key min max . options)
  231. (redis-command (*redis*) "ZRANGEBYSCORE" key min max))
  232.  
  233. ;; FIXME: options WITHSCORES
  234. (define (redis-zrevrange key start stop . options)
  235. (redis-command (*redis*) "ZREVRANGE" key start stop))
  236.  
  237. (define (redis-zremrangebyscore key min max)
  238. (redis-command (*redis*) "ZREMRANGEBYSCORE" key min max))
  239.  
  240. (define (redis-zremrangebyrank key start stop)
  241. (redis-command (*redis*) "ZREMRANGEBYRANK" key start stop))
  242.  
  243. (define (redis-zscore key member)
  244. (redis-command (*redis*) "ZSCORE" key))
  245.  
  246. (define (redis-zrem key member)
  247. (make-bool (redis-command (*redis*) "ZREM" key)))
  248.  
  249. ;; FIXME: options [WEIGHTS w1 ... wN] [AGGREGATE SUM|MIN|MAX]
  250. ;; flatten keys param
  251. (define (redis-zinterstore destination keys . options)
  252. (redis-command (*redis*) "ZINTERSTORE" destination (length keys) keys))
  253.  
  254. ;; FIXME: options [WEIGHTS w1 ... wN] [AGGREGATE SUM|MIN|MAX]
  255. ;; flatten keys param
  256. (define (redis-zunionstore destination keys . options)
  257. (redis-command (*redis*) "ZUNIONSTORE" destination (length keys) keys))
  258.  
  259. (define (redis-move key db)
  260. (make-bool (redis-command (*redis*) "MOVE" key db)))
  261.  
  262. (define (redis-setnx key value)
  263. ;; TODO: convert value to string
  264. (make-bool (redis-command (*redis*) "SETNX" key value)))
  265.  
  266. (define (redis-del . keys)
  267. (make-bool (apply redis-command (*redis*) "DEL" keys)))
  268.  
  269. (define (redis-rename oldname newname)
  270. (redis-command (*redis*) "RENAME" oldname newname))
  271.  
  272. (define (redis-renamenx oldname newname)
  273. (make-bool (redis-command (*redis*) "RENAMENX" oldname newname)))
  274.  
  275. (define (redis-expire key seconds)
  276. (make-bool (redis-command (*redis*) "EXPIRE" key seconds)))
  277.  
  278. (define (redis-ttl key)
  279. (redis-command (*redis*) "TTL" key))
  280.  
  281. (define (redis-expireat key time)
  282. (make-bool (redis-command (*redis*) "EXPIREAT" key time)))
  283.  
  284. (define (redis-set key value)
  285. ;; TODO: convert value to string
  286. (redis-command (*redis*) "SET" key value))
Add Comment
Please, Sign In to add comment