Source

redis-haskell / StringsExamples.hs

Full commit
module RedisTest where

-- import Network.Redis(Redis)
import qualified Database.Redis as R
import Database.Redis.Protocol as Single
import Database.Redis.Serializable(Serializable(..))
import Database.Redis.Key
  
main = do
  server <- Single.connect "127.0.0.1" 6379
  
  R.set server "mykey" "value"
  
  doGet server "mykey"
  
  doGet server "nokey"
  
  R.setnx server "mykey" "value" >>= \val -> putStrLn $ show val
  
  R.setnx server "mykey2" "value" >>= \val -> putStrLn $ show val
  
  print_action $ R.getset server "mykey2" "value2"
  
  R.mget server ["mykey", "nosuchkey", "mykey2"] >>= 
    maybe (putStrLn "MGET got nothing") (mapM_ (\h -> do putStrLn ("MGET: " ++ show (h::Maybe String) ++ ".")))
      
  R.incr server "incrkey" >>= \val -> print_int "INCR" val
  R.incr server "incrkey" >>= \val -> print_int "INCR" val
  
  decr <- R.decr server "incrkey"
  putStrLn ("DECR: " ++ show decr ++ ".")

  incrby <- R.incrby server "incrbykey" 5
  putStrLn ("INCRBY: " ++ show incrby ++ ".")

  decrby <- R.decrby server "decrbykey" 5
  putStrLn ("DECRBY: " ++ show decrby ++ ".")
  
  R.flushdb server
  
  Single.disconnect server
    
  where
    doGet server key = R.get server key >>= maybe (putStrLn ("Non existen key: " ++ key ++ ".")) putStrLn
    print_action f = f >>= maybe (putStrLn "something is wrong") putStrLn
    print_int action val = putStrLn (action ++ ": " ++ show val ++ ".")