mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-08-15 07:40:46 +03:00
Refactor cache interaction
This commit is contained in:
parent
9300a1bc60
commit
596efc8734
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user