blob: af40a8465fcc138d953ef5a776e0cb9f222ef094 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
|