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

net.anotheria.anoprise.cache.FailoverCache 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;

/**
 * A cache implementation where elements stored based on the instance of cache
 * If its called by its instance (so current call is not fail over call) - element will be stored, other wise - not stored.
 *
 * @param  key value
 * @param  stored element
 * @author ivanbatura
 * @since: 23.06.12
 */
public class FailoverCache implements Cache {
	/**
	 * Internal cache delegate.
	 */
	private Cache cache;
	/**
	 * Total number of cache instance to failover support
	 */
	private int instanceAmount;
	/**
	 * Current number of cache instance to failover support
	 */
	private int currentInstanceNumber;
	/**
	 * modable value calculator
	 */
	private ModableTypeHandler modableTypeHandler;
	/**
	 * Moskito cache stats of the internal cache delegate. Both caches work on the same cache stats object ( but different values of it).
	 */
	private CacheStats moskitoCacheStats;

	public FailoverCache(String name, int aStartSize, int aMaxSize, int aInstanceAmount, int aCurrentInstanceNumber, ModableTypeHandler aModableTypeHandler, CacheFactory underlyingCacheFactory) {
		this(name, aInstanceAmount, aCurrentInstanceNumber, aModableTypeHandler, underlyingCacheFactory.create(name, aStartSize, aMaxSize));
	}

	public FailoverCache(String name, int aInstanceAmount, int aCurrentInstanceNumber, ModableTypeHandler aModableTypeHandler, Cache underlyingCache) {
		cache = underlyingCache;
		instanceAmount = aInstanceAmount > 0 ? aInstanceAmount : 1;
		currentInstanceNumber = aCurrentInstanceNumber;
		modableTypeHandler = aModableTypeHandler != null ? aModableTypeHandler : new DefaultModableTypeHandler();
		moskitoCacheStats = cache.getCacheStats();
	}


	@Override
	public V get(K id) {
		if (isFailOverCall(id))
			return null;
		return cache.get(id);
	}

	public void put(K id, V cacheable) {
		if (!isFailOverCall(id))
			cache.put(id, cacheable);
	}

	@Override
	public String toString() {
		return cache.toString() + ", instanceAmount=" + instanceAmount + ", currentInstanceNumber=" + currentInstanceNumber;
	}

	@Override
	public void clear() {
		cache.clear();
	}

	@Override
	public void remove(K id) {
		if (!isFailOverCall(id))
			cache.remove(id);
	}

	@Override
	public CacheStats getCacheStats() {
		return moskitoCacheStats;
	}

	/**
	 * Detect if this call was for failover or not
	 *
	 * @param id cache key to stored
	 * @return
	 */
	private boolean isFailOverCall(Object id) {
		if (instanceAmount < 2 || currentInstanceNumber < 0)
			return false;
		return currentInstanceNumber != (Math.abs(modableTypeHandler.getModableValue(id)) % instanceAmount);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy