add better server error messages, cbor vs raw hex

This commit is contained in:
Emily Martins 2022-06-27 13:30:27 +02:00
parent a5765a355d
commit 6a7e8369fd
7 changed files with 109 additions and 97 deletions

View file

@ -6,7 +6,8 @@
-}
module Data.Cache.Cached (
cached,
cachedFor,
cachedM,
cachedForM,
) where
import Control.Monad.IO.Class (MonadIO (liftIO))
@ -20,7 +21,14 @@ import System.Clock (TimeSpec)
Uses a HashMap under the hood.
-}
cached :: (Monad m, MonadIO m, Hashable k, Ord k) => (k -> v) -> IO (k -> m v)
cached = cachedFor Nothing
cached f = cachedForM Nothing (pure . f)
{- | 'cachedFor' but items last forever.
Uses a HashMap under the hood.
-}
cachedM :: (Monad m, MonadIO m, Hashable k, Ord k) => (k -> m v) -> IO (k -> m v)
cachedM = cachedForM Nothing
{- | Create a cached version of a function tainting result with MonadIO context.
@ -28,13 +36,13 @@ cached = cachedFor Nothing
Uses a HashMap under the hood.
-}
cachedFor :: (Monad m, MonadIO m, Hashable k, Ord k) => Maybe TimeSpec -> (k -> v) -> IO (k -> m v)
cachedFor t f =
cachedForM :: (Monad m, MonadIO m, Hashable k, Ord k) => Maybe TimeSpec -> (k -> m v) -> IO (k -> m v)
cachedForM t f =
Cache.newCache t <&> \cache k -> do
res <- liftIO $ Cache.lookup cache k
case res of
Nothing -> do
let v = f k
v <- f k
liftIO $ Cache.insert cache k v
pure v
Just v -> do