packages feed

redis-simple (empty) → 0.1

raw patch · 4 files changed

+187/−0 lines, 4 filesdep +basedep +binarydep +bytestringsetup-changed

Dependencies added: base, binary, bytestring, redis

Files

+ Database/Redis/Simple.hs view
@@ -0,0 +1,127 @@+-- | 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 #-}+module Database.Redis.Simple+    ( -- * Type for keys+      Key (..) ++      -- * Working with simple key-value pairs+    , itemGet+    , itemExists+    , itemSet+    , itemDelete++      -- * Working with sets+    , setAdd+    , setRemove+    , setContains+    , setFindAll+    ) where++import Control.Applicative ((<$>))+import Data.Maybe (catMaybes)+import Data.Monoid (Monoid)+import Data.ByteString (ByteString)+import GHC.Exts (IsString)+import Data.Binary (Binary, encode, decode)++import Database.Redis.Redis++-- | Type for a key in the key-value store+--+newtype Key = Key {unKey :: ByteString}+            deriving (Show, Eq, Ord, IsString, Monoid, Binary)++-- | Gets an item from the database+--+itemGet :: Binary a+        => Redis         -- ^ Redis handle+        -> Key           -- ^ Key of the value to get+        -> IO (Maybe a)  -- ^ Resulting value+itemGet redis key = do+    reply <- get redis $ unKey key+    return $ case reply of RBulk (Just r) -> Just $ decode r+                           _              -> Nothing++-- | Checks if an item with a given key exists+--+itemExists :: Redis    -- ^ Redis handle+           -> Key      -- ^ Key to test+           -> IO Bool  -- ^ If the key exists+itemExists redis (Key key) = do+    reply <- exists redis key+    return $ case reply of RInt 1 -> True+                           _      -> False++-- | Set an item in the database+--+itemSet :: Binary a+        => Redis     -- ^ Redis handle+        -> Key       -- ^ Key+        -> a         -- ^ Value+        -> IO ()     -- ^ No result+itemSet redis (Key key) item = do+    _ <- set redis key (encode item)+    return ()++-- | Delete an item in the database+--+itemDelete :: Redis  -- ^ Redis handle+           -> Key    -- ^ Key+           -> IO ()  -- ^ No result+itemDelete redis (Key key) = do+    _ <- del redis key+    return ()++-- | Add an item to a redis set+--+setAdd :: Binary a+       => Redis     -- ^ Redis handle+       -> Key       -- ^ Key of the set+       -> a         -- ^ Item to add to the set+       -> IO ()     -- ^ No result+setAdd redis (Key s) m = do+    _ <- sadd redis s $ encode m+    return ()++-- | Remove an item from a redis set+--+setRemove :: Binary a+          => Redis     -- ^ Redis handle+          -> Key       -- ^ Key of the set+          -> a         -- ^ Item to remove from the set+          -> IO ()     -- ^ No result+setRemove redis (Key s) m = do+    _ <- srem redis s $ encode m+    return ()++-- | Check if a set contains a certain item+--+setContains :: Binary a+            => Redis     -- ^ Redis handle+            -> Key       -- ^ Key of the set+            -> a         -- ^ Item to check for+            -> IO Bool   -- ^ If the item is present in the set+setContains redis (Key s) m = do+    reply <- sismember redis s $ encode m+    return $ case reply of+        RInt 1 -> True+        _      -> False++-- | Get all items from a set+--+setFindAll :: Binary a+           => Redis     -- ^ Redis handle+           -> Key       -- ^ Key of the set+           -> IO [a]    -- ^ All items in the set+setFindAll redis (Key s) = do+    reply <- smembers redis s+    case reply of+        RMulti (Just replies) ->+            return $ catMaybes $ flip map replies $ \r -> case r of+                RBulk i -> decode <$> i+                _       -> Nothing+        _ -> return []
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Jasper Van der Jeugt 2010++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Jasper Van der Jeugt nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ redis-simple.cabal view
@@ -0,0 +1,28 @@+Name:                redis-simple+Version:             0.1+Synopsis:            Simple redis bindings for Haskell+Description:         This package is meant to simplify usage of redis in+                     Haskell. It is built on the full-blown @redis@ package+                     and, in fact, does nothing more than providing an easier+                     interface to a small subset of it's functions.+Homepage:            http://github.com/jaspervdj/redis-simple+Bug-Reports:         http://github.com/jaspervdj/redis-simple/issues+License:             BSD3+License-file:        LICENSE+Author:              Jasper Van der Jeugt+Maintainer:          jaspervdj@gmail.com+Stability:           Experimental+Category:            Text+Build-type:          Simple+Cabal-version:       >=1.2+Library+  Ghc-Options:       -Wall++  -- Modules exported by the library.+  Exposed-modules:   Database.Redis.Simple+  +  -- Packages needed in order to build this package.+  Build-depends:     base >= 4 && < 5,+                     redis >= 0.7,+                     bytestring >= 0.9,+                     binary >= 0.5