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

com.skynet.infrastructure.InMemoryCacheService Maven / Gradle / Ivy

The newest version!
package com.skynet.infrastructure;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InMemoryCacheService  extends InfraBaseService implements CacheService {
	
	Map cacheMap;
	public InMemoryCacheService() {
		
	}

	public Object get(String key,Class clazz) {
		ensureCacheMap();
		synchronized(cacheMap){//protect HashMap as it is not a thread safe class
			return cacheMap.get(key);
		}
	}
	public List mget(Listkeys,Class clazz) {
		throw new IllegalStateException("InMemoryCacheService not implemented mget(Listkeys,Class clazz)");
	}
	
	public void put(String key, Object value,int ttlInSeconds) {
		ensureCacheMap();
		synchronized(cacheMap){
			cacheMap.put(key,value);
		}
		
	}
	protected void ensureCacheMap(){
		if(cacheMap != null){
			return;
		}
		cacheMap = new HashMap();
	}

	public void remove(String key) {
		// TODO Auto-generated method stub
		ensureCacheMap();
		synchronized(cacheMap){
			cacheMap.remove(key);
		}
	}

}