tmp-proc-example 0.7.0.0 → 0.7.2.5
raw patch · 3 files changed
+45/−18 lines, 3 filesdep ~tmp-proc-redis
Dependency ranges changed: tmp-proc-redis
Files
- src/TmpProc/Example1/Cache.hs +22/−8
- src/TmpProc/Example2/Cache.hs +21/−8
- tmp-proc-example.cabal +2/−2
src/TmpProc/Example1/Cache.hs view
@@ -1,11 +1,11 @@ {-# LANGUAGE LambdaCase #-}-{-|++{- | Copyright : (c) 2020-2021 Tim Emiola SPDX-License-Identifier: BSD3 Maintainer : Tim Emiola <adetokunbo@users.noreply.github.com> Implements a cache for the demo service- -} module TmpProc.Example1.Cache ( -- * Cache services@@ -20,32 +20,46 @@ ) where -import Control.Monad (void)-import Data.ByteString.Char8 (pack, unpack, ByteString)-import Database.Redis+import Control.Monad (void)+import Data.ByteString.Char8 (ByteString, pack, unpack)+import Database.Redis+ ( ConnectInfo+ , Redis+ , connect+ , defaultConnectInfo+ , get+ , runRedis+ , setex+ )+import System.TmpProc.Docker.Redis (del)+import TmpProc.Example1.Schema -import TmpProc.Example1.Schema -{-| Specifies the @Redis@ instance to use as a cache .-}+-- | Specifies the @Redis@ instance to use as a cache . type Locator = ConnectInfo -{-| A default for local development .-}++-- | A default for local development . defaultLoc :: Locator defaultLoc = defaultConnectInfo + runRedisAction :: Locator -> Redis a -> IO a runRedisAction loc action = do connection <- connect loc runRedis connection action + saveContact :: Locator -> ContactID -> Contact -> IO () saveContact loc cid contact = runRedisAction loc $ void $ setex (toKey cid) 3600 (pack . show $ contact) + loadContact :: Locator -> ContactID -> IO (Maybe Contact) loadContact loc cid = runRedisAction loc $ do (get $ toKey cid) >>= \case Right (Just contactString) -> return $ Just (read . unpack $ contactString) _ -> return Nothing+ deleteContact :: Locator -> ContactID -> IO () deleteContact loc cid = do
src/TmpProc/Example2/Cache.hs view
@@ -1,11 +1,11 @@ {-# LANGUAGE LambdaCase #-}-{-|++{- | Copyright : (c) 2020-2021 Tim Emiola SPDX-License-Identifier: BSD3 Maintainer : Tim Emiola <adetokunbo@users.noreply.github.com> Implements a cache for the demo service- -} module TmpProc.Example2.Cache ( -- * Cache services@@ -20,32 +20,45 @@ ) where -import Control.Monad (void)-import Data.ByteString.Char8 (pack, unpack, ByteString)-import Database.Redis+import Control.Monad (void)+import Data.ByteString.Char8 (ByteString, pack, unpack)+import Database.Redis+ ( Connection+ , Redis+ , connect+ , defaultConnectInfo+ , get+ , runRedis+ , setex+ )+import System.TmpProc.Docker.Redis (del)+import TmpProc.Example2.Schema -import TmpProc.Example2.Schema -{-| A default for local development .-}+-- | A default for local development . defaultConn :: IO Connection defaultConn = connect defaultConnectInfo + runRedisAction :: Connection -> Redis a -> IO a runRedisAction loc action = runRedis loc action + saveContact :: Connection -> ContactID -> Contact -> IO () saveContact loc cid contact = runRedisAction loc $ void $ setex (toKey cid) 3600 (pack . show $ contact) + loadContact :: Connection -> ContactID -> IO (Maybe Contact) loadContact loc cid = runRedisAction loc $ do (get $ toKey cid) >>= \case Right (Just contactString) -> return $ Just (read . unpack $ contactString) _ -> return Nothing + deleteContact :: Connection -> ContactID -> IO () deleteContact loc cid = do runRedis loc $ do- _ <- del [pack . show $ cid]+ _ <- del [(pack . show $ cid)] return ()
tmp-proc-example.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: tmp-proc-example-version: 0.7.0.0+version: 0.7.2.5 synopsis: Test a simple service with backends running on docker using tmp-proc description: An example of testing a service that uses Postgresql and Redis where these@@ -65,7 +65,7 @@ , time , tmp-proc >=0.5.3 && <0.8 , tmp-proc-postgres- , tmp-proc-redis+ , tmp-proc-redis >=0.7.2.5 , transformers , wai , warp