Refactor cache interaction

This commit is contained in:
Joshua Clayton 2017-04-20 06:34:56 -07:00
parent 9300a1bc60
commit 596efc8734
No known key found for this signature in database
GPG Key ID: 5B6558F77E9A8118

View File

@ -23,21 +23,19 @@ cached cachePrefix f =
writeCache :: ToRecord a => [a] -> Cache [a]
writeCache [] = return []
writeCache contents = do
liftIO $ D.createDirectoryIfMissing True cacheDirectory
(CacheFileName fileName) <- ask
liftIO $ BS.writeFile fileName $ encode contents
ensureCacheDirectoryExists
writeContentsToCacheFile contents =<< ask
return contents
readCache :: FromRecord a => Cache (Maybe [a])
readCache = do
(CacheFileName fileName) <- ask
readCache =
either
(const Nothing)
(processCsv . decode NoHeader)
<$> liftIO (safeReadFile fileName)
<$> (readFromCache =<< ask)
where
processCsv = either (const Nothing) (Just . V.toList)
readFromCache (CacheFileName fileName) = liftIO $ safeReadFile fileName
cacheFileName :: String -> IO (Either FingerprintOutcome CacheFileName)
cacheFileName context = do
@ -48,3 +46,11 @@ cacheFileName context = do
cacheDirectory :: String
cacheDirectory = "tmp/unused"
ensureCacheDirectoryExists :: Cache ()
ensureCacheDirectoryExists =
liftIO $ D.createDirectoryIfMissing True cacheDirectory
writeContentsToCacheFile :: ToRecord a => [a] -> CacheFileName -> Cache ()
writeContentsToCacheFile contents (CacheFileName fileName) =
liftIO $ BS.writeFile fileName $ encode contents