tags:

views:

656

answers:

2

Hi,

Does anyone know any implementation of a templated cache of objects?

  • You use a key to find object (the same as in std::map<>)
  • You specify a maximum number of objects that can be in the cache at the same time
  • There are facilities to create an object not found in the cache
  • There are facilities to know when an object is discarded from the cache

For example :

typedef cache<int, MyObj*> MyCache;
MyCache oCache;
oCache.SetSize(1);
oCache.Insert(make_pair(1, new MyObj());
oCache.Touch(1);
MyObj* oldObj = oCache.Delete(1);

...

It can be as simple as a LRU or MRU cache.

Any suggestions are welcomed!

Nic

A: 

In an application I can hardly imagine it would speed/boost up performance to store objects that apparently can be re-created (hip: since they can be automatically discarded, when the cache tops). A sw cache would require memory-fetching through associativism code, surely slower then memory allocation and constructor running (mostly memory initializations).

With the exception of manual user configuration to avoid the paging mechanism (precisely to boost performance, btw), most OS "caches" memory for you on disk... it's "paging", a form of "high-cost caching", because nothing gets thrown away, and it's done by specific HW, a sub-processing unit called Memory Management Unit...

A caching-code, in the big picture, would be slowing processes while being redundant.

jpinto3912
What if the (re)creation of an object is much slower than a key->value lookup? Not every constructor is "mostly memory initializations."
mos
I get why the downvote: I'm not providing an answer.So I'm trying to get one:Nowdays the MMU will flag the memory containing the non-recently used cached objects as low-usage, hence candidate to be sent to a page-file on the harddisk... assuming there's a HDD. So, re-fetching a missing cached-object from HDD, iso running code to re-create the object, would only in a very cumbersome set of circumstances be "right".@Nicolas: what are your concrete circumstances?
jpinto3912
+1  A: 

It seems that this would serve your purpose:

http://blackcat.ca/code/lrucache

Hint: it's the 4th result from Google("template cache c++").

Note: licence is GPL.

rlerallut