packages feed

cache 0.1.2.0 → 0.1.3.0

raw patch · 3 files changed

+33/−4 lines, 3 filesdep ~transformersPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: transformers

API changes (from Hackage documentation)

+ Data.Cache: filterWithKey :: (Eq k, Hashable k) => (k -> v -> Bool) -> Cache k v -> IO ()
+ Data.Cache: purge :: (Eq k, Hashable k) => Cache k v -> IO ()
+ Data.Cache: toList :: Cache k v -> IO [(k, v, Maybe TimeSpec)]
- Data.Cache: fetchWithCache :: (Eq k, Hashable k) => Cache k v -> k -> (k -> IO v) -> IO v
+ Data.Cache: fetchWithCache :: (Eq k, Hashable k, MonadIO m) => Cache k v -> k -> (k -> m v) -> m v

Files

cache.cabal view
@@ -1,5 +1,5 @@ name:           cache-version:        0.1.2.0+version:        0.1.3.0 synopsis:       An in-memory key/value store with expiration support homepage:       https://github.com/hverr/haskell-cache#readme license:        BSD3
src/Data/Cache.hs view
@@ -40,6 +40,8 @@     -- ** Deletion   , delete   , deleteSTM+  , filterWithKey+  , purge   , purgeExpired   , purgeExpiredSTM     -- ** Combined actions@@ -48,6 +50,7 @@     -- * Cache information   , size   , sizeSTM+  , toList ) where  import Prelude hiding (lookup)@@ -55,6 +58,7 @@ import Control.Concurrent.STM import Control.Monad import Control.Monad.Trans.Maybe+import Control.Monad.IO.Class import Data.Cache.Internal import qualified Data.HashMap.Strict as HM import Data.Hashable@@ -203,14 +207,14 @@  -- | Get a value from cache. If not available from cache, use the provided action and update the cache. -- Note that the cache check and conditional execution of the action is not one atomic action.-fetchWithCache :: (Eq k, Hashable k) => Cache k v -> k -> (k -> IO v) -> IO v+fetchWithCache :: (Eq k, Hashable k, MonadIO m) => Cache k v -> k -> (k -> m v) -> m v fetchWithCache c k f = do-  mv <- lookup c k+  mv <- liftIO $ lookup c k   case mv of     Just v -> return v     Nothing -> do        v <- f k-       insert c k v+       liftIO $ insert c k v        return v  -- | STM variant of 'keys'.@@ -224,6 +228,15 @@ now :: IO TimeSpec now = getTime Monotonic +-- | Keeps elements that satify a predicate (used for cache invalidation).+-- Note that the predicate might be called for expired items.+filterWithKey :: (Eq k, Hashable k) => (k -> v -> Bool) -> Cache k v -> IO ()+filterWithKey f c = atomically $ writeTVar c' =<< (HM.filterWithKey (\k (CacheItem v _) -> f k v) <$> readTVar c') where c' = container c++-- | Delete all elements (cache invalidation).+purge :: (Eq k, Hashable k) => Cache k v -> IO ()+purge c = atomically $ writeTVar v HM.empty where v = container c+ -- | STM variant of 'purgeExpired'. -- -- The 'TimeSpec' argument should be the current 'Monotonic' time, i.e.@@ -237,6 +250,14 @@ purgeExpired :: (Eq k, Hashable k) => Cache k v -> IO () purgeExpired c = (atomically . purgeExpiredSTM c) =<< now +-- | Returns the cache content as a list.+-- The third element of the tuple is the expiration date. Nothing means that it doesn't expire.+toList :: Cache k v -> IO [(k, v, Maybe TimeSpec)]+toList c = atomically $ do+  m <- readTVar $ container c+  let l = HM.toList m+  return $ map (\(k, (CacheItem v i)) -> (k, v, i)) l+   -- $use -- -- All operations are automically executed in the IO monad. The
test/Data/CacheSpec.hs view
@@ -48,6 +48,12 @@         liftIO (lookup' c (fst notExpired)  ) >>= (`shouldBe` Just (snd notExpired))         liftIO (lookup' c (fst expired)     ) >>= (`shouldBe` Nothing)         liftIO (lookup' c (fst autoExpired) ) >>= (`shouldBe` Just (snd autoExpired))+    it "should filter items" $ do+        c <- liftIO $ defCache Nothing+        _ <- liftIO $ expire defExpiration+        liftIO (size c) >>= (`shouldBe` 4)+        _ <- liftIO $ filterWithKey (\r _ -> r /= fst ok) c+        liftIO (size c) >>= (`shouldBe` 3)     it "should copy" $ do         c  <- liftIO $ defCache Nothing         c' <- liftIO $ copyCache c@@ -67,6 +73,8 @@         liftIO (size c) >>= (`shouldBe` 4)         _ <- liftIO $ purgeExpired c         liftIO (size c) >>= (`shouldBe` 3)+        _ <- liftIO $ purge c+        liftIO (size c) >>= (`shouldBe` 0)     it "should work with actions" $ do         c <- liftIO $ defCache Nothing         _ <- liftIO $ expire defExpiration