Redis backend is all green.

This commit is contained in:
Felipe Lessa 2015-05-28 02:42:22 -03:00
parent 92fbee20da
commit ff6c0efaab
2 changed files with 46 additions and 18 deletions

View File

@ -18,6 +18,7 @@ module Web.ServerSession.Backend.Redis.Internal
, getSessionImpl , getSessionImpl
, deleteSessionImpl , deleteSessionImpl
, removeSessionFromAuthId , removeSessionFromAuthId
, insertSessionForAuthId
, deleteAllSessionsOfAuthIdImpl , deleteAllSessionsOfAuthIdImpl
, insertSessionImpl , insertSessionImpl
, replaceSessionImpl , replaceSessionImpl
@ -185,9 +186,19 @@ deleteSessionImpl sid = do
-- | Remove the given 'SessionId' from the set of sessions of the -- | Remove the given 'SessionId' from the set of sessions of the
-- given 'AuthId'. Does not do anything if @Nothing@. -- given 'AuthId'. Does not do anything if @Nothing@.
removeSessionFromAuthId :: R.RedisCtx m f => SessionId -> Maybe AuthId -> m () removeSessionFromAuthId :: R.RedisCtx m f => SessionId -> Maybe AuthId -> m ()
removeSessionFromAuthId _ Nothing = return () removeSessionFromAuthId = fooSessionBarAuthId R.srem
removeSessionFromAuthId sid (Just authId) =
void $ R.srem (rAuthKey authId) [rSessionKey sid] -- | Insert the given 'SessionId' into the set of sessions of the
-- given 'AuthId'. Does not do anything if @Nothing@.
insertSessionForAuthId :: R.RedisCtx m f => SessionId -> Maybe AuthId -> m ()
insertSessionForAuthId = fooSessionBarAuthId R.sadd
-- | (Internal) Helper for 'removeSessionFromAuthId' and 'insertSessionForAuthId'
fooSessionBarAuthId
:: R.RedisCtx m f => (ByteString -> [ByteString] -> m (f Integer)) -> SessionId -> Maybe AuthId -> m ()
fooSessionBarAuthId _ _ Nothing = return ()
fooSessionBarAuthId fun sid (Just authId) = void $ fun (rAuthKey authId) [rSessionKey sid]
-- | Delete all sessions of the given auth ID. -- | Delete all sessions of the given auth ID.
@ -200,21 +211,40 @@ deleteAllSessionsOfAuthIdImpl authId = do
-- | Insert a new session. -- | Insert a new session.
insertSessionImpl :: Session -> R.Redis () insertSessionImpl :: Session -> R.Redis ()
insertSessionImpl session = do insertSessionImpl session = do
transaction $ do -- Check that no old session exists.
let sk = rSessionKey $ sessionKey session let sid = sessionKey session
r <- R.hmset sk (printSession session) moldSession <- getSessionImpl sid
-- TODO: R.expireat case moldSession of
maybe (return ()) (\authId -> void $ R.sadd (rAuthKey authId) [sk]) $ sessionAuthId session Just oldSession -> liftIO $ E.throwIO $ SessionAlreadyExists oldSession session
return (() <$ r) Nothing -> do
transaction $ do
let sk = rSessionKey sid
r <- R.hmset sk (printSession session)
-- TODO: R.expireat
insertSessionForAuthId (sessionKey session) (sessionAuthId session)
return (() <$ r)
-- | Replace the contents of a session. -- | Replace the contents of a session.
replaceSessionImpl :: Session -> R.Redis () replaceSessionImpl :: Session -> R.Redis ()
replaceSessionImpl session = do replaceSessionImpl session = do
-- Remove the old auth ID from the map if it has changed. -- Check that the old session exists.
oldSession <- getSessionImpl (sessionKey session) let sid = sessionKey session
let oldAuthId = sessionAuthId =<< oldSession moldSession <- getSessionImpl sid
when (oldAuthId /= sessionAuthId session) $ case moldSession of
removeSessionFromAuthId (sessionKey session) oldAuthId Nothing -> liftIO $ E.throwIO $ SessionDoesNotExist session
-- Otherwise the operation is the same as inserting. Just oldSession -> do
insertSessionImpl session transaction $ do
-- Delete the old session and set the new one.
let sk = rSessionKey sid
_ <- R.del [sk]
r <- R.hmset sk (printSession session)
-- Remove the old auth ID from the map if it has changed.
let oldAuthId = sessionAuthId oldSession
newAuthId = sessionAuthId session
when (oldAuthId /= newAuthId) $ do
removeSessionFromAuthId sid oldAuthId
insertSessionForAuthId sid newAuthId
return (() <$ r)

View File

@ -5,8 +5,6 @@ import Test.Hspec
import Web.ServerSession.Backend.Redis import Web.ServerSession.Backend.Redis
import Web.ServerSession.Core.StorageTests import Web.ServerSession.Core.StorageTests
import qualified Control.Exception as E
main :: IO () main :: IO ()
main = do main = do
conn <- connect defaultConnectInfo conn <- connect defaultConnectInfo