Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cached and cachedBy will not overwrite global state changes #1268

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions yesod-core/Yesod/Core/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ import Text.Hamlet (Html, HtmlUrl, hamlet)
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Map as Map
import qualified Data.HashMap.Strict as HM

import Data.Byteable (constEqBytes)

Expand Down Expand Up @@ -1002,12 +1003,14 @@ cached :: (MonadHandler m, Typeable a)
=> m a
-> m a
cached action = do
gs <- get
eres <- Cache.cached (ghsCache gs) action
cache <- ghsCache <$> get
eres <- Cache.cached cache action
case eres of
Right res -> return res
Left (newCache, res) -> do
put $ gs { ghsCache = newCache }
gs <- get
let merged = newCache `HM.union` ghsCache gs
put $ gs { ghsCache = merged }
return res

-- | a per-request cache. just like 'cached'.
Expand All @@ -1022,12 +1025,14 @@ cached action = do
-- Since 1.4.0
cachedBy :: (MonadHandler m, Typeable a) => S.ByteString -> m a -> m a
cachedBy k action = do
gs <- get
eres <- Cache.cachedBy (ghsCacheBy gs) k action
cache <- ghsCacheBy <$> get
eres <- Cache.cachedBy cache k action
case eres of
Right res -> return res
Left (newCache, res) -> do
put $ gs { ghsCacheBy = newCache }
gs <- get
let merged = newCache `HM.union` ghsCacheBy gs
put $ gs { ghsCacheBy = merged }
return res

-- | Get the list of supported languages supplied by the user.
Expand Down
31 changes: 31 additions & 0 deletions yesod-core/test/YesodCoreTest/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Rank2Types #-}
module YesodCoreTest.Cache (cacheTest, Widget) where

import Test.Hspec
Expand All @@ -25,6 +26,8 @@ newtype V2 = V2 Int
mkYesod "C" [parseRoutes|
/ RootR GET
/key KeyR GET
/nested NestedR GET
/nested-key NestedKeyR GET
|]

instance Yesod C where
Expand Down Expand Up @@ -55,6 +58,24 @@ getKeyR = do

return $ RepPlain $ toContent $ show [v1a, v1b, v2a, v2b, v3a, v3b]

getNestedR :: Handler RepPlain
getNestedR = getNested cached

getNestedKeyR :: Handler RepPlain
getNestedKeyR = getNested $ cachedBy "3"

-- | Issue #1266
getNested :: (forall a. Typeable a => (Handler a -> Handler a)) -> Handler RepPlain
getNested cacheMethod = do
ref <- newIORef 0
let getV2 = atomicModifyIORef ref $ \i -> (i + 1, V2 $ i + 1)
V1 _ <- cacheMethod $ do
V2 val <- cacheMethod $ getV2
return $ V1 val
V2 v2 <- cacheMethod $ getV2

return $ RepPlain $ toContent $ show v2

cacheTest :: Spec
cacheTest =
describe "Test.Cache" $ do
Expand All @@ -68,5 +89,15 @@ cacheTest =
assertStatus 200 res
assertBody (L8.pack $ show [1, 1, 2, 2, 3, 3 :: Int]) res

it "nested cached" $ runner $ do
res <- request defaultRequest { pathInfo = ["nested"] }
assertStatus 200 res
assertBody (L8.pack $ show (1 :: Int)) res

it "nested cachedBy" $ runner $ do
res <- request defaultRequest { pathInfo = ["nested-key"] }
assertStatus 200 res
assertBody (L8.pack $ show (1 :: Int)) res

runner :: Session () -> IO ()
runner f = toWaiApp C >>= runSession f