The idea here is that it’s necessary-but-painful to read in your big,
convoluted file every time your program runs. It would be convenient
if you could cache the value you read out of the file, and only do the
painful file reading when the original big, convoluted file changes on
the filesystem. So that’s what CacheFile
does.
import Data.CacheFile
painful_read :: FilePath -> IO SomeBinaryInstance
painful_read = ...
read_cached = cacheFile painful_read
main = do ouch <- read_cached "file"
ahhh <- read_cached "file"
return (ouch == ahhh)
Reading ouch
will then, presumably, take an uncomfortably long
amount of time. The read into ahhh
pleasurably short (the speed of
a decodeFile
from Data.Binary
).
The various cacheFile*
functions check whether the file you want to
read has already been cached, and if so, whether the cache is stale
(just using mtime at the moment). If it is, it runs the original
painful read, and caches the result. Otherwise, it just reads the
cache file.