packages feed

snaplet-redis (empty) → 0.1

raw patch · 4 files changed

+137/−0 lines, 4 filesdep +basedep +data-lensdep +data-lens-templatesetup-changed

Dependencies added: base, data-lens, data-lens-template, hedis, mtl, snap, text, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Dmitry Dzhus++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 Dmitry Dzhus 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
+ snaplet-redis.cabal view
@@ -0,0 +1,37 @@+Name:                snaplet-redis+Version:             0.1+Synopsis:            Redis support for Snap Framework+Description:         This package provides a snaplet which wraps Redis+                     interface as implemented by Hedis library. Inline+                     documentation contains usage examples.+Homepage:            https://github.com/dzhus/snaplet-redis/+Bug-reports:         https://github.com/dzhus/snaplet-redis/issues+License:             BSD3+License-file:        LICENSE+Author:              Dmitry Dzhus+Maintainer:          dima@dzhus.org+Category:            Web++Build-type:          Simple+Cabal-version:       >=1.6++source-repository head+  type:     git+  location: git://github.com/dzhus/snaplet-redis.git++Library+  ghc-options: -Wall+  hs-source-dirs: src++  Exposed-modules:   Snap.Snaplet.RedisDB+  +  Build-depends:+    base >= 4 && < 5,+    data-lens >= 2.0.1 && < 2.1,+    data-lens-template >= 2.1 && < 2.2,+    hedis == 0.3.*,+    mtl >= 2 && < 3,+    snap == 0.7.*,+    text == 0.11.*,+    transformers == 0.2.*+  
+ src/Snap/Snaplet/RedisDB.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}++{-|++Redis DB snaplet.++-}++module Snap.Snaplet.RedisDB (RedisDB+                            , runRedisDB+                            , redisDBInit)+where++import Prelude hiding ((.))+import Control.Category ((.))+import Control.Monad.State++import Data.Lens.Common+import Data.Lens.Template+import Data.Text (Text)++import Database.Redis++import Snap.Snaplet+++------------------------------------------------------------------------------+-- | Description text used in redisDBInit as makeSnaplet argument.+description :: Text+description = "Redis snaplet."++------------------------------------------------------------------------------+-- | Snaplet's state data type+data RedisDB = RedisDB+    { _connection :: Connection -- ^ DB connection pool.+    }++makeLens ''RedisDB++------------------------------------------------------------------------------+-- | Perform action using Redis connection from RedisDB snaplet pool+-- (wrapper for 'Database.Redis.runRedis').+--+-- > runRedisDB database $ do+-- >   set "hello" "world"+runRedisDB :: (MonadIO m, MonadState app m) =>+               Lens app (Snaplet RedisDB) -> Redis a -> m a+runRedisDB snaplet action = do+  c <- gets $ getL (connection . snapletValue . snaplet)+  liftIO $ runRedis c action+++------------------------------------------------------------------------------+-- | Make RedisDB snaplet and initialize database connection.+--+-- > appInit :: SnapletInit MyApp MyApp+-- > appInit = makeSnaplet "app" "App with Redis child snaplet" Nothing $+-- >           do+-- >             d <- nestSnaplet "" database $+-- >                                 redisDBInit defaultConnectInfo+-- >             return $ MyApp d+redisDBInit :: ConnectInfo -- ^ Information for connnecting to a Redis server. +            -> SnapletInit b RedisDB+redisDBInit connInfo =+    makeSnaplet "snaplet-redis" description Nothing $ do+      conn <- liftIO $ connect connInfo+      return $ RedisDB conn