2019-04-17 12:48:41 +03:00
|
|
|
module Hasura.Cache
|
2019-11-25 20:12:23 +03:00
|
|
|
( Cache
|
|
|
|
, CacheOptions
|
|
|
|
, mkCacheOptions
|
|
|
|
, B.CacheSize
|
|
|
|
, B.mkCacheSize
|
|
|
|
|
|
|
|
, initialise
|
2019-04-17 12:48:41 +03:00
|
|
|
, lookup
|
2019-11-25 20:12:23 +03:00
|
|
|
, insert
|
|
|
|
, clear
|
|
|
|
, getEntries
|
2019-04-17 12:48:41 +03:00
|
|
|
) where
|
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
import Hasura.Prelude hiding (lookup)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
import qualified Hasura.Cache.Bounded as B
|
|
|
|
import qualified Hasura.Cache.Unbounded as U
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
data Cache k v
|
|
|
|
= CacheBounded !(B.BoundedCache k v)
|
|
|
|
| CacheUnbounded !(U.UnboundedCache k v)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
newtype CacheOptions
|
|
|
|
= CacheOptions (Maybe B.CacheSize)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
mkCacheOptions :: Maybe B.CacheSize -> CacheOptions
|
|
|
|
mkCacheOptions = CacheOptions
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
initialise :: CacheOptions -> IO (Cache k v)
|
|
|
|
initialise (CacheOptions cacheSizeM) =
|
|
|
|
case cacheSizeM of
|
|
|
|
Nothing -> CacheUnbounded <$> U.initialise
|
|
|
|
Just cacheSize -> CacheBounded <$> B.initialise cacheSize
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
lookup :: (Hashable k, Ord k) => k -> Cache k v -> IO (Maybe v)
|
|
|
|
lookup k = \case
|
|
|
|
CacheBounded cache -> B.lookup k cache
|
|
|
|
CacheUnbounded cache -> U.lookup k cache
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
insert :: (Hashable k, Ord k) => k -> v -> Cache k v -> IO ()
|
|
|
|
insert k v = \case
|
|
|
|
CacheBounded cache -> B.insert k v cache
|
|
|
|
CacheUnbounded cache -> U.insert k v cache
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
clear :: Cache k v -> IO ()
|
|
|
|
clear = \case
|
|
|
|
CacheBounded cache -> B.clear cache
|
|
|
|
CacheUnbounded cache -> U.clear cache
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2019-11-25 20:12:23 +03:00
|
|
|
getEntries :: (Hashable k, Ord k) => Cache k v -> IO [[(k, v)]]
|
|
|
|
getEntries = \case
|
|
|
|
CacheBounded cache -> B.getEntries cache
|
|
|
|
CacheUnbounded cache -> U.getEntries cache
|