• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Generic LRU (Least Recently Used) cache implementation in java.

0 votes
19 views
Generic LRU (Least Recently Used) cache implementation in java. How to implement LRU - Least Recently Used cache in java with running code using generic implementation.
asked Apr 16 in Data Structure And Algorithm by Aalok kumar

1 Answer

0 votes
Generic LRU cache:- The generic implementation for LRU cache. That means accept any type of data in cache for example you can provide integer, string, float etc.

See the following running example of generic LRU cache:-
package com.ds.queue;

/**
 * LRUCache class provides the facility to insert data into cache and get data from cache if available.
 * Doubly linked list data structure used in queue format
 *
 */
public class GenericLRUCache<T> {
   
    final static int CATACITY = 3;
    int currentCapacity = 0;
   
    Node<T> front, rear;
   
    /**
     * Insert data into cache
     *
     * @param data
     */
    public void put(T data){
        //If data is available in cache
        if(isAvailableInCache(data)){
            //Then update data position (Always in front)
            updatePosition(data);
        }else{//If data is not available in cache
            //If cache is full
            if(isFullCache()){
                //Then remove data from queue in rear
                removeFromRear();
            }
            insert(data);
        }
    }
   
    /**
     * if given data is available in cache then return cache data and update its position (Comes front)
     *
     * @param data
     * @return
     */
    public T get(T data){
        if(isAvailableInCache(data)){
            System.out.println("Availabe in cahche:"+data);
        }
        return data;
    }
   
    /**
     * Insert data into queue
     * @param data
     */
    public void insert(T data){
        Node<T> newNode = new Node<>(data);
        currentCapacity++;
        if(front == null){
            front =  rear = newNode;
            return;
        }
        newNode.next = front;
        front.prev = newNode;
        front = newNode;
    }
   
    /**
     * Update data to front position. Recently used data always comes front
     *
     * @param data
     */
    public void updatePosition(T data){
        if(rear==null)
            return;
        Node<T> current = front;
        while(current != null){
            if(current.data == data){
                Node<T> temp = current;
                current.prev.next = current.next;
                current.next.prev = current.prev;
                temp.next = front;
                temp.prev = null;
                front.prev = temp;
                front = temp;
                return;
            }
            current = current.next;
        }
    }
   
    /**
     * If data is available in cache then return true otherwise false
     *
     * @param data
     * @return
     */
    private boolean isAvailableInCache(T data){
        Node<T> current = front;
        while(current != null){
            if(current.data == data){
                return true;
            }
            current = current.next;
        }
        return false;
    }
   
    /**
     * If cache is full then return true otherwise false
     *
     * @return
     */
    private boolean isFullCache(){
        return CATACITY == currentCapacity;
    }
   
    /**
     * Remove from rear
     *
     */
    public void removeFromRear(){
        Node<T> prev_of_rear = rear.prev;
        rear.prev = null;
        prev_of_rear.next = null;
        rear = prev_of_rear;
        currentCapacity--;
    }
   
    public static void main(String[] args) {
        GenericLRUCache<Integer> cache = new GenericLRUCache<>();
        cache.put(5);
        cache.put(10);
        cache.put(15);
        cache.put(20);
        cache.put(15);
        cache.get(5);
       
        GenericLRUCache<String> strCache = new GenericLRUCache<>();
        strCache.put("Ram");
        strCache.put("Kam");
        strCache.put("Dam");
        strCache.put("Sam");
        strCache.put("Dam");
        strCache.get("Kam");
    }
   
    class Node<T>{
        T data;
        Node<T> next;
        Node<T> prev;
       
        public Node(T data){
            this.data = data;
        }
    }

}

Output:-
Availabe in cahche:Kam
answered Apr 16 by ranju_12 (2,060 points)
...