1
0
forked from GitHub/gf-core

(fastcgi) replace DataRef with a more general Cache type, which can hold several PGF grammars.

This commit is contained in:
bjorn
2008-10-17 14:12:53 +00:00
parent 1ddb738459
commit e7656c9d2e
4 changed files with 47 additions and 33 deletions

36
src/server/Cache.hs Normal file
View File

@@ -0,0 +1,36 @@
module Cache (Cache,newCache,readCache) where
import Control.Concurrent
import Data.Map (Map)
import qualified Data.Map as Map
import System.Directory (getModificationTime)
import System.Time (ClockTime)
data Cache a = Cache {
cacheLoad :: FilePath -> IO a,
cacheObjects :: MVar (Map FilePath (MVar (ClockTime, a)))
}
newCache :: (FilePath -> IO a) -> IO (Cache a)
newCache load =
do objs <- newMVar Map.empty
return $ Cache { cacheLoad = load, cacheObjects = objs }
readCache :: Cache a -> FilePath -> IO a
readCache c file =
do t' <- getModificationTime file
objs <- takeMVar (cacheObjects c)
case Map.lookup file objs of
-- object is in cache
Just v -> do (t,x) <- takeMVar v
putMVar (cacheObjects c) objs
-- check timestamp
x' <- if t == t' then return x else cacheLoad c file
putMVar v (t',x')
return x'
-- first time this object is requested
Nothing -> do v <- newEmptyMVar
putMVar (cacheObjects c) (Map.insert file v objs)
x' <- cacheLoad c file
putMVar v (t',x')
return x'