Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site













SourceForge.net Logo

LRUCache

 

template <class T, class K = String>

class LRUCache

This class simplifies implementation of LRU (least recently used) type of caches. Cache keeps the track of size of data contained and provides Shrink method, that reduces this size to requested number, removing least recently used items first. LRUCache also provides two size counters, GetFoundSize and GetNewSize, that can be used to further finetuning of cache.

GetFoundSize basically says how much data was reused since the last counter reset. GetNewSize says how much data had to be added since the last counter reset. GetSize() - GetFoundSize() - GetNewSize() is amount of data that has not been used since the last counter reset.

 

Maker subclass

 

struct Maker

This structure provides virtual base class for specific LRUCache to create requested data if not available in cache. Client code derives from Maker, adds required input parameters; Maker then provides key for the client parameter set and Make to create the data based on parameters.

 


 

virtual K Key() const = 0

Returns key based on parameters.

 


 

virtual int Make(T& objectconst = 0

Creates the data based on parameters, returns the size of created data.

 


 

~Maker()

Virtual destructor.

 

 

Public Method List

 

int GetSize() const

Returns the total size of data in cache (as sum of numbers returned by Maker::Make).

 


 

int GetCount() const

Returns the number of data items in cache.

 


 

template <class Pvoid AdjustSize(P getsize)

Recalculates the total size of data in cache, using getsize functional (which should have single const T& parameter) to retrieve the size of individual data items.

 


 

T& GetLRU()

Returns a reference to the least recently used data item in the cache (candidate for removal).

 


 

const K& GetLRUKey()

Returns the key of the least recently used data item in the cache (candidate for removal).

 


 

void DropLRU()

Removes the least recently used data item from the cache.

 


 

void Shrink(int maxsize, int maxcount = 30000)

Reduces the cache to contain no more than maxsize data and maxcount items removing the least recently used items first.

 


 

template <class Pint Remove(P predicate)

Removes data items from the cache for which predicate (which should have single const T& parameter) returns true. Returns the number of items removed.

 


 

template <class Pbool RemoveOne(P predicate)

Removes the first data item from the cache for which predicate (which should have single const T& parameter) returns true, scanning the cache from the most recently used items to least recently used. Returns true if item was found and removed.

 


 

T& Get(const Maker& m)

Retrieves data from the cache or creates them if needed m.

 


 

void Clear()

Clears all data from the cache.

 


 

void ClearCounters()

Clears the value of both size counters.

 


 

int GetFoundSize() const

Returns the size of data in the cache that has been reused since the last ClearCounters call (or constructor if there was none).

 


 

int GetNewSize() const

Returns the size of data that had to be created since the last ClearCounters call (or constructor if there was none).

 

 

Last edit by cxl on 10/31/2018. Do you want to contribute?. T++