-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
609 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
module Cache ( | ||
Cache, | ||
CacheMonad, | ||
HasCache, | ||
getCache, | ||
getCacheM, | ||
initCache, | ||
withCache, | ||
withCacheM, | ||
) where | ||
|
||
import Control.Concurrent.MVar | ||
import Control.Monad | ||
import Control.Monad.IO.Class | ||
import Control.Monad.State | ||
|
||
import Data.Map (Map) | ||
import qualified Data.Map as M | ||
|
||
-- Opaque cache type storing values of type v against keys of type k | ||
data Cache k v = Cache (MVar (Map k v)) | ||
|
||
instance Show (Cache k v) where | ||
show _ = "(cache)" | ||
|
||
-- Initialize an empty cache | ||
initCache :: MonadIO m => m (Cache k v) | ||
initCache = liftM Cache $ liftIO $ newMVar M.empty | ||
|
||
-- Cache the result of monadic action under a certain key | ||
withCache :: (MonadIO m, Ord k) => Cache k v -> k -> m v -> m v | ||
withCache (Cache cache) key action = do | ||
values <- liftIO $ readMVar cache | ||
case M.lookup key values of | ||
Just val -> return val | ||
Nothing -> do | ||
val <- action | ||
liftIO $ modifyMVar_ cache $ return . insertIfMissing key val | ||
return val | ||
|
||
-- Class for data structures with a cache field | ||
class HasCache k v a where | ||
getCache :: a -> Cache k v | ||
|
||
-- Monads that have caches | ||
class CacheMonad k v m where | ||
getCacheM :: m (Cache k v) | ||
|
||
-- If the state has cache, it can be used | ||
instance (MonadState s m, HasCache k v s) => CacheMonad k v m where | ||
getCacheM = gets getCache | ||
|
||
withCacheM :: (MonadIO m, CacheMonad k v m, Ord k) => k -> m v -> m v | ||
withCacheM key action = do | ||
cache <- getCacheM | ||
withCache cache key action | ||
|
||
-- Insert the new value only if there's not an existing one | ||
insertIfMissing :: Ord k => k -> v -> Map k v -> Map k v | ||
insertIfMissing = M.insertWith (flip const) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.