In my approach, he needed to have a hash table to quickly search for stored objects and a linked list to save the last used sequence.
When an object is requested. 1) try to find the object from the 2.yes hash table), if found (the value has a pointer to an object in the linked list), move the object in the linked list at the top of the linked list. 2.no) if not, remove the last object from the linked list and delete the data from the hash table, and then put the object in the hash table and the top of the linked list.
For example, let's say we have a cache for only 3 objects.
Sequence of requests 1 3 2 1 4.
1) Hash table: [1] Linked list: [1]
2) Hash table: [1, 3] Linked list: [3, 1]
3) Hash table: [1,2,3] Linked list: [2,3,1]
4) Hash table: [1,2,3] Linked list: [1,2,3]
5) Hash table: [1,2,4] Linked list: [4,1,2] => 3 out
Seungyoung 09 Oct '10 at 9:32 2010-10-09 09:32
source share