summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/ResourceProvider/MetadataCache.hs
blob: 959cddebd2063abf603b558994541863df6f5d8a (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--------------------------------------------------------------------------------
module Hakyll.Core.ResourceProvider.MetadataCache
    ( resourceMetadata
    , resourceBody
    , resourceInvalidateMetadataCache
    ) where


--------------------------------------------------------------------------------
import           Hakyll.Core.Identifier
import           Hakyll.Core.Metadata
import           Hakyll.Core.ResourceProvider.Internal
import           Hakyll.Core.ResourceProvider.Metadata
import qualified Hakyll.Core.Store                     as Store


--------------------------------------------------------------------------------
resourceMetadata :: ResourceProvider -> Identifier -> IO Metadata
resourceMetadata rp r = do
    load rp r
    Store.Found md <- Store.get (resourceStore rp)
        [name, toFilePath r, "metadata"]
    return md


--------------------------------------------------------------------------------
resourceBody :: ResourceProvider -> Identifier -> IO String
resourceBody rp r = do
    load rp r
    Store.Found bd <- Store.get (resourceStore rp)
        [name, toFilePath r, "body"]
    maybe (resourceString r) return bd


--------------------------------------------------------------------------------
resourceInvalidateMetadataCache :: ResourceProvider -> Identifier -> IO ()
resourceInvalidateMetadataCache rp r = do
    Store.delete (resourceStore rp) [name, toFilePath r, "metadata"]
    Store.delete (resourceStore rp) [name, toFilePath r, "body"]


--------------------------------------------------------------------------------
load :: ResourceProvider -> Identifier -> IO ()
load rp r = do
    mmd <- Store.get store mdk :: IO (Store.Result Metadata)
    case mmd of
        -- Already loaded
        Store.Found _  -> return ()
        -- Not yet loaded
        _ -> do
            (metadata, body) <- loadMetadata rp r
            Store.set store mdk metadata
            Store.set store bk  body
  where
    store = resourceStore rp
    mdk   = [name, toFilePath r, "metadata"]
    bk    = [name, toFilePath r, "body"]


--------------------------------------------------------------------------------
name :: String
name = "Hakyll.Core.Resource.Provider.MetadataCache"