All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.anoprise.cache.RequestBasedCache Maven / Gradle / Ivy

Go to download

Collection of utils for different enterprise class projects. Among other stuff contains Caches, Mocking, DualCrud, MetaFactory and SessionDistributorService. Visit https://opensource.anotheria.net for details.

There is a newer version: 4.0.0
Show newest version
package net.anotheria.anoprise.cache;

import net.anotheria.moskito.core.predefined.CacheStats;

import java.util.HashMap;

public class RequestBasedCache extends AbstractCache implements Cache{
	
	/**
	 * A copy of the CacheStats for optimized access.
	 */
	private CacheStats cacheStatsCopy = null;

	public RequestBasedCache(String name){
		super(name);
		
		cacheStatsCopy = getCacheStats();
	}
	
	public RequestBasedCache(){
		this(getUnnamedInstanceName(RequestBasedCache.class));
	}
	
	
	@Override
	public V get(K id) {
		cacheStatsCopy.addRequest();
		V ret = (V)mapHolder.get().get(id);
		if (ret!=null)
			cacheStatsCopy.addHit();
		return ret;
	}

	@Override
	public void put(K id, V cacheable) {
		cacheStatsCopy.addWrite();
		mapHolder.get().put(id, cacheable);
		
	}

	@Override
	public void remove(K id) {
		cacheStatsCopy.addDelete();
		mapHolder.get().remove(id);
		
	}

	@Override
	public void clear() {
		mapHolder.get().clear();
	}

	@Override public String toString(){
		return super.toString();
	}
	
	
	/**
	 * The thread local variable associated with the current thread.
	 */
	private static InheritableThreadLocal mapHolder = new InheritableThreadLocal() {
		@Override
		protected synchronized HashMap initialValue() {
			return new HashMap();
		}

		@Override
		protected HashMap childValue(HashMap parentValue) {
			HashMap ret = new HashMap();
			ret.putAll(parentValue);
			return ret;
		}
	};
	
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy