| Intro to Caching,Caching algorithms and caching frameworks part 3 |
|
| Sunday, 09 August 2009 20:53 |
|
Introduction:
Meet LRU Cache Implementation: private void moveToFront(int index) { Analyzing LRU Cache Code (Talk show): After LFU finished talking, there were not much screaming, they didn’t like the presentation and LFU was hesitating while talking, this gave a big push to LRU which started by saying: This time I will consider the case also when the cache is not full, I am little more complex than those other algorithms, when the cache isn’t full and it is the first entry I will just increment the numEntries which represents the number of entries in the cache. After adding a second entry I will need to move it to the front by calling moveToFront method (we will talk about it soon), I didn’t do this for the first entry because it is for sure the first element. So let’s see some action. As you can see I am stating that the previous of the current entry will have the tail value and the next entry will be -1 (undefined in other words) these are just initial data. After adding the new entry (which isn’t the first entry) I will move it to front. if(!isFull()) { The moveToFront method moves an entry to the head of the array so that the least recently used elements reside at the bottom of the array. Before I do any move I check if the head is not equal to current index (this will be false in case we only have 1 entry) if yes, then assign the value of the next of the current entry (which is a pointer to next entry as in linked list) to nextIndex and the value of the previous of the current entry (which is a pointer to the previous entry as in linked list) to prevIndex Then I assign the value of the nextIndex to the value of next of the previous entry // Only the head has a prev entry that is an invalid index so After that I am going to check for the nextIndex if it is greater that or equal 0 then the previous the next entry will have the value of prevIndex , else the tail will be equal to the prevIndex And because I moved this entry to the front so there won’t be any previous entry for it so am assigning -1 to it and the next entry of the current entry (top one) will be the head (previous old head) and the prev of head (the old head) will have the index of the current entry and then the new head is assigned the new index (current index) prev[index] = -1; It is magnifying time! Get your magnifying glass we are going to see some interesting stuff here
http://2.bp.blogspot.com/_WgM4xO79GUI/SZcxFHZATKI/AAAAAAAAAC8/NV3EJSHwIH8/s1600-h/LRUCache.jpg (more clear image) It is Confession Time! : LRU didn’t mention that it is possible to implement the LRU algorithm in a simple way , our previous implementation is based on Arrays , the other implementation that LRU cache didn’t mention is through LinkedHashMap which was introduced in JDK 1.4 public class LRUCache2 extends LinkedHashMap For sure, the LinkedHashMap solution is less time consuming that the array solution and it is more efficient coz you will leave the handling of the deletion and so on to the JDK itself, so you won’t bother yourself implementing such stuff. Conclusion: We have seen how to implement LFU and LRU algorithms and the two ways to implement the LRU, it is based on you to choose which way to use, Arrays or LinkedHashMap for me I would recommend Arrays for small size entries and LinkedHashMap for big size entries. In next part we will be talking about the Caching framework and a comparison between them and what caching algorithm is employed by which caching framework, stay tuned till then ;) |
| Last Updated on Monday, 10 August 2009 02:27 |
Home
Community
