Weak Cache Map : Cache « Development Class « Java






Weak Cache Map

 
/*
 * This file is part of aion-emu <aion-emu.com>.
 *
 *  aion-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.aionemu.gameserver.utils.collections.cachemap;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;



/**
 * This class is a simple map implementation for cache usage.<br>
 * <br>
 * Values from the map will be removed after the first garbage collector run if there isn't any strong reference to the
 * value object.
 * 
 * @author Luno
 * 
 */
class WeakCacheMap<K, V> extends AbstractCacheMap<K, V> implements CacheMap<K, V>
{
  /**
   * This class is a {@link WeakReference} with additional responsibility of holding key object
   * 
   * @author Luno
   */
  private class Entry extends WeakReference<V>
  {
    private K  key;

    Entry(K key, V referent, ReferenceQueue<? super V> q)
    {
      super(referent, q);
      this.key = key;
    }

    K getKey()
    {
      return key;
    }
  }

  WeakCacheMap(String cacheName, String valueName)
  {
    super(cacheName, valueName);
  }

  @SuppressWarnings("unchecked")
  @Override
  protected synchronized void cleanQueue()
  {
    Entry en = null;
    while ((en = (Entry) refQueue.poll()) != null)
    {
      K key = en.getKey();
      cacheMap.remove(key);
    }
  }

  @Override
  protected Reference<V> newReference(K key, V value, ReferenceQueue<V> vReferenceQueue)
  {
    return new Entry(key, value, vReferenceQueue);
  }
}

/**
 * Base class for {@link WeakCacheMap} and {@link SoftCacheMap}
 * 
 * @author Luno
 * 
 * @param <K>
 * @param <V>
 */
abstract class AbstractCacheMap<K, V> implements CacheMap<K, V>
{
  protected final String          cacheName;
  protected final String          valueName;

  /** Map storing references to cached objects */
  protected final Map<K, Reference<V>>  cacheMap  = new HashMap<K, Reference<V>>();

  protected final ReferenceQueue<V>    refQueue  = new ReferenceQueue<V>();

  /**
   * @param cacheName
   * @param valueName
   */
  AbstractCacheMap(String cacheName, String valueName)
  {
    this.cacheName = "#CACHE  [" + cacheName + "]#  ";
    this.valueName = valueName;
  }

  /** {@inheritDoc} */
  @Override
  public void put(K key, V value)
  {
    cleanQueue();

    if (cacheMap.containsKey(key))
      throw new IllegalArgumentException("Key: " + key + " already exists in map");

    Reference<V> entry = newReference(key, value, refQueue);

    cacheMap.put(key, entry);
  }

  /** {@inheritDoc} */
  @Override
  public V get(K key)
  {
    cleanQueue();

    Reference<V> reference = cacheMap.get(key);

    if (reference == null)
      return null;

    V res = reference.get();
    return res;
  }

  @Override
  public boolean contains(K key)
  {
    cleanQueue();
    return cacheMap.containsKey(key);
  }

  protected abstract void cleanQueue();

  @Override
  public void remove(K key)
  {
    cacheMap.remove(key);
  }

  protected abstract Reference<V> newReference(K key, V value, ReferenceQueue<V> queue);
}

interface CacheMap<K, V>
{

  /**
   * Adds a pair <key,value> to cache map.<br>
   * <br>
   * 
   * <font color='red'><b>NOTICE:</b> </font> if there is already a value with given id in the map,
   * {@link IllegalArgumentException} will be thrown.
   * 
   * @param key
   * @param value
   */
  public void put(K key, V value);

  /**
   * Returns cached value correlated to given key.
   * 
   * @param key
   * @return V
   */
  public V get(K key);

  /**
   * Checks whether this map contains a value related to given key.
   * @param key
   * @return true or false
   */
  public boolean contains(K key);

  /**
   * Removes an entry from the map, that has given key.
   * @param key
   */
  public void remove(K key);
}

   
  








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.Async LRU List
7.FIFO First In First Out cache replacement policy
8.Implementation of a Least Recently Used cache policy
9.Generic LRU Cache
10.LRU Cache
11.A Least Recently Used Cache
12.The class that implements a simple LRU cache
13.Map implementation for cache usage
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.