1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-27 10:10:50 +03:00

Invalidate cache in new handlers (#225)

This commit is contained in:
Artyom Kazak 2018-10-16 22:01:25 +02:00 committed by GitHub
parent bfca5dcf99
commit d6eb20dae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@ import Guide.Api.Types
import Guide.State import Guide.State
import Guide.Types import Guide.Types
import Guide.Utils import Guide.Utils
import Guide.Cache
import qualified Data.Text as T import qualified Data.Text as T
import qualified Guide.Search as Search import qualified Guide.Search as Search
@ -52,13 +53,14 @@ createCategory db title' group' = do
Nothing -> do Nothing -> do
catId <- randomShortUid catId <- randomShortUid
time <- liftIO getCurrentTime time <- liftIO getCurrentTime
(_edit, newCategory) <- dbUpdate db (AddCategory catId title' group' time) (_edit, _newCategory) <- dbUpdate db (AddCategory catId title' group' time)
invalidateCache' db (CacheCategory catId)
-- TODO addEdit edit -- TODO addEdit edit
return (newCategory^.uid) return catId
-- | Delete a category. -- | Delete a category.
deleteCategory :: DB -> Uid Category -> Handler NoContent deleteCategory :: DB -> Uid Category -> Handler NoContent
deleteCategory db catId = do deleteCategory db catId = uncache db (CacheCategory catId) $ do
_mbEdit <- dbUpdate db (DeleteCategory catId) _mbEdit <- dbUpdate db (DeleteCategory catId)
pure NoContent pure NoContent
-- TODO mapM_ addEdit mbEdit -- TODO mapM_ addEdit mbEdit
@ -83,6 +85,7 @@ createItem db catId name' = do
kind' = if looksLikeLibrary then Library (Just name') else Other kind' = if looksLikeLibrary then Library (Just name') else Other
time <- liftIO getCurrentTime time <- liftIO getCurrentTime
(_edit, _newItem) <- dbUpdate db (AddItem catId itemId name' time kind') (_edit, _newItem) <- dbUpdate db (AddItem catId itemId name' time kind')
invalidateCache' db (CacheItem itemId)
-- TODO: addEdit edit -- TODO: addEdit edit
pure itemId pure itemId
@ -90,7 +93,7 @@ createItem db catId name' = do
-- | Delete an item. -- | Delete an item.
deleteItem :: DB -> Uid Item -> Handler NoContent deleteItem :: DB -> Uid Item -> Handler NoContent
deleteItem db itemId = do deleteItem db itemId = uncache db (CacheItem itemId) $ do
_mbEdit <- dbUpdate db (DeleteItem itemId) _mbEdit <- dbUpdate db (DeleteItem itemId)
pure NoContent pure NoContent
-- TODO: mapM_ addEdit mbEdit -- TODO: mapM_ addEdit mbEdit
@ -103,7 +106,7 @@ deleteItem db itemId = do
-- | Delete a trait (pro/con). -- | Delete a trait (pro/con).
deleteTrait :: DB -> Uid Item -> Uid Trait -> Handler NoContent deleteTrait :: DB -> Uid Item -> Uid Trait -> Handler NoContent
deleteTrait db itemId traitId = do deleteTrait db itemId traitId = uncache db (CacheItemTraits itemId) $ do
_mbEdit <- dbUpdate db (DeleteTrait itemId traitId) _mbEdit <- dbUpdate db (DeleteTrait itemId traitId)
pure NoContent pure NoContent
-- TODO: mapM_ addEdit mbEdit -- TODO: mapM_ addEdit mbEdit
@ -136,3 +139,18 @@ dbQuery :: (MonadIO m, EventState event ~ GlobalState, QueryEvent event)
=> DB -> event -> m (EventResult event) => DB -> event -> m (EventResult event)
dbQuery db x = liftIO $ dbQuery db x = liftIO $
Acid.query db x Acid.query db x
-- Twins of corresponding functions used in "Guide.Handlers".
-- TODO: remove them when the old backend is gone.
uncache :: (MonadIO m) => DB -> CacheKey -> m a -> m a
uncache db key act = do
gs <- dbQuery db GetGlobalState
x <- act
invalidateCache gs key
return x
invalidateCache' :: (MonadIO m) => DB -> CacheKey -> m ()
invalidateCache' db key = do
gs <- dbQuery db GetGlobalState
invalidateCache gs key