-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Cache a function (a -> b) -- -- Please see README.md @package fcache @version 0.1.0.0 module Cache.Internal data Cache k v Cache :: Maybe Int -> Int -> HashMap k v -> Seq k -> Cache k v [_limit] :: Cache k v -> Maybe Int [_size] :: Cache k v -> Int [_cache] :: Cache k v -> HashMap k v [_queue] :: Cache k v -> Seq k class IsCache m k where newUnlimitedCache = newCache Nothing dump 0 cache = cache dump n cache = dump (n - 1) (dumpOldest cache) -- | newCache size. Set a max number of elements the cache will -- hold. -- -- If the newly inserted element exceeds the size limit, the oldest -- element is removed from the cache. newCache :: IsCache m k => Maybe Int -> m k v -- | Alias for newCache Nothing newUnlimitedCache :: IsCache m k => m k v -- | Lookup an element from the cache lookup :: IsCache m k => k -> m k v -> Maybe v -- | Insert an element to the cache. -- -- If the newly inserted element exceeds the size limit, the oldest -- element is removed from the cache. insert :: IsCache m k => k -> v -> m k v -> m k v -- | Remove the oldest key from the cache. dumpOldest :: IsCache m k => m k v -> m k v -- | Return the current number of elements. size :: IsCache m k => m k v -> Int -- | Return the max number of elements the cache can hold. limit :: IsCache m k => m k v -> Maybe Int -- | Change the size limit. setLimit :: IsCache m k => Maybe Int -> m k v -> m k v -- | dump n cache removes n oldest elements from the cache. dump :: IsCache m k => Int -> m k v -> m k v instance (GHC.Classes.Eq k, GHC.Classes.Eq v) => GHC.Classes.Eq (Cache.Internal.Cache k v) instance (GHC.Show.Show k, GHC.Show.Show v) => GHC.Show.Show (Cache.Internal.Cache k v) instance (Data.Hashable.Class.Hashable k, GHC.Classes.Eq k) => Cache.Internal.IsCache Cache.Internal.Cache k -- | This module has everything you need to cache a function (a -> -- b) -- --
--   let cache = newCache Nothing :: Cache Int Int
--   let f = do
--      withCache doSomethingSlow 3 -- Slow; the result is cached in a map using 3 as the key.
--      withCache doSomethingSlow 3 -- Read from the cache.
--      withCache doSomethingSlow 3 :: SCache Int Int
--   evalState f cache 
--   
module Cache.State data Cache k v type SCacheT k v n = StateT (Cache k v) n v type SCache k v = State (Cache k v) v class (IsCache m k, Monad n) => IsSCacheT m k n where withCache action k = do { cache <- get; case lookup k cache of { Just v -> return v Nothing -> do { v <- lift $ action k; modify (insert k v); return v } } } -- | withCache action x, "x" is an argument for the action. -- -- If "x" is a key in the cache, ignore the "action" and return the -- cached value; -- -- Otherwise perform the action and cache the result using "x" as a key. withCache :: IsSCacheT m k n => (k -> n v) -> k -> StateT (m k v) n v instance (Cache.Internal.IsCache m k, GHC.Base.Monad n) => Cache.State.IsSCacheT m k n