<p>Does anyone know any implementation of a templated cache of objects?</p>

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

<p>For example : </p>

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

...
</code></pre>

<p>It can be as simple as a LRU or MRU cache.</p>

<p>Any suggestions are welcomed!</p>

<p>Nic</p>

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.