1. Alvaro Videla
  2. redis-haskell

Source

redis-haskell / Database / Redis / Lists.hs

module Database.Redis.Lists where

import Database.Redis.Protocol
import Database.Redis.Key
import Database.Redis.Serializable
import System.IO

lpush :: (Key k, Serializable s) => Server -> k -> s -> IO (Bool)
lpush = doBulkCommand "LPUSH"

rpush :: (Key k, Serializable s) => Server -> k -> s -> IO (Bool)
rpush = doBulkCommand "RPUSH"

lrange :: (Key k, Serializable s) => Server -> k -> Int -> Int -> IO (Maybe [Maybe s])
lrange (Server handle) key start end = do
  hPutCommand handle ["LRANGE", toKey key, show start, show end]
  getMultiBulkReply handle

llen :: (Key k) => Server -> k -> IO (Maybe Int)  
llen (Server handle) key =  do
  hPutCommand handle ["LLEN", toKey key]
  readIntegerReply handle

ltrim :: (Key k) => Server -> k -> Int -> Int -> IO (Bool)  
ltrim (Server handle) key start end = do
  hPutCommand handle ["LTRIM", toKey key, show start, show end]
  val <- readSingleLineReply handle
  case val of
    Just "OK" -> return True
    _ -> return False

lindex :: (Key k, Serializable s) => Server -> k -> Int -> IO (Maybe s)
lindex (Server handle) key index = do
  hPutCommand handle ["LINDEX", toKey key, show index]
  getBulkReply handle

lset :: (Key k, Serializable s) => Server -> k -> Int -> s -> IO (Bool)  
lset (Server handle) key index value = do
  let valString = toString value
  let bytes = length valString
  hPutCommand handle ["LSET", toKey key, show index, show bytes]
  hPutNetLn handle valString
  hFlush handle
  val <- readSingleLineReply handle
  case val of
    Just "OK" -> return True
    _ -> return False

lrem :: (Key k, Serializable s) => Server -> k -> Int -> s -> IO (Maybe Int)
lrem (Server handle) key index value = do
  let valString = toString value
  let bytes = length valString
  hPutCommand handle ["LREM", toKey key, show index, show bytes]
  hPutNetLn handle valString
  hFlush handle
  readIntegerReply handle

lpop :: (Key k, Serializable s) => Server -> k -> IO (Maybe s)  
lpop (Server handle) key = do
  hPutCommand handle ["LPOP", toKey key]
  getBulkReply handle

rpop :: (Key k, Serializable s) => Server -> k -> IO (Maybe s)
rpop (Server handle) key = do
  hPutCommand handle ["RPOP", toKey key]
  getBulkReply handle