TCache-0.6.4: A Transactional data cache with configurable persistence

Data.TCache.TMVar.Dynamic

Description

Data.TCache.TMVar.Dynamic: A dynamic interface for TCache using TMVars

Dynamic present essentially the same methods than Data.TCache. The added functionality is the management of IDynamic types. Any datatype that is instance of IResource and Typeable can be handled mixed with any other datatype. TCache.Dynamic is essentially a TCache working with a single datatype: IDynamic that is indexable and serializable. You dont need to do anything special except to define the IResource and typeable instances for your particular datatype. Also, before use, your datatype must be registered (with registerType, see example in the package).

there are basically two types of methods:

  • *Resource(s) : manage one type of data, Are the same than Data.TCache. The marsalling to and from IDynamic is managed internally
  • *DResource(s): handle the IDynamic type. you must wrap your datatype (with toIDyn) and unwap it (with fromIDyn)

The first set allows different modules to handle their particular kind of data without regard that it is being handled in the same cache with other datatypes.

The second set allows to handle, transact etc with many datatypes at the same time.

There is also a useful Key object whose purpose is to retrieve any objecto fo any datatype by its sting key

Also the parameter refcache has been dropped from the methods that used it (the syncronization methods)

Synopsis

Documentation

class IResource a whereSource

Interface that must be defined for every object being cached. readResource and writeResource are implemented by default as read-write to files with its key as filename serialize and deserialize are specified just to allow these defaults. If you define your own persistence, then serialize and deserialize are not needed. The package Workflow need them anyway.

minimal definition: keyResource, serialize, deserialize

While serialize and deserialize are agnostic about the way of converison to strings, either binary or textual, treadp and tshowp use the monad defined in the RefSerialize package. Both ways of serialization are alternative. one is defined by default in terms of the other. the RefSerialize monad has been introduced to permit IResource objects to be serialized as part of larger structures that embody them. This is necessary for the Workdlow package.

The keyResource string must be a unique since this is used to index it in the hash table. when accessing a resource, the user must provide a partial object for wich the key can be obtained. for example:

data Person= Person{name, surname:: String, account :: Int ....)

keyResource Person n s ...= n++s

the data being accesed must have the fields used by keyResource filled. For example

  readResource Person {name=John, surname= Adams}

leaving the rest of the fields undefined

IResource has defaults definitions for all the methods except keyResource Either one or other serializer must be defiened for default witeResource, readResource and delResource

Methods

keyResourceSource

Arguments

:: a 
-> String

must be defined

serializeSource

Arguments

:: a 
-> String

must be defined by the user

deserializeSource

Arguments

:: String 
-> a

must be defined by the user

tshowpSource

Arguments

:: a 
-> ST String

serializer in the RefSerialize monad. Either one or other serializer must be defined to use default persistence

treadpSource

Arguments

:: ST a

deserialize in the RefSerilzlize monad.

defPathSource

Arguments

:: a 
-> String

additional extension for default file paths or key prefixes

readResource :: a -> IO (Maybe a)Source

writeResource :: a -> IO ()Source

delResource :: a -> IO ()Source

data Resources a b Source

Resources returned by withSTMResources'

Constructors

Retry

forces a retry

Resources 

Fields

toAdd :: [a]

resources to be inserted back in the cache

toDelete :: [a]

resources to be deleted from the cache and from permanent storage

toReturn :: b

result to be returned

resources :: Resources a ()Source

resources= Resources  [] [] ()

setCache :: (Ht a, Integer) -> IO ()Source

set the cache. this is useful for hot loaded modules that will update an existing cache

defaultCheck :: Integer -> Integer -> Integer -> BoolSource

To drop from the cache all the elems not accesed since half the time between now and the last sync the default check procedure :: Integer -- current time in seconds -> Integer --last access time for a given object -> Integer --last cache syncronization (with the persisten storage) -> Bool --return true for all the elems not accesed since half the time between now and the last sync

data IDynamic Source

Constructors

forall a . (Typeable a, IResource a) => IDynamic a 

type Cache a = IORef (Ht a, Integer)Source

class DynamicInterface x whereSource

DynamicInterface groups a set of default method calls to handle dynamic objects. It is not necessary to derive instances from it

Methods

toIDynSource

Arguments

:: x 
-> IDynamic

encapsulates data in a dynamic object

registerTypeSource

Arguments

:: IO x

registers the deserialize, readp and readResource methods for this data type

fromIDynSource

Arguments

:: IDynamic 
-> x

extract the data from the dynamic object. trows a user error when the cast fails

unsafeFromIDynSource

Arguments

:: IDynamic 
-> x

unsafe version.

Instances

data Key Source

Key datatype can be used to read any object trough the Dynamic interface.

 data Key =  Key TypeRep String deriving Typeable

Example

  mst <- getDResource $ Key type keyofDesiredObject
             case mst of
               Nothing -> error $ "not found "++ key
               Just (idyn) ->  fromIDyn idyn :: DesiredDatatype}

Constructors

Key TypeRep String 

deleteDResource :: IDynamic -> IO ()Source

return error if any resource is not found

withResource :: (Typeable a, Typeable b, IResource a, IResource b) => a -> (Maybe a -> b) -> IO ()Source

withResources :: (Typeable a, Typeable b, IResource a, IResource b) => [a] -> ([Maybe a] -> [b]) -> IO ()Source

withSTMResourcesSource

Arguments

:: forall x a b . (Typeable a, Typeable b, IResource a, IResource b) 
=> [a]

the list of resources to be retrieved

-> ([Maybe a] -> Resources b x)

The function that process the resources found and return a Resources structure

-> STM x

The return value in the STM monad.