diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-12-07 03:55:45 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-12-07 03:55:47 +0100 |
commit | 3c6e62b75293b6625509ade3c278fc2d4d147c30 (patch) | |
tree | b33f76c2634a771879f9178cff8e5335e43d2f43 /app/History/Cache.hs | |
parent | a5dde0c6e1c1f54a1660f6c2345277927beef30f (diff) |
chore: increase performance by caching everything
Initial cache generation is slower, as we are losing out on parallelism.
Diffstat (limited to 'app/History/Cache.hs')
-rw-r--r-- | app/History/Cache.hs | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/app/History/Cache.hs b/app/History/Cache.hs index d0473e2..978f3d9 100644 --- a/app/History/Cache.hs +++ b/app/History/Cache.hs @@ -1,24 +1,33 @@ -module History.Cache (cached) where +module History.Cache + ( cached, + cachedMaybe, + ) +where import Data.Binary (Binary, decodeFileOrFail, encodeFile) import Data.Text qualified as T +import Debug import Git qualified import System.Directory (createDirectoryIfMissing, doesFileExist) import System.FilePath ((</>)) -cached :: Binary a => T.Text -> (T.Text -> IO a) -> IO a -cached hash func = do +cached :: Binary a => T.Text -> IO a -> IO a +cached key func = do root <- Git.getRootDir createDirectoryIfMissing True (root </> ".anissue") - let file = (root </> ".anissue" </> T.unpack hash) + let file = (root </> ".anissue" </> T.unpack key) doesFileExist file >>= \case True -> decodeFileOrFail file >>= \case - Left _ -> generate file + Left e -> debug "e" e `seq` generate file Right blob -> pure blob False -> generate file where generate file = do - blob <- func hash - encodeFile file blob + blob <- func + encodeFile (debug "cache miss" file) blob pure blob + +cachedMaybe :: Binary a => Maybe T.Text -> IO a -> IO a +cachedMaybe Nothing func = func +cachedMaybe (Just key) func = cached key func |