Skip to content

Instantly share code, notes, and snippets.

@banacorn

banacorn/MQ3.hs

Created Jun 20, 2016
Embed
What would you like to do?
{-# LANGUAGE OverloadedStrings, DataKinds, DeriveGeneric, TypeFamilies #-}
module MQ where
import Database.Edis
import Data.ByteString
import Data.Serialize hiding (Get)
import GHC.Generics
data Message = Msg ByteString Integer deriving (Show, Generic)
instance Serialize Message where
fromRight :: Either a b -> b
fromRight (Right x) = x
fromRight (Left _) = error "Left"
push :: (StringOfIntegerOrNX xs "id", ListOrNX xs "queue")
=> ByteString
-> Edis xs (Set xs "queue" (ListOf Message)) (Either Reply Integer)
push msg = incr (Proxy :: Proxy "id")
`bind` \i -> lpush (Proxy :: Proxy "queue") (Msg msg (fromRight i))
pop :: (Get xs "queue" ~ (ListOf Message))
=> Edis xs xs (Either Reply (Maybe Message))
pop = lpop (Proxy :: Proxy "queue")
main :: IO ()
main = do
conn <- connect defaultConnectInfo
result <- runRedis conn $ unEdis $ start
>>> push "hello"
>>> pop
print $ result
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment