on master help sign in
Close open cache files as soon as possible
ec1f961Jasper Van der Jeugt
src/Hakyll/Core/Store.hs+24−15
@@ -15,20 +15,22 @@ module Hakyll.Core.Store
15
15
16
16
17--------------------------------------------------------------------------------
17--------------------------------------------------------------------------------
18−import Control.Applicative ((<$>))
18+import Control.Applicative ((<$>))
19−import Control.Exception (IOException, handle)
19+import Control.Exception (IOException, handle)
20−import qualified Crypto.Hash.MD5 as MD5
20+import qualified Crypto.Hash.MD5 as MD5
21−import Data.Binary (Binary, decodeFile, encodeFile)
21+import Data.Binary (Binary, decode, encodeFile)
22−import qualified Data.ByteString as B
22+import qualified Data.ByteString as B
23−import qualified Data.Cache.LRU.IO as Lru
23+import qualified Data.ByteString.Lazy as BL
24+import qualified Data.Cache.LRU.IO as Lru
24−import Data.List (intercalate)
25+import Data.List (intercalate)
25−import qualified Data.Text as T
26+import qualified Data.Text as T
26−import qualified Data.Text.Encoding as T
27+import qualified Data.Text.Encoding as T
27−import Data.Typeable (TypeRep, Typeable, cast, typeOf)
28+import Data.Typeable (TypeRep, Typeable, cast, typeOf)
28−import System.Directory (createDirectoryIfMissing)
29+import System.Directory (createDirectoryIfMissing)
29−import System.Directory (doesFileExist, removeFile)
30+import System.Directory (doesFileExist, removeFile)
30−import System.FilePath ((</>))
31+import System.FilePath ((</>))
32+import System.IO (IOMode (..), hClose, openFile)
31−import Text.Printf (printf)
33+import Text.Printf (printf)
32
34
33
35
34--------------------------------------------------------------------------------
36--------------------------------------------------------------------------------
@@ -132,7 +134,7 @@ get store identifier = do
132 then return NotFound
134 then return NotFound
133 -- Found in the filesystem
135 -- Found in the filesystem
134 else do
136 else do
135− v <- decodeFile path
137+ v <- decodeClose
136 cacheInsert store key v
138 cacheInsert store key v
137 return $ Found v
139 return $ Found v
138 -- Found in the in-memory map (or wrong type), just return
140 -- Found in the in-memory map (or wrong type), just return
@@ -141,6 +143,13 @@ get store identifier = do
141 key = hash identifier
143 key = hash identifier
142 path = storeDirectory store </> key
144 path = storeDirectory store </> key
143
145
146+ -- 'decodeFile' from Data.Binary which closes the file ASAP
147+ decodeClose = do
148+ h <- openFile path ReadMode
149+ lbs <- BL.hGetContents h
150+ BL.length lbs `seq` hClose h
151+ return $ decode lbs
152+
144
153
145--------------------------------------------------------------------------------
154--------------------------------------------------------------------------------
146-- | Delete an item
155-- | Delete an item