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

com.datastax.insight.core.entity.Cache Maven / Gradle / Ivy

The newest version!
package com.datastax.insight.core.entity;

import org.apache.commons.collections.map.HashedMap;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;

/*public class Cache {
    private static Map cacheMap=new HashedMap();

    public static void addCache(String key,Object value){
        if(cacheMap.containsKey(key)){
            cacheMap.remove(key);
        }
        cacheMap.put(key,value);
    }

    public static Object getCache(String key){
        return cacheMap.get(key);
    }
}*/

public class Cache {
    private static InheritableThreadLocal  threadLocal=new InheritableThreadLocal();

    public Cache() {
    }

    public static void addCache(String key, Object value) {
        Object o = threadLocal.get();
        Map map=null;
        if (o!=null){
            map= (Map) o;
        }else {
            map=new HashMap<>();
        }

        if (map.containsKey(key)) {
            map.remove(key);
        }
        map.put(key, value);
        threadLocal.set(map);
    }

    public static Object getCache(String key) {

        Object o = threadLocal.get();
        if (o==null){
            threadLocal.set(new HashMap<>());
        }
        return ((Map)(threadLocal.get())).get(key);
    }
}