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