Created
June 20, 2016 08:00
-
-
Save banacorn/ae89695808e15f61203a7481c3ffa454 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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