filecache 0.5.0 → 0.5.1
raw patch · 3 files changed
+22/−8 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Data.FileCache: queryWith :: forall e a. IsString e => FileCacheR e a -> FilePath -> OnModified -> IO (Either e a) -> IO (Either e a)
+ Data.FileCache: type OnModified = IO ()
+ Data.FileCache.Internal: queryWith :: forall e a. IsString e => FileCacheR e a -> FilePath -> OnModified -> IO (Either e a) -> IO (Either e a)
+ Data.FileCache.Internal: type OnModified = IO ()
- Data.FileCache.Internal: FileCache :: TVar (Map FilePath (Either r a)) -> TVar (Map FilePath (Set FilePath, StopListening)) -> WatchManager -> EventChannel -> TVar (Maybe ThreadId) -> FileCacheR r a
+ Data.FileCache.Internal: FileCache :: TVar (Map FilePath (Either r a, OnModified)) -> TVar (Map FilePath (Set FilePath, StopListening)) -> WatchManager -> EventChannel -> TVar (Maybe ThreadId) -> FileCacheR r a
- Data.FileCache.Internal: [_cache] :: FileCacheR r a -> TVar (Map FilePath (Either r a))
+ Data.FileCache.Internal: [_cache] :: FileCacheR r a -> TVar (Map FilePath (Either r a, OnModified))
Files
- filecache.cabal +2/−1
- src/Data/FileCache.hs +1/−1
- src/Data/FileCache/Internal.hs +19/−6
filecache.cabal view
@@ -1,5 +1,5 @@ name: filecache-version: 0.5.0+version: 0.5.1 synopsis: A cache system associating values to files. description: A cache system, that works by associating computation results with file names. When the files are modified, the cache entries are discarded. homepage: http://lpuppet.banquise.net/@@ -12,6 +12,7 @@ build-type: Simple cabal-version: >=1.10 bug-reports: https://github.com/bartavelle/filecache/issues+tested-with: GHC == 9.8.1 || ==9.6.4 || ==9.4.8 || ==9.2.8 source-repository head type: git
src/Data/FileCache.hs view
@@ -9,6 +9,6 @@ The computation will be used to populate the cache if this call results in a miss. The result is forced to WHNM. -}-module Data.FileCache (FileCache, FileCacheR, newFileCache, killFileCache, invalidate, query, getCache, lazyQuery) where+module Data.FileCache (FileCache, FileCacheR, newFileCache, killFileCache, invalidate, query, OnModified, queryWith, getCache, lazyQuery) where import Data.FileCache.Internal
src/Data/FileCache/Internal.hs view
@@ -26,7 +26,7 @@ -- type must be an instance of 'Error'. data FileCacheR r a = FileCache- { _cache :: TVar (M.Map FilePath (R.Either r a))+ { _cache :: TVar (M.Map FilePath (R.Either r a, OnModified)) , _watchedDirs :: TVar (M.Map FilePath (S.Set FilePath, StopListening)) , _manager :: WatchManager , _channel :: EventChannel@@ -36,6 +36,10 @@ -- | A default type synonym, for String errors. type FileCache = FileCacheR String +-- | Hook to invoke after an entry is removed from the cache+-- (because the corresponding file has been modified)+type OnModified = IO ()+ -- | Generates a new file cache. The opaque type is for use with other -- functions. newFileCache :: IO (FileCacheR r a)@@ -49,9 +53,10 @@ let cfp = eventPath e dir = addTrailingPathSeparator (takeDirectory cfp) join $ atomically $ do+ onModified <- maybe (return ()) snd . M.lookup cfp <$> readTVar tcache modifyTVar tcache $ M.delete cfp wdirs <- readTVar wcache- case M.lookup dir wdirs of+ (onModified >>) <$> case M.lookup dir wdirs of Nothing -> return $ return () Just (watched, stop) -> let watched' = S.delete cfp watched@@ -88,7 +93,16 @@ -> FilePath -- ^ Path of the file entry -> IO (R.Either e a) -- ^ The computation that will be used to populate the cache -> IO (R.Either e a)-query f@(FileCache tcache twatched wm chan tmtid) fp action = do+query q fp = queryWith q fp (return ())++-- | Generalization of 'query' that allows to specify an 'OnModified' hook+queryWith :: forall e a. IsString e+ => FileCacheR e a+ -> FilePath -- ^ Path of the file entry+ -> OnModified+ -> IO (R.Either e a) -- ^ The computation that will be used to populate the cache+ -> IO (R.Either e a)+queryWith f@(FileCache tcache twatched wm chan tmtid) fp onModified action = do mtid <- readTVarIO tmtid case mtid of Nothing -> return (R.Left (fromString "Closed cache"))@@ -105,7 +119,7 @@ withWatch canonical value = value <$ (addWatch canonical value `catchAll` traceShowM ) addWatch canonical value = join $ atomically $ do let cpath = addTrailingPathSeparator (takeDirectory canonical)- modifyTVar tcache (M.insert canonical value)+ modifyTVar tcache (M.insert canonical (value, onModified)) watched <- readTVar twatched case M.lookup cpath watched of Nothing -> return $ do@@ -131,5 +145,4 @@ -- | Gets a copy of the cache. getCache :: FileCacheR e a -> IO (M.Map FilePath (R.Either e a))-getCache = atomically . readTVar . _cache-+getCache = atomically . fmap (fmap fst) . readTVar . _cache