perf: cache system-message visibility times

This commit is contained in:
Gregor Kleen 2021-07-21 21:55:25 +02:00
parent ef4734ebb6
commit 33171a28d7
2 changed files with 27 additions and 4 deletions

View File

@ -479,6 +479,7 @@ data AuthorizationCacheKey
| AuthCacheSchoolFunctionList SchoolFunction | AuthCacheSystemFunctionList SystemFunction | AuthCacheSchoolFunctionList SchoolFunction | AuthCacheSystemFunctionList SystemFunction
| AuthCacheLecturerList | AuthCacheExternalExamStaffList | AuthCacheCorrectorList | AuthCacheExamCorrectorList | AuthCacheTutorList | AuthCacheSubmissionGroupUserList | AuthCacheLecturerList | AuthCacheExternalExamStaffList | AuthCacheCorrectorList | AuthCacheExamCorrectorList | AuthCacheTutorList | AuthCacheSubmissionGroupUserList
| AuthCacheCourseRegisteredList TermId SchoolId CourseShorthand | AuthCacheCourseRegisteredList TermId SchoolId CourseShorthand
| AuthCacheVisibleSystemMessages
deriving (Eq, Ord, Read, Show, Generic, Typeable) deriving (Eq, Ord, Read, Show, Generic, Typeable)
deriving anyclass (Hashable, Binary) deriving anyclass (Hashable, Binary)
@ -1053,10 +1054,22 @@ tagAccessPredicate AuthTime = APDB $ \_ (runTACont -> cont) mAuthId route isWrit
MessageR cID -> maybeT (unauthorizedI MsgUnauthorizedSystemMessageTime) $ do MessageR cID -> maybeT (unauthorizedI MsgUnauthorizedSystemMessageTime) $ do
smId <- catchIfMaybeT (const True :: CryptoIDError -> Bool) $ decrypt cID smId <- catchIfMaybeT (const True :: CryptoIDError -> Bool) $ decrypt cID
SystemMessage{systemMessageFrom, systemMessageTo} <- $cachedHereBinary smId . MaybeT $ get smId cTime <- liftIO getCurrentTime
cTime <- NTop . Just <$> liftIO getCurrentTime let cacheTime = diffDay
guard $ NTop systemMessageFrom <= cTime massageVisible = Map.fromList . map (over _1 E.unValue . over (_2 . _1) E.unValue . over (_2 . _2) E.unValue)
&& NTop systemMessageTo >= cTime visibleSystemMessages <- lift . memcacheAuth' @(Map SystemMessageId (Maybe UTCTime, Maybe UTCTime)) (Right cacheTime) AuthCacheVisibleSystemMessages . fmap massageVisible . E.select . E.from $ \systemMessage -> do
E.where_ $ E.maybe E.true (E.>=. E.val cTime) (systemMessage E.^. SystemMessageTo)
E.&&. E.maybe E.false (E.<=. E.val (realToFrac diffDay `addUTCTime` cTime)) (systemMessage E.^. SystemMessageFrom) -- good enough.
return
( systemMessage E.^. SystemMessageId
, ( systemMessage E.^. SystemMessageFrom
, systemMessage E.^. SystemMessageTo
)
)
(msgFrom, msgTo) <- hoistMaybe $ Map.lookup smId visibleSystemMessages
let cTime' = NTop $ Just cTime
guard $ NTop msgFrom <= cTime'
&& NTop msgTo >= cTime'
return Authorized return Authorized
MessageHideR cID -> maybeT (unauthorizedI MsgUnauthorizedSystemMessageTime) $ do MessageHideR cID -> maybeT (unauthorizedI MsgUnauthorizedSystemMessageTime) $ do

View File

@ -21,6 +21,12 @@ import qualified Database.Esqueleto.Legacy as E
-- htmlField' moved to Handler.Utils.Form/Fields -- htmlField' moved to Handler.Utils.Form/Fields
invalidateVisibleSystemMessages :: (MonadHandler m, HandlerSite m ~ UniWorX)
=> m ()
invalidateVisibleSystemMessages
= memcachedByInvalidate AuthCacheVisibleSystemMessages $ Proxy @(Map SystemMessageId (Maybe UTCTime, Maybe UTCTime))
getMessageR, postMessageR :: CryptoUUIDSystemMessage -> Handler Html getMessageR, postMessageR :: CryptoUUIDSystemMessage -> Handler Html
getMessageR = postMessageR getMessageR = postMessageR
postMessageR cID = do postMessageR cID = do
@ -138,6 +144,7 @@ postMessageR cID = do
where where
modifySystemMessage smId sm = do modifySystemMessage smId sm = do
runDB $ replace smId sm runDB $ replace smId sm
invalidateVisibleSystemMessages
addMessageI Success MsgSystemMessageEditSuccess addMessageI Success MsgSystemMessageEditSuccess
redirect $ MessageR cID redirect $ MessageR cID
@ -258,18 +265,21 @@ postMessageListR = do
| not $ null selection -> do | not $ null selection -> do
selection' <- traverse decrypt $ Set.toList selection selection' <- traverse decrypt $ Set.toList selection
runDB $ deleteWhere [ SystemMessageId <-. selection' ] runDB $ deleteWhere [ SystemMessageId <-. selection' ]
invalidateVisibleSystemMessages
$(addMessageFile Success "templates/messages/systemMessagesDeleted.hamlet") $(addMessageFile Success "templates/messages/systemMessagesDeleted.hamlet")
redirect MessageListR redirect MessageListR
FormSuccess (SMDActivate ts, selection) FormSuccess (SMDActivate ts, selection)
| not $ null selection -> do | not $ null selection -> do
selection' <- traverse decrypt $ Set.toList selection selection' <- traverse decrypt $ Set.toList selection
runDB $ updateWhere [ SystemMessageId <-. selection' ] [ SystemMessageFrom =. ts ] runDB $ updateWhere [ SystemMessageId <-. selection' ] [ SystemMessageFrom =. ts ]
invalidateVisibleSystemMessages
$(addMessageFile Success "templates/messages/systemMessagesSetFrom.hamlet") $(addMessageFile Success "templates/messages/systemMessagesSetFrom.hamlet")
redirect MessageListR redirect MessageListR
FormSuccess (SMDDeactivate ts, selection) FormSuccess (SMDDeactivate ts, selection)
| not $ null selection -> do | not $ null selection -> do
selection' <- traverse decrypt $ Set.toList selection selection' <- traverse decrypt $ Set.toList selection
runDB $ updateWhere [ SystemMessageId <-. selection' ] [ SystemMessageTo =. ts ] runDB $ updateWhere [ SystemMessageId <-. selection' ] [ SystemMessageTo =. ts ]
invalidateVisibleSystemMessages
$(addMessageFile Success "templates/messages/systemMessagesSetTo.hamlet") $(addMessageFile Success "templates/messages/systemMessagesSetTo.hamlet")
redirect MessageListR redirect MessageListR
FormSuccess (_, _selection) -- prop> null _selection FormSuccess (_, _selection) -- prop> null _selection