How to design a latest recently used cache?

How to design a latest recently used cache?

Suppose that you have visited some items. You need to design a data structure to hold these items. Each item is associated with the latest visited time.

Each time when you visit an item, check it in the data structure. If the item has been in the cache, update its visit time. Otherwise, insert it into the cache. The cache size is fixed, if it is full, delete the oldest item.

My solution:

  1. Use a map < item, visitTime >

  2. Initaliztion: Sort the map with f(visitTime) with descending order. O(nlg n)

  3. If an item is visited, search it in the map with O(lg n).

  4. If it has been in the map, update the time O(1). Sort the map O(lg n).

  5. If not, insert it into map and then sort. O(lg n)

  6. If map size > fixed size, delet the last element O(1).

Another solution:

  1. Use hashtable < item , visitTime >

  2. Sort it O(n lgn).

  3. If an item is visited, search it in the talbe with O(1).

  4. If it has been in the table , update the time O(1). Sort the table O(n lg n).

  5. If not, insert it into table and then sort. O(n lg n)

  6. If table size > fixed size, delet the last element O(1).

Are there better solutions ? O(n) ?

Answers


If you use a Doubly Linked List, you'll get O(1) insertion (after search), O(1) deletion, O(n) search.

Assuming you insert new items in the front:

If the cache is not full, just add to the front (O(1)).

If you need to update an item, find it (O(n)), remove it from the linked list (O(1)), then add to the front (O(1)).

If you need to delete the oldest to insert a new item, delete the end element (O(1)), and insert to the front (O(1)) [note: you need to search the list first in this case to see if the item is not already in the cache, so O(n)].

A Linked List can also give you the same time, since the search will leave you at the last element.


Need Your Help

SQL Server Insert query for a forum

sql sql-server concurrency transaction-isolation

Considering a forum table and many users simultaneously inserting messages into it, how safe is this transaction?

finding inflection points in spline fitted 1d data

python numpy scipy spline

I have some one dimensional data and fit it with a spline. Then I want to find the inflection points (ignoring saddle points) in it. Now I am searching the extrema of its first derivation by using

How to plot a surface with a texture map

matlab image-processing texture-mapping geometry-surface

I want to plot a surface with a texture map on it, but the conditions are not the "ideal" ones.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.