hedis-monadic-0.0.1: A la MonadReader for Redis connection

Safe HaskellNone
LanguageHaskell2010

Database.Redis.Monadic

Contents

Synopsis

Typeclass

class MonadBase IO m => HasRedis m where Source

Monad which has access to Redis connection

Instances

Default transformer

Redis interconnection

queryRedis :: HasRedis m => Redis a -> m a Source

runRedisTrans :: Connection -> IO Int -> Int -> Redis (TxResult a) -> IO (TxResult a) Source

Run redis transaction and try rerun it if it was aborted. Perform random delay between retries.

runRedisTrans con (randomRIO (100, 1000)) 10 $ do
    watch [key1, key2, key3]
    lIndex key3 0 >>= case
        Nothing -> unwatch *> pure TxAborted
        Just val -> multiExec $ do
            lRem  key3 1
            lPush key1 [val]
            lPush key2 [val]

In next example we copy first value to two different lists and remove it from original list transactionally. If any value in either key1, key2, or key3 was changed between watch command and exec (inside multiExec) then transaction will be aboretd, then thread will wait for between 100 and 1000 microseconds, then whole action will be relaunched.

User responsible do not perform mutating actions outside of multiExec because this actions can be launched multiple times.

Look at next example:

runRedisTrans con (randomRIO (100, 1000)) 10 $ do
    watch [key1, key2, key3]
    lPop key3 >>= case
        Nothing -> unwatch *> pure TxAborted
        Just val -> multiExec $ do
            lPush key1 [val]
            lPush key2 [val]

It is highly unrecommended to do that, because if transaction aborted lPop will be performed several times (up to 10).

queryRedisTrans :: HasRedis m => IO Int -> Int -> Redis (TxResult a) -> m (TxResult a) Source

Same as runRedisTrans but for HasRedis monad instances.