Async LRU List : Cache « Development Class « Java






Async LRU List

    
// LRUList.java
// $Id: LRUList.java,v 1.7 2000/08/16 21:37:58 ylafon Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html
// AsyncLRUList.java
// $Id: AsyncLRUList.java,v 1.10 1998/01/22 14:24:58 bmahe Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html


// All locks allocated from left to right

public class AsyncLRUList extends LRUList {

    public final synchronized void toHead(LRUAble node) {
  _remove(node) ;
  if ( head.next != null ) {
      node.setNext(head.next) ;
      head.next.setPrev(node) ;
      node.setPrev(head) ;
      head.next = node ;
  } else {
      node.setNext(head.next) ;
      // head.next.setPrev(node) ;
      node.setPrev(head) ;
      head.next = node ;
  }
    }
      
    public final synchronized void toTail(LRUAble node) {
  _remove(node) ;
  if ( tail.prev != null ) {
      node.setPrev(tail.prev) ;
      tail.prev.setNext(node) ;
      node.setNext(tail) ;
      tail.prev = node ;
  } else {
      node.setPrev(tail.prev) ;
      // tail.prev.setNext(node) ;
      node.setNext(tail) ;
      tail.prev = node ;
  }
    }
      

    private final synchronized void _remove(LRUAble node) {
  LRUAble itsPrev = node.getPrev() ;
  if(itsPrev==null) 
      return ;
  LRUAble itsNext = node.getNext() ;
  node.setNext(null);
  node.setPrev(null);
  itsPrev.setNext(itsNext) ;
  if ( itsNext == null )
      return;
  itsNext.setPrev(itsPrev) ;
    }

    public final synchronized LRUAble remove(LRUAble node) {
  _remove(node) ;
  node.setNext((LRUAble) null) ;
  node.setPrev((LRUAble) null) ;
  return node ;
    }

    public final synchronized LRUAble getTail() {
  if ( tail.prev == null )
      return null;
  LRUAble prev = tail.prev ;
  return (prev == head) ? null : prev;
    }

    public final synchronized LRUAble getHead() {
  LRUAble next = head.next;
  return (next == tail) ? null : next;
    }

    public final synchronized LRUAble removeTail() {
  return (tail.prev != head) ? remove(tail.prev) : null;
    }

    public final synchronized LRUAble getNext(LRUAble node) {
  LRUAble next = node.getNext();
  return ((next == tail) || (next == head)) ? null : next;
    }

    public final synchronized LRUAble getPrev(LRUAble node) {
  LRUAble prev = node.getPrev();
  return ((prev == tail) || (prev == head)) ? null : prev;
    }

    
}

 abstract class LRUList {
    protected LRUNode head ;
    protected LRUNode tail ;

    public LRUList()
    {
  this.head = new LRUNode() ;
  this.tail = new LRUNode() ;
  head.prev = null ;
  head.next = tail ;
  tail.prev = head ;
  tail.next = null ;
    }

    /**
     * Moves node to front of list. It can be a new node, or it can be 
     * an existing node.
     * @param node the node
     */

    public abstract void toHead(LRUAble node) ;

    /**
     * Moves node to back of list. It can be a new node, or it can be
     * an existing node.
     * @param node the node
     */

    public abstract void toTail(LRUAble node) ;

    /**
     * Removes node if it's in list.
     * Does nothing if it's not.
     * When a node is removed, both its links are set to null.
     * @param node The node to remove
     * @return the same node
     */

    public abstract LRUAble remove(LRUAble node) ;

    /**
     * Obtain the backmost node.
     * @return the backmost node, or null if list is empty
     */

    public abstract LRUAble getTail() ;

    /**
     * Obtain the frontmost node.
     * @return the frontmost node, or null if list is empty
     */

    public abstract LRUAble getHead() ;

    /**
     * Obtain the backmost node, and remove it from list too.
     * @return the backmost node, or null if list is empty
     */

    public abstract LRUAble removeTail() ;

    /**
     * Get the next node of this list.
     * @return The next node, or <strong>null</strong> if this one was
     * last.
     */

    abstract public LRUAble getNext(LRUAble node) ;

    /**
     * Get the previous node of this list.
     * @return The previous node, or <strong>null</strong> if this one was
     * last.
     */

    abstract public LRUAble getPrev(LRUAble node) ;

}
//LRUNode.java
//$Id: LRUNode.java,v 1.4 2003/02/14 16:14:56 ylafon Exp $
//(c) COPYRIGHT MIT and INRIA, 1996.
//Please first read the full copyright statement in file COPYRIGHT.html

class LRUNode implements LRUAble {
  protected LRUAble prev ;
  protected LRUAble next ;

  public LRUNode() {
this.prev = null ;
this.next = null ;
  }

  public LRUNode(LRUAble prev,LRUAble next) {
this.prev = prev ;
this.next = next ;
  }

  public LRUAble getNext() {
return next ;
  }

  public LRUAble getPrev() {

return prev;
  }

  public void setPrev(LRUAble prev) {
this.prev = prev ;
  }

  public void setNext(LRUAble next) {
this.next = next ;
  }
}
//LRUAble.java
//$Id: LRUAble.java,v 1.3 1998/01/22 14:25:09 bmahe Exp $  
//(c) COPYRIGHT MIT and INRIA, 1997.
//Please first read the full copyright statement in file COPYRIGHT.html
interface LRUAble {
 public LRUAble getNext() ;
 public LRUAble getPrev() ;
 public void setNext(LRUAble next) ;
 public void setPrev(LRUAble prev) ;

}

   
    
    
    
  








Related examples in the same category

1.A LRU (Least Recently Used) cache replacement policy
2.A Map that is size-limited using an LRU algorithm
3.A random cache replacement policy
4.A second chance FIFO (First In First Out) cache replacement policy
5.An LRU (Least Recently Used) cache replacement policy
6.FIFO First In First Out cache replacement policy
7.Implementation of a Least Recently Used cache policy
8.Generic LRU Cache
9.LRU Cache
10.A Least Recently Used Cache
11.The class that implements a simple LRU cache
12.Map implementation for cache usage
13.Weak Cache Map
14.Provider for the application cache directories.
15.Fixed length cache with a LRU replacement policy.
16.A small LRU object cache.
17.A least recently used (LRU) cache.
18.LRU Cache 2
19.A cache that purges values according to their frequency and recency of use and other qualitative values.
20.A thread-safe cache that keeps its values as java.lang.ref.SoftReference so that the cache is, in effect, managed by the JVM and kept as small as is required
21.Cache LRU
22.A FastCache is a map implemented with soft references, optimistic copy-on-write updates, and approximate count-based pruning.
23.A HardFastCache is a map implemented with hard references, optimistic copy-on-write updates, and approximate count-based pruning.