A LRU Cache( Least Recently Used) is used to organize items according to their frequency of uses. It helps to identify Quickly Which item is used the least number of time in the Cache.
-
get(Key)
If the Key is There in cache , return corresponding value , either return -1 . -
put(key,value)
If the chache is not overflown insert the {key,value} pairs , either remove the LRU element pair from Cache and insert the new key-value pair.
- HashMap : Hash is used to refer as key -address of related node as their value.
- Queue : It is implemented as a Doubly Linked List. Queue Size== Cache Size .
The Least Recently Used element is placed towards the rear end and most used is towards to fron end .
- Let a Cache , size=2
- put(2,2)
- get(1) == 1
5 put(3,3)
-
get(1) == -1 **(Key=1 not present in cache)
-
put(4,4)
- Space Complexity -> O(n)
- Access Time -> O(1)