Click here to Skip to main content
15,890,845 members
Articles / Programming Languages / C++

Memory cache for collections of objects

Rate me:
Please Sign up or sign in to vote.
3.12/5 (7 votes)
8 Sep 2006CPOL2 min read 33K   363   13   2
A generic template class to implement a memory cache for collections of objects.

Introduction

This template class implements a cache for memory objects. You can use it when you have a large collection of objects that you do not want to put entirely in memory, but you need to use some of them with quick access anyway. It can also be seen as a kind of STL map with its size controlled.

Typical use

The example below shows a cache of strings indexed by an int:

C++
cache<int,string> mycache;
int key=95623;
string *pText = mycache[key];  // find element 95623
if(!mycache.iscached(pText))
{
   FillTextFromDisk(key,pText); // fills with a pText= new string...
   mycache.push_back(key,pText);
}
// ... here *pText is available, and very quickly if it was cached

Integration

Just include cache.h in your code, copy paste the example above, and modify it following your needs as described below.

Description

C++
cache<K, V>;

Constructor of the cache: K is the key, and V is the object stored. See also "Size of the cache". K is the name of the key class; it may be a string, an int, or any object that can be a key for an STL map. V is the name of class that you want to cache. V must support a size() method and a correct ~V destructor, which are used by the cache.

C++
push_back(K k,V* p);

Insert the p element in the cache at position k. p must point to a V element or be NULL. This element has been created with new V, and will be deleted by the cache soon or later; this element may be NULL, on the condition that NULL->size() gives back 0 and delete NULL works.

C++
V* operator[](K& k);

This allows to access an object indexed by k, and gives back a pointer on this object. If the object is not cached, (*V)(-1) is returned. For example: pText = mycache[key];.

iscached(V* p);

Simply gives back true when p is not -1.

Size of the cache

You can control the number of elements of the queue and the total size of the queue. A more complete declaration of the cache is:

C++
cache<K, V, limsize>;  // example cache<int,string,10000> mycache;

When the cache grows till limsize, the older elements are removed. limsize represents the maximum size of the cache expressed in the unit of V::size() (for example: characters for string::size()).

A more complete declaration of the cache is:

C++
cache<K, V, limsize, BigQ, SmallQ>;

It allows to control the size of the queue. The size of the cache had been defined above. The size of the queue is the maximum number of V elements allowed in the queue. When the size of the cache is greater than limsize, SmallQ is used, else BigQ is used.

Hints

  • Do not forget that cache will delete V elements.
  • All elements of the cache will be deleted in the destructor of the cache: do not keep it more than necessary.
  • When 'maximum' is used in this article, it may be a bit more...
  • It is a choice to allow or not a NULL pointer, but respect the conditions on V.
C++
V::size() { if(!this) return 0; else...

Inside

This code is based on the STL hash_map and list. It is not very complex once you have entered STL.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks! Pin
Member 9427197-Mar-09 3:32
Member 9427197-Mar-09 3:32 
Generalvirus found in the url provide by "Member 942719" Pin
zxq20-Mar-09 3:10
zxq20-Mar-09 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.