Cache.icl 728 Bytes
Newer Older
1 2 3
implementation module Cache

import StdFunc
4
import StdTuple
5 6 7 8 9 10 11 12 13 14 15
from Data.Func import $
import Control.Monad
import Control.Applicative
import Data.Functor
import Crypto.Hash.MD5
import Text.JSON
import Data.Error
import StdFile
import System.FilePath
import System.File
import Data.Tuple
16 17

CACHE_DIR :== "./cache"
18 19

toCacheFile :: (a -> FilePath) | toString a
20
toCacheFile = ((</>) CACHE_DIR) o md5 o toString
21

22
readCache :: !a -> *World -> (Maybe b, !*World) | toString a & JSONDecode{|*|} b
Mart Lubbers's avatar
Mart Lubbers committed
23
readCache k = appFst (join o fmap (fromJSON o fromString) o error2mb) o readFile (toCacheFile k)
24

25
writeCache :: !a !b -> *World -> *World | toString a & JSONEncode{|*|} b
26
writeCache k v = snd o writeFile (toCacheFile k) (toString $ toJSON v)