-- | This module is meant to make working with redis in Haskell more simple. It-- is a small layer above the full-blown @redis@ package.---- It only supports a small subset of the redis features.--{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}moduleDatabase.Redis.Simple(-- * Type for keysKey(..)-- * Working with simple key-value pairs,itemGet,itemExists,itemSet,itemDelete-- * Working with sets,setAdd,setRemove,setContains,setFindAll)whereimportControl.Applicative((<$>))importData.Maybe(catMaybes)importData.Monoid(Monoid)importData.ByteString(ByteString)importGHC.Exts(IsString)importData.Binary(Binary,encode,decode)importDatabase.Redis.Redis-- | Type for a key in the key-value store--newtypeKey=Key{unKey::ByteString}deriving(Show,Eq,Ord,IsString,Monoid,Binary)-- | Gets an item from the database--itemGet::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key of the value to get->IO(Maybea)-- ^ Resulting valueitemGetrediskey=doreply<-getredis$unKeykeyreturn$casereplyofRBulk(Justr)->Just$decoder_->Nothing-- | Checks if an item with a given key exists--itemExists::Redis-- ^ Redis handle->Key-- ^ Key to test->IOBool-- ^ If the key existsitemExistsredis(Keykey)=doreply<-existsrediskeyreturn$casereplyofRInt1->True_->False-- | Set an item in the database--itemSet::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key->a-- ^ Value->IO()-- ^ No resultitemSetredis(Keykey)item=do_<-setrediskey(encodeitem)return()-- | Delete an item in the database--itemDelete::Redis-- ^ Redis handle->Key-- ^ Key->IO()-- ^ No resultitemDeleteredis(Keykey)=do_<-delrediskeyreturn()-- | Add an item to a redis set--setAdd::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key of the set->a-- ^ Item to add to the set->IO()-- ^ No resultsetAddredis(Keys)m=do_<-saddrediss$encodemreturn()-- | Remove an item from a redis set--setRemove::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key of the set->a-- ^ Item to remove from the set->IO()-- ^ No resultsetRemoveredis(Keys)m=do_<-sremrediss$encodemreturn()-- | Check if a set contains a certain item--setContains::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key of the set->a-- ^ Item to check for->IOBool-- ^ If the item is present in the setsetContainsredis(Keys)m=doreply<-sismemberrediss$encodemreturn$casereplyofRInt1->True_->False-- | Get all items from a set--setFindAll::Binarya=>Redis-- ^ Redis handle->Key-- ^ Key of the set->IO[a]-- ^ All items in the setsetFindAllredis(Keys)=doreply<-smembersredisscasereplyofRMulti(Justreplies)->return$catMaybes$flipmapreplies$\r->caserofRBulki->decode<$>i_->Nothing_->return[]