Skip to content

Commit

Permalink
Merge pull request #32 from juspay/feature/multi
Browse files Browse the repository at this point in the history
added support for Redis multi (Transaction)
  • Loading branch information
iamnewspecies authored Mar 4, 2019
2 parents 5593804 + d35912b commit a0a9e24
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
"purescript-sequelize": "https://github.com/juspay/purescript-sequelize.git#0.1.2",
"purescript-body-parser": "https://github.com/juspay/purescript-body-parser.git#0.1.0",
"purescript-uuid": "^4.0.0",
"purescript-redis": "https://github.com/juspay/purescript-redis.git#0.1.0",
"purescript-redis": "https://github.com/juspay/purescript-redis.git#0.1.1",
"purescript-now": "3.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
},
"resolutions": {
"purescript-aff-promise": "^0.7.0"
"purescript-aff-promise": "^0.7.0",
"purescript-foreign-generic": "5.0.0"
},
"version": "0.0.1"
}
65 changes: 63 additions & 2 deletions src/Presto/Backend/Language/Flow.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Presto.Backend.Flow where

import Prelude

import Cache (CacheConn)
import Cache (CacheConn, Multi)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (Error)
import Control.Monad.Free (Free, liftF)
Expand Down Expand Up @@ -75,6 +75,21 @@ data BackendFlowCommands next st rt s =
| PublishToChannel CacheConn String String (Either Error String -> next)
| Subscribe CacheConn String (Either Error String -> next)
| SetMessageHandler CacheConn (forall eff. (String -> String -> Eff eff Unit)) (Either Error String -> next)
| GetMulti CacheConn (Multi -> next)
| SetCacheInMulti Multi String String (Multi -> next)
| GetCacheInMulti Multi String (Multi -> next)
| DelCacheInMulti Multi String (Multi -> next)
| SetCacheWithExpiryInMulti Multi String String String (Multi -> next)
| ExpireInMulti Multi String String (Multi -> next)
| IncrInMulti Multi String (Multi -> next)
| SetHashInMulti Multi String String (Multi -> next)
| GetHashInMulti Multi String String (Multi -> next)
| SetWithOptionsInMulti Multi (Array String) (Multi -> next)
| PublishToChannelInMulti Multi String String (Multi -> next)
| SubscribeInMulti Multi String (Multi -> next)
| EnqueueInMulti Multi String String (Multi -> next)
| DequeueInMulti Multi String (Multi -> next)
| GetQueueIdxInMulti Multi String Int (Multi -> next)
| RunSysCmd String (String -> next)

-- | HandleException
Expand Down Expand Up @@ -169,25 +184,42 @@ getDBConn dbName = do
getCacheConn :: forall st rt. String -> BackendFlow st rt CacheConn
getCacheConn dbName = wrap $ GetCacheConn dbName id

getMulti :: forall st rt. String -> BackendFlow st rt Multi
getMulti cacheName = do
cacheConn <- getCacheConn cacheName
wrap $ GetMulti cacheConn id

callAPI :: forall st rt a b. Encode a => Decode b => RestEndpoint a b
=> Headers -> a -> BackendFlow st rt (APIResult b)
callAPI headers a = wrap $ CallAPI (apiInteract a headers) id

setCacheInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
setCacheInMulti multi key value = wrap $ SetCacheInMulti multi key value id

setCache :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
setCache cacheName key value = do
cacheConn <- getCacheConn cacheName
wrap $ SetCache cacheConn key value id

getCacheInMulti :: forall st rt. Multi -> String -> BackendFlow st rt Multi
getCacheInMulti multi key = wrap $ GetCacheInMulti multi key id

getCache :: forall st rt. String -> String -> BackendFlow st rt (Either Error String)
getCache cacheName key = do
cacheConn <- getCacheConn cacheName
wrap $ GetCache cacheConn key id

delCacheInMulti :: forall st rt. Multi -> String -> BackendFlow st rt Multi
delCacheInMulti multi key = wrap $ DelCacheInMulti multi key id

delCache :: forall st rt. String -> String -> BackendFlow st rt (Either Error String)
delCache cacheName key = do
cacheConn <- getCacheConn cacheName
wrap $ DelCache cacheConn key id

setCacheWithExpireInMulti :: forall st rt. Multi -> String -> String -> String -> BackendFlow st rt Multi
setCacheWithExpireInMulti multi key value ttl = wrap $ SetCacheWithExpiryInMulti multi key value ttl id

setCacheWithExpiry :: forall st rt. String -> String -> String -> String -> BackendFlow st rt (Either Error String)
setCacheWithExpiry cacheName key value ttl = do
cacheConn <- getCacheConn cacheName
Expand All @@ -196,52 +228,81 @@ setCacheWithExpiry cacheName key value ttl = do
log :: forall st rt a. String -> a -> BackendFlow st rt Unit
log tag message = wrap $ Log tag message unit

expireInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
expireInMulti multi key ttl = wrap $ ExpireInMulti multi key ttl id

expire :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
expire cacheName key ttl = do
cacheConn <- getCacheConn cacheName
wrap $ Expire cacheConn key ttl id

incrInMulti :: forall st rt. Multi -> String -> BackendFlow st rt Multi
incrInMulti multi key = wrap $ IncrInMulti multi key id

incr :: forall st rt. String -> String -> BackendFlow st rt (Either Error String)
incr cacheName key = do
cacheConn <- getCacheConn cacheName
wrap $ Incr cacheConn key id

setHashInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
setHashInMulti multi key value = wrap $ SetHashInMulti multi key value id

setHash :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
setHash cacheName key value = do
cacheConn <- getCacheConn cacheName
wrap $ SetCache cacheConn key value id
wrap $ SetHash cacheConn key value id

getHashKeyInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
getHashKeyInMulti multi key field = wrap $ GetHashInMulti multi key field id

getHashKey :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
getHashKey cacheName key field = do
cacheConn <- getCacheConn cacheName
wrap $ GetHashKey cacheConn key field id

setWithOptionsInMulti :: forall st rt. Multi -> Array String -> BackendFlow st rt Multi
setWithOptionsInMulti multi arr = wrap $ SetWithOptionsInMulti multi arr id

setWithOptions :: forall st rt. String -> Array String -> BackendFlow st rt (Either Error String)
setWithOptions cacheName arr = do
cacheConn <- getCacheConn cacheName
wrap $ SetWithOptions cacheConn arr id

publishToChannelInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
publishToChannelInMulti multi channel message = wrap $ PublishToChannelInMulti multi channel message id

publishToChannel :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
publishToChannel cacheName channel message = do
cacheConn <- getCacheConn cacheName
wrap $ PublishToChannel cacheConn channel message id

subscribeToMulti :: forall st rt. Multi -> String -> BackendFlow st rt Multi
subscribeToMulti multi channel = wrap $ SubscribeInMulti multi channel id

subscribe :: forall st rt. String -> String -> BackendFlow st rt (Either Error String)
subscribe cacheName channel = do
cacheConn <- getCacheConn cacheName
wrap $ Subscribe cacheConn channel id

enqueueInMulti :: forall st rt. Multi -> String -> String -> BackendFlow st rt Multi
enqueueInMulti multi listName value = wrap $ EnqueueInMulti multi listName value id

enqueue :: forall st rt. String -> String -> String -> BackendFlow st rt (Either Error String)
enqueue cacheName listName value = do
cacheConn <- getCacheConn cacheName
wrap $ Enqueue cacheConn listName value id

dequeueInMulti :: forall st rt. Multi -> String -> BackendFlow st rt Multi
dequeueInMulti multi listName = wrap $ DequeueInMulti multi listName id

dequeue :: forall st rt. String -> String -> BackendFlow st rt (Either Error String)
dequeue cacheName listName = do
cacheConn <- getCacheConn cacheName
wrap $ Dequeue cacheConn listName id

getQueueIdxInMulti :: forall st rt. Multi -> String -> Int -> BackendFlow st rt Multi
getQueueIdxInMulti multi listName index = wrap $ GetQueueIdxInMulti multi listName index id

getQueueIdx :: forall st rt. String -> String -> Int -> BackendFlow st rt (Either Error String)
getQueueIdx cacheName listName index = do
cacheConn <- getCacheConn cacheName
Expand Down
32 changes: 31 additions & 1 deletion src/Presto/Backend/Language/Interpreter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Presto.Backend.Interpreter where

import Prelude

import Cache (CacheConn, delKey, enqueue, dequeue, expire, getHashKey, getKey, incr, publishToChannel, set, setHash, setKey, setMessageHandler, setex, subscribe, getQueueIdx)
import Cache (CacheConn, delKey, dequeue, dequeueMulti, enqueue, enqueueMulti, expire, expireMulti, getHashKey, getHashKeyMulti, getKey, getKeyMulti, getMulti, getQueueIdx, getQueueIdxMulti, incr, incrMulti, publishToChannel, publishToChannelMulti, set, setHash, setHashMulti, setKey, setKeyMulti, setMessageHandler, setMulti, setex, setexKeyMulti, subscribe, subscribeMulti)
import Control.Monad.Aff (Aff, forkAff)
import Control.Monad.Eff.Exception (Error, error)
import Control.Monad.Except.Trans (ExceptT(..), lift, throwError, runExceptT) as E
Expand Down Expand Up @@ -110,6 +110,36 @@ interpret _ (Dequeue cacheConn listName next) = (R.lift $ S.lift $ E.lift $ dequ

interpret _ (GetQueueIdx cacheConn listName index next) = (R.lift $ S.lift $ E.lift $ getQueueIdx cacheConn listName index) >>= (pure <<< next)

interpret _ (GetMulti cacheConn next) = (R.lift $ S.lift $ E.lift $ getMulti cacheConn) >>= (pure <<< next)

interpret _ (SetCacheInMulti multi key val next) = (R.lift <<< S.lift <<< E.lift <<< setKeyMulti key val $ multi ) >>= (pure <<< next)

interpret _ (GetCacheInMulti multi key next) = (R.lift <<< S.lift <<< E.lift <<< pure <<< next $ multi)

interpret _ (DelCacheInMulti multi key next) = (R.lift <<< S.lift <<< E.lift <<< getKeyMulti key$ multi) >>= (pure <<< next )

interpret _ (SetCacheWithExpiryInMulti multi key val ttl next) = (R.lift <<< S.lift <<< E.lift <<< setexKeyMulti key val ttl $ multi )>>= (pure <<< next )

interpret _ (ExpireInMulti multi key ttl next) = (R.lift <<< S.lift <<< E.lift <<< expireMulti key ttl $ multi) >>= (pure <<< next)

interpret _ (IncrInMulti multi key next) = (R.lift <<< S.lift <<< E.lift <<< incrMulti key $ multi) >>= (pure <<< next)

interpret _ (SetHashInMulti multi key value next) = (R.lift <<< S.lift <<< E.lift <<< setHashMulti key value $ multi) >>= (pure <<< next )

interpret _ (GetHashInMulti multi key value next) = (R.lift <<< S.lift <<< E.lift <<< getHashKeyMulti key value $ multi) >>= (pure <<< next )

interpret _ (SetWithOptionsInMulti multi key next) = (R.lift <<< S.lift <<< E.lift <<< setMulti key $ multi) >>= (pure <<< next)

interpret _ (PublishToChannelInMulti multi channel message next) = (R.lift <<< S.lift <<< E.lift <<< publishToChannelMulti channel message $ multi) >>= (pure <<< next)

interpret _ (SubscribeInMulti multi channel next) = (R.lift <<< S.lift <<< E.lift <<< subscribeMulti channel $ multi) >>= (pure <<< next)

interpret _ (EnqueueInMulti multi listName val next) = (R.lift <<< S.lift <<< E.lift <<< enqueueMulti listName val $ multi) >>= (pure <<< next)

interpret _ (DequeueInMulti multi listName next) = (R.lift <<< S.lift <<< E.lift <<< dequeueMulti listName $ multi) >>= (pure <<< next)

interpret _ (GetQueueIdxInMulti multi listName index next) = (R.lift <<< S.lift <<< E.lift <<< getQueueIdxMulti listName index $ multi) >>= (pure <<< next)

interpret (BackendRuntime a connections c) (GetCacheConn cacheName next) = do
maybeCache <- pure $ lookup cacheName connections
case maybeCache of
Expand Down

0 comments on commit a0a9e24

Please sign in to comment.