From ea1236f2cf6d3ef4b739b2ca28f47a3bbed42295 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Tue, 17 Oct 2023 14:14:48 +0200 Subject: refactor history --- app/History/Cache.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 app/History/Cache.hs (limited to 'app/History/Cache.hs') diff --git a/app/History/Cache.hs b/app/History/Cache.hs new file mode 100644 index 0000000..af40a84 --- /dev/null +++ b/app/History/Cache.hs @@ -0,0 +1,26 @@ +module History.Cache (cached) where + +import Data.Binary (Binary, decodeFileOrFail, encodeFile) +import Data.Text qualified as T +import System.Directory (createDirectoryIfMissing, doesFileExist, getCurrentDirectory) + +cached :: Binary a => T.Text -> (T.Text -> IO a) -> IO a +cached hash func = do + -- FIXME Cache inside Git root + -- + -- The cache location should not be dependant on the current directory, but + -- should be placed alongside the `.git` directory. + cwd <- getCurrentDirectory + createDirectoryIfMissing True (cwd ++ "/.anissue") + let file = (cwd ++ "/.anissue/" ++ T.unpack hash) + doesFileExist file >>= \case + True -> + decodeFileOrFail file >>= \case + Left _ -> generate file + Right blob -> pure blob + False -> generate file + where + generate file = do + blob <- func hash + encodeFile file blob + pure blob -- cgit v1.2.3