aboutsummaryrefslogtreecommitdiffstats
path: root/app/Cache.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-07 03:58:21 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-07 04:01:23 +0100
commitc1ff403387064ff0027b9e762cc6f6a8fa20c8d9 (patch)
tree62b9a4cfccfc4fce42055ef94b9de41d1dfcc0a7 /app/Cache.hs
parent3c6e62b75293b6625509ade3c278fc2d4d147c30 (diff)
chore: move remaining `History.*` modules outside of `History`
Diffstat (limited to 'app/Cache.hs')
-rw-r--r--app/Cache.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/app/Cache.hs b/app/Cache.hs
new file mode 100644
index 0000000..52d18ca
--- /dev/null
+++ b/app/Cache.hs
@@ -0,0 +1,33 @@
+module 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 -> IO a -> IO a
+cached key func = do
+ root <- Git.getRootDir
+ createDirectoryIfMissing True (root </> ".anissue")
+ let file = (root </> ".anissue" </> T.unpack key)
+ doesFileExist file >>= \case
+ True ->
+ decodeFileOrFail file >>= \case
+ Left e -> debug "e" e `seq` generate file
+ Right blob -> pure blob
+ False -> generate file
+ where
+ generate file = do
+ 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