Design Patterns for LRU Cache in C++
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
Design Patterns for LRU Cache in C++         


Author: Raja
Date: May 28, 2007 21:01

Hi,
I am trying to do a simple design of an LRU Cache . Can anyone
please guide me in this regard. I have a basic idea ADT to be used
using linked list and reference counted array. But I want to know the
best method to design , the classes , interfaces etc required .

Thank You,
Raja.
2 Comments
Re: Design Patterns for LRU Cache in C++         


Author: Erik Wikström
Date: May 28, 2007 23:07

On 29 Maj, 06:01, Raja gmail.com> wrote:
> Hi,
> I am trying to do a simple design of an LRU Cache . Can anyone
> please guide me in this regard. I have a basic idea ADT to be used
> using linked list and reference counted array. But I want to know the
> best method to design , the classes , interfaces etc required .

Depends on your needs and constraints. A simple version would be to
use a vector to store a struct describing whatever it is that you
cache, and each time you access one of them you update a time-stamp.
When you need to replace one of them you can either sort the vector on
the time-stamp, or, if you need the order unchanged, make a copy and
sort the copy.

For more efficient implementations you might want to get a book on
operating systems, or google, or check wikipedia.

--
Erik Wikström
no comments
Re: Design Patterns for LRU Cache in C++         


Author: Naresh Rautela
Date: May 29, 2007 05:20

On May 28, 9:01 pm, Raja gmail.com> wrote:
> Hi,
> I am trying to do a simple design of an LRU Cache . Can anyone
> please guide me in this regard. I have a basic idea ADT to be used
> using linked list and reference counted array. But I want to know the
> best method to design , the classes , interfaces etc required .
>
> Thank You,
> Raja.

Another simple implentation would be to use a list. Each time you get
a cached element append it to the end of the list. The head of the
list indicates the least recently used item. When a cached element is
used again move it to the end of the list. When a new element needs
to be cached delete the one at the front of the list and append the
new one at the end of the list.

Hope this helps.
no comments