on e8e0f21 help sign in
28 lines 1.1 kB history raw
1module Text.Hakyll.Internal.Cache
2 ( storeInCache
3 , getFromCache
4 ) where
6import Control.Monad.Reader (liftIO)
7import Text.Hakyll.Hakyll (Hakyll)
8import Text.Hakyll.File
9import Data.Binary
11-- | We can store all datatypes instantiating @Binary@ to the cache. The cache
12-- directory is specified by the @HakyllConfiguration@, usually @_cache@.
13storeInCache :: (Binary a) => a -> FilePath -> Hakyll ()
14storeInCache value path = do
15 cachePath <- toCache path
16 makeDirectories cachePath
17 liftIO $ encodeFile cachePath value
19-- | Get a value from the cache. The filepath given should not be located in the
20-- cache. This function performs a timestamp check on the filepath and the
21-- filepath in the cache, and only returns the cached value when it is still
22-- up-to-date.
23getFromCache :: (Binary a) => FilePath -> Hakyll (Maybe a)
24getFromCache path = do
25 cachePath <- toCache path
26 valid <- isMoreRecent cachePath [path]
27 if valid then liftIO (decodeFile cachePath) >>= return . Just
28 else return Nothing