473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Design Patterns for LRU Cache in C++

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.

May 29 '07 #1
4 4963
On 29 Maj, 06:01, Raja <rokkamr...@gmail.comwrote:
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

May 29 '07 #2
On May 28, 9:01 pm, Raja <rokkamr...@gmail.comwrote:
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.

May 29 '07 #3
Naresh Rautela wrote:
On May 28, 9:01 pm, Raja <rokkamr...@gmail.comwrote:
>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.
How about using a set to store the structure of cache data and make it
sorted by timestamp. Put the least recently used entry at the head of
the set, then it can be deleted easily and quickly when necessary.
Whenever a cache entry is accessed, change its timestamp and remove its
original copy from the set and then put the new copy into the set. Since
the set is sorted by timestamp, the new copy will be put at the
appropriate position automatically.

Just my 2 cents.
May 29 '07 #4
Erik Wikström wrote:
On 29 Maj, 06:01, Raja <rokkamr...@gmail.comwrote:
>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 .
You might want to look at std::priority_queue, sorted by time of use.
May 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Leon Mergen | last post by:
Hello, I'm currently in the process of implementing a file cache, and am looking for any existing design patterns. I hadn't had much luck with google, is anyone aware of a good file cache...
5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
1
by: Jay | last post by:
The GOF text is widely considered the definitive book on the topic. Design Patterns: Elements of Reusable Object-Oriented Softare, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides ...
1
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The...
13
by: John Salerno | last post by:
Here are a few I'm considering: Design Patterns Explained : A New Perspective on Object-Oriented Design (2nd Edition) (Software Patterns Series) by Alan Shalloway Design Patterns C# by...
2
by: Carlo Stonebanks | last post by:
I have the infamous GoF Design Patterns boo - it's been sittin gon my shelf for years. I have a huge reading list and find this book a rather dry read and am always putting it off. I have...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
7
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I would like to know more about design patterns and specifically using C#. Can any one recommend a good book? Thanks
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.