mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-08-16 08:10:55 +03:00
Use internal CacheFileName newtype for referencing where the cache lives
This commit is contained in:
parent
cfa194b936
commit
a5b8f31e4d
@ -10,23 +10,26 @@ import Data.Vector (toList)
|
||||
import qualified Data.ByteString.Lazy as BS
|
||||
import Unused.Cache.DirectoryFingerprint (sha)
|
||||
|
||||
newtype CacheFileName = CacheFileName String
|
||||
type Cache = ReaderT CacheFileName IO
|
||||
|
||||
cached :: (FromRecord a, ToRecord a) => String -> IO [a] -> IO [a]
|
||||
cached context f =
|
||||
runReaderT fromCache =<< cacheFileName context
|
||||
where
|
||||
fromCache = maybe (writeCache =<< liftIO f) return =<< readCache
|
||||
|
||||
writeCache :: ToRecord a => [a] -> ReaderT String IO [a]
|
||||
writeCache :: ToRecord a => [a] -> Cache [a]
|
||||
writeCache [] = return []
|
||||
writeCache contents = do
|
||||
liftIO $ createDirectoryIfMissing True cacheDirectory
|
||||
fileName <- ask
|
||||
(CacheFileName fileName) <- ask
|
||||
liftIO $ BS.writeFile fileName $ encode contents
|
||||
return contents
|
||||
|
||||
readCache :: FromRecord a => ReaderT String IO (Maybe [a])
|
||||
readCache :: FromRecord a => Cache (Maybe [a])
|
||||
readCache = do
|
||||
fileName <- ask
|
||||
(CacheFileName fileName) <- ask
|
||||
exists <- liftIO $ doesFileExist fileName
|
||||
|
||||
if exists
|
||||
@ -35,11 +38,11 @@ readCache = do
|
||||
where
|
||||
processCsv = either (const Nothing) (Just . toList)
|
||||
|
||||
cacheFileName :: String -> IO String
|
||||
cacheFileName :: String -> IO CacheFileName
|
||||
cacheFileName context = do
|
||||
putStrLn "\n\nCalculating cache fingerprint... "
|
||||
currentSha <- sha
|
||||
return $ cacheDirectory ++ "/" ++ context ++ "-" ++ currentSha ++ ".csv"
|
||||
return $ CacheFileName $ cacheDirectory ++ "/" ++ context ++ "-" ++ currentSha ++ ".csv"
|
||||
|
||||
cacheDirectory :: String
|
||||
cacheDirectory = "tmp/unused"
|
||||
|
Loading…
Reference in New Issue
Block a user