Skip to content

Commit 7b36641

Browse files
committed
Make fetch return True if a value is in cache
This makes it easier to determine the origin of a value with hierarchical cache.
1 parent 4179c1d commit 7b36641

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Data/MemCache.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ new sizeFun sizeLimit =
5959
-- multiple threads try to access a key that doesn't exist in parallel, the
6060
-- constructing action will be run only once.
6161
--
62-
-- /Note:/ returned 'Bool' signifies whether the object was constructed.
62+
-- /Note:/ returned 'Bool' signifies whether the object was in cache.
6363
--
6464
-- __Warning:__ for a given key, the constructing action needs to always return
6565
-- the same result.
@@ -106,7 +106,7 @@ fetch (MemCache mv) key construct = do
106106
)
107107

108108
case eel of
109-
Right el -> unlift $ pure (el, False)
109+
Right el -> unlift $ pure (el, True)
110110
Left (mvEl, mvAlreadyThere) -> do
111111
-- If we got an existing MVar, wait for its result. Otherwise run
112112
-- constructing action and update cache accordingly.
@@ -118,7 +118,7 @@ fetch (MemCache mv) key construct = do
118118
-- If the thread that was supposed to construct the value
119119
-- failed for some reason, let's try ourselves.
120120
loop
121-
Just value -> unlift $ pure (value, False)
121+
Just value -> unlift $ pure (value, True)
122122
else do
123123
evalue <- (`onException` removeInProgress mvEl) $ do
124124
-- Construct the value and update cache accordingly.
@@ -132,7 +132,7 @@ fetch (MemCache mv) key construct = do
132132
v <- liftBase . evaluate . force =<< construct
133133
liftBase $ do
134134
putMVar' mvEl $ Just v
135-
pure (v, True)
135+
pure (v, False)
136136
modifyMVar'_ mv $ \mc ->
137137
tryReadMVar' mvEl >>= \case
138138
Nothing -> do
@@ -172,7 +172,7 @@ fetch (MemCache mv) key construct = do
172172
Just (_, _, minEl, tidiedCache) -> go tidiedCache $ size - mcSizeFun mc minEl
173173
{-# INLINEABLE fetch #-}
174174

175-
-- | Fetch an object from cache or construct it if it's not there.
175+
-- | Variant of 'fetch' that doesn't return a 'Bool'.
176176
fetch_
177177
:: (Ord k, Hashable k, NFData v, MonadBaseControl IO m)
178178
=> MemCache k v

0 commit comments

Comments
 (0)