#include <FXLRUCache.h>
To maximise performance in modern systems which tend to have plenty of free RAM, caching can be used to speed up cases where possibly the same processing may need to be repeated. When I wrote this class, I had the Tornado kernel namespace nodes in mind, but it's generically applicable to any similar situation.
Some effort is made to replicate the Qt API for QCache, sufficiently that with suitable typedef
-ing of the template parameters you can make a compatible QCache or QIntCache. However anything that FX::QDictBase can be configured with can also make a FXLRUCache.
One interesting feature of FXLRUCache is that it can handle value objects. This was done primarily with FX::FXRefingObject in mind but its use exists outside of that. To enable FXLRUCache as a value-based container, use the following form:
typedef FXLRUCache<QDict<Foo>, Foo> FooValueCache;
If dynamic mode is enabled, maxCost() is shifted right by FXProcess::memoryFull(). By default dynamic mode is enabled.
Public Types | |
typedef dictbase::KeyType | KeyType |
typedef dictbase::ItemType | FundamentalDictType |
Public Member Functions | |
FXLRUCache (FXuint maxCost=100, FXuint size=13, bool autodel=false) | |
void | setAutoDelete (bool a) |
bool | dynamic () const throw () |
void | setDynamic (bool v) const throw () |
FXuint | maxCost () const throw () |
void | setMaxCost (FXuint newmax) |
FXuint | totalCost () const throw () |
void | cacheStats (float *hitrate, float *churn) const throw () |
void | statistics () const |
bool | insert (KeyTypeAsParam k, TypeAsParam d, FXint itemcost=1) |
bool | remove (KeyTypeAsParam k) |
TypeAsReturn | take (KeyTypeAsParam k) |
TypeAsReturn | find (KeyTypeAsParam k, bool tofront=true) const |
TypeAsReturn | operator[] (KeyTypeAsParam k) const |
Protected Types | |
typedef FundamentalDictType * | DictType |
typedef Generic::select < Generic::sameType< type, Generic::NullType >::value, DictType, type >::value | Type |
typedef Generic::TraitsBasic < Type > | TypeTraits |
typedef Generic::TraitsBasic < KeyType >::asROParam | KeyTypeAsParam |
typedef TypeTraits::asROParam | TypeAsParam |
typedef Generic::leastIndir < Type >::value * | TypeAsReturn |
typedef FXLRUCacheImpl::getPtr < Type, TypeAsParam, TypeTraits::isValue > | CacheItemBase |
Protected Member Functions | |
virtual void | purgeLFU () |
virtual void | deleteItem (DictType d) |
Protected Attributes | |
FXuint | topmax |
FXuint | maximum |
FXuint | cost |
bool | amDynamic |
std::list< CacheItem > | cache |
struct FX::FXLRUCache::stats_t | stats |
Friends | |
class | FXLRUCacheIterator |
FX::FXLRUCache< dictbase, type >::FXLRUCache | ( | FXuint | maxCost = 100 , |
|
FXuint | size = 13 , |
|||
bool | autodel = false | |||
) | [inline] |
Constructs an instance with maximum cost maxCost and dictionary size size.
virtual void FX::FXLRUCache< dictbase, type >::purgeLFU | ( | ) | [inline, protected, virtual] |
Called when totalCost() exceeds maxCost() to purge the least recently used items. The default implementation works on the basis of the least recently looked up via find(), however this is virtual so you can override it with a more intelligent mechanism
bool FX::FXLRUCache< dictbase, type >::dynamic | ( | ) | const throw () [inline] |
Returns if this cache adjusts itself dynamically to memory full.
void FX::FXLRUCache< dictbase, type >::setDynamic | ( | bool | v | ) | const throw () [inline] |
Sets if this cache adjusts itself dynamically to memory full.
FXuint FX::FXLRUCache< dictbase, type >::maxCost | ( | ) | const throw () [inline] |
Returns the maximum cost permitted by the cache.
void FX::FXLRUCache< dictbase, type >::setMaxCost | ( | FXuint | newmax | ) | [inline] |
Sets the maximum cost permitted. Disposes of items immediately if the new cost warrants it.
FXuint FX::FXLRUCache< dictbase, type >::totalCost | ( | ) | const throw () [inline] |
Returns the total cost of the cache's current contents.
void FX::FXLRUCache< dictbase, type >::cacheStats | ( | float * | hitrate, | |
float * | churn | |||
) | const throw () [inline] |
Returns operating statistics about the cache. hitrate is a percentage of hits versus total lookups and churn is a percentage of items flushed versus total lookups.
Referenced by FX::FXLRUCache< FX::QDict< type > >::statistics().
void FX::FXLRUCache< dictbase, type >::statistics | ( | ) | const [inline] |
Prints some statistics about the hash table (only debug builds).
bool FX::FXLRUCache< dictbase, type >::insert | ( | KeyTypeAsParam | k, | |
TypeAsParam | d, | |||
FXint | itemcost = 1 | |||
) | [inline] |
bool FX::FXLRUCache< dictbase, type >::remove | ( | KeyTypeAsParam | k | ) | [inline] |
Removes the item associated with key k, deleting if auto-deletion is enabled.
TypeAsReturn FX::FXLRUCache< dictbase, type >::take | ( | KeyTypeAsParam | k | ) | [inline] |
Removes the item associated with key k, never deleting except when a value-based container (in which case, always returns zero)
TypeAsReturn FX::FXLRUCache< dictbase, type >::find | ( | KeyTypeAsParam | k, | |
bool | tofront = true | |||
) | const [inline] |
Finds the item associated with key k, returning zero if not found. Marks the item as most recently used if tofront is true.