Workspace 6.21.5
Public Member Functions | List of all members
DiskCache< T, LockingPolicy, Traits > Class Template Reference

#include <Workspace/DataExecution/DataObjects/diskcache.h>

Inheritance diagram for DiskCache< T, LockingPolicy, Traits >:
[legend]

Public Member Functions

 DiskCache (qint64 maxSize, bool deferredCaching=true)
 
virtual ~DiskCache ()
 
T * acquire (const QString &key, bool forceAcquire=false)
 
bool add (const QString &key, T &data, qint64 size=sizeof(T), bool destroyExistingData=false)
 
int getCountOfItems () override
 
int getCountOfItemsInMemory () override
 
qint64 getCurrentSize () override
 
bool isInCache (const QString &key)
 
bool isInMemory (const QString &key)
 
void release (const QString &key)
 
bool remove (const QString &key, bool destroyData=true, bool forceRemove=false)
 
bool removeAll (bool destroyData=true, bool forceRemove=false)
 
void setMaxSize (qint64)
 
void setMode (bool deferCaching)
 
virtual int getCountOfItems ()=0
 
virtual int getCountOfItemsInMemory ()=0
 
virtual qint64 getCurrentSize ()=0
 

Constructor & Destructor Documentation

◆ DiskCache()

DiskCache ( qint64  maxSize,
bool  deferredCaching = true 
)

◆ ~DiskCache()

~DiskCache
virtual

Member Function Documentation

◆ acquire()

T * acquire ( const QString &  key,
bool  forceAcquire = false 
)

Retrieves an item from the DiskCache. This marks the retrieved item as being in-use. Once the retrieved item is no longer needed call release() to make it a candidate for moving it from main-memory to disk. NB. When acquiring an item it may be that the cache's memory is full and so can't load the data associated with the provided key into memory. The default behaviour in this scenario is to return 0. This default behaviour can be overridden by setting the forceAcquire parameter to true. Under this scenario the cache's size will be forcibly increased to accommodate the new item however a diagnostic will be logged informing of this scenario.

Parameters
keyThe key or handle to use
forceAcquireIf set to true the cache will be forcibly resized and the data will be guaranteed to be returned
See also
DiskCache::release

◆ add()

bool add ( const QString &  key,
T &  data,
qint64  size = sizeof(T),
bool  destroyExistingData = false 
)

Adds data to the cache indexed against the provided key returning true if successfully added and false if not. Even if the provided data is > the cache size the data will still be added to the cache however it will be immeadiately written to disk. As such, you MUST NOT attempt to use the added data directly after calling this method but must instead re-acquire a pointer to your data via the cache's acquire() method. If you call this method more than once with the same key then it is YOUR responsibility to inform the cache if you want to destroy the data currently held for the existing key by setting the destroyExistingData parameter to true (the default is false as it is assumed that operations on the same key will be to resize existing data). If this is not the case, in other words, if the same key is to be-reused for entirely different data then you should set the destroyExistingData parameter to true to destroy the data held against this existing key.

Typical usage is as follows:

DiskCache<Foo> myCache; Foo* myObject = new Foo(); myCache.add("MyKey", myObject); Foo safeObject = myCache.acquire("MyKey");

...use your object then...

To permanently remove your item from the cache:

 myCache.remove("MyKey");

OR

To mark it as no longer being in use (but not remove it as it may be req'd later on):

 myCache.release("MyKey");
Parameters
keyThe key or handle used
dataThe data to store against the private key
sizeThe size of the provided data
destroyExistingDataDestroy the data currently held for the existing key
See also
acquire()

◆ getCountOfItems()

int getCountOfItems ( )
overridevirtual

Returns the number of items currently in the cache (both in memory and on disk)

Implements AbstractCache.

◆ getCountOfItemsInMemory()

int getCountOfItemsInMemory ( )
overridevirtual

Returns the number of items currently in memory

Implements AbstractCache.

◆ getCurrentSize()

qint64 getCurrentSize ( )
overridevirtual

Returns the current size of a cache

Implements AbstractCache.

◆ isInCache()

bool isInCache ( const QString &  key)
Returns
True to indicate that the requested item is stored in the cache, either in memory or on disk. This can be useful if client code needs to know whether something needs to be loaded and added to the cache, without necessarily retrieving the data straight away (if it already exists in the cache).

◆ isInMemory()

bool isInMemory ( const QString &  key)

Returns true to indicate if the requested item is currently in memory. This can be useful if client code decides that the item is quicker to regenerate than fetch from disk.

◆ release()

void release ( const QString &  key)

Marks an item as no longer being in-use and so is a candidate for moving from main-memory to disk.

See also
release

◆ remove()

bool remove ( const QString &  key,
bool  destroyData = true,
bool  forceRemove = false 
)

Attempts to remove an item from the cache. This method will decrement the usage count of an item in the cache prior to attempting to remove for convenience (i.e. to save client code from having to call release prior to remove). If the item being removed is still in use after performing a release then the item will not be removed unless the forceRemove parameter is set to true. If forceRemove is set to true then the client code is saying that despite the item being marked as being in use the client code knows it is safe to focribly remove (and potentially destroy) the in use data.

See also
add, release

◆ removeAll()

bool removeAll ( bool  destroyData = true,
bool  forceRemove = false 
)

◆ setMaxSize()

void setMaxSize ( qint64  maxSize)

◆ setMode()

void setMode ( bool  deferCaching)