TCache 0.10.0.6 → 0.10.0.8
raw patch · 4 files changed
+28/−23 lines, 4 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Data.TCache.Memoization: cachedByKeySTM :: (Typeable a, Executable m) => String -> Int -> m a -> STM a
Files
- Data/TCache/IndexQuery.hs +2/−3
- Data/TCache/Memoization.hs +18/−12
- TCache.cabal +4/−2
- demos/DynamicSample.hs +4/−6
Data/TCache/IndexQuery.hs view
@@ -283,9 +283,8 @@ return [(zs, union xs ys) | (zs,xs) <- xss] --- | return all the (indexed) registers which has this field-indexOf :: (Queriable reg a )- => (reg -> a) -> STM [(a,[DBRef reg])]+-- | return all the (indexed) values which this field has and a DBRef pointer to the register+indexOf :: (Queriable reg a) => (reg -> a) -> STM [(a,[DBRef reg])] indexOf selector= do let [one, two]= typeRepArgs $! typeOf selector let rindex= getDBRef $! keyIndex one two
Data/TCache/Memoization.hs view
@@ -15,7 +15,7 @@ , ExistentialQuantification , FlexibleInstances , TypeSynonymInstances #-}-module Data.TCache.Memoization (writeCached,cachedByKey,flushCached,cachedp,addrStr,Executable(..))+module Data.TCache.Memoization (writeCached,cachedByKey,cachedByKeySTM,flushCached,cachedp,addrStr,Executable(..)) where import Data.Typeable@@ -64,7 +64,7 @@ writeResource _= return () delResource _= return ()- readResourceByKey k= error $ "access By key is undefined for cached objects.key= " ++ k+ readResourceByKey k= return Nothing -- error $ "access By key is undefined for cached objects.key= " ++ k readResource (Cached a f _ _)=do@@ -95,24 +95,27 @@ cached :: (Indexable a,Typeable a, Typeable b, Executable m,MonadIO m) => Int -> (a -> m b) -> a -> m b-cached time f a= do+cached time f a= liftIO . atomically $ cachedSTM time f a++cachedSTM time f a= do let prot= Cached a f undefined undefined- cho@(Cached _ _ b t) <- liftIO $ getResource prot `onNothing` fillIt prot+ let ref= getDBRef $ keyResource prot+ cho@(Cached _ _ b t) <- readDBRef ref `onNothing` fillIt ref prot case time of 0 -> return b _ -> do- TOD tnow _ <- liftIO $ getClockTime- if tnow - t > fromIntegral time+ TOD tnow _ <- unsafeIOToSTM $ getClockTime+ if tnow - t >= fromIntegral time then do- liftIO $ deleteResource cho- cached time f a+ Cached _ _ b _ <- fillIt ref prot+ return b else return b where -- has been invalidated by flushCached- fillIt proto= do- r <- return . fromJust =<< (readResource proto) -- !> "fillIt"- withResources [] $ const [r]- return r+ fillIt ref proto= do+ let r = unsafePerformIO $return . fromJust =<< readResource proto -- !> "fillIt"+ writeDBRef ref r+ return r -- | Memoize the result of a computation for a certain time. A string 'key' is used to index the result --@@ -120,6 +123,9 @@ -- . Time == 0 means no timeout cachedByKey :: (Typeable a, Executable m,MonadIO m) => String -> Int -> m a -> m a cachedByKey key time f = cached time (\_ -> f) key++cachedByKeySTM :: (Typeable a, Executable m) => String -> Int -> m a -> STM a+cachedByKeySTM key time f = cachedSTM time (\_ -> f) key -- Flush the cached object indexed by the key flushCached :: String -> IO ()
TCache.cabal view
@@ -1,5 +1,5 @@ name: TCache-version: 0.10.0.6+version: 0.10.0.8 cabal-version: >= 1.6 build-type: Simple license: BSD3@@ -13,7 +13,9 @@ The package implements serializable STM references, access by key and by record field value, triggers, full text and field indexation, default serialization and a query language based on record fields .- This version add memoization and a persistent and transactional collection/queue+ 0.10 version add memoization and a persistent and transactional collection/queue.+ .+ 0.10.0.8 subversion add cachedByKeySTM . See "Data.TCache" for details
demos/DynamicSample.hs view
@@ -15,11 +15,9 @@ --two objects with two different datatypes: Int and String instance Indexable Int where - key x= show x + key = show - - - + instance Indexable String where key x= take 2 x @@ -52,8 +50,8 @@ -- to use heterogeneous data in the same transaction, -- use DBRef's: s <- atomically $ do- let refInt = getDBRef $ keyResource x :: DBRef Int- refString = getDBRef $ keyResource "ho" :: DBRef String+ let refInt = getDBRef $ key x :: DBRef Int+ refString = getDBRef $ key "ho" :: DBRef String i <- readDBRef refInt writeDBRef refString $ "hola, the retrieved value of x is " ++ show i s <- readDBRef refString