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

org.treeleaf.cache.local.LocalCacheImpl Maven / Gradle / Ivy

The newest version!
package org.treeleaf.cache.local;

import org.treeleaf.cache.Cache;
import org.treeleaf.cache.CacheException;

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

/**
 * Created by yaoshuhong on 2015/8/20.
 */
public class LocalCacheImpl implements Cache {

    private static Map cache = new HashMap<>();
    private static Map cacheMap = new HashMap<>();
    private static Map cacheList = new HashMap<>();

    @Override
    public void set(String key, Object object, int... expireSeconds) throws CacheException {
        cache.put(key, object);
    }

    @Override
    public  T get(String key, Class classz) throws CacheException {
        return (T) cache.get(key);
    }

    @Override
    public  List mget(String[] keys, Class... classz) throws CacheException {
        if (classz.length > 0) {
            List objList = new ArrayList<>(keys.length);
            for (String key : keys) {
                objList.add(this.get(key, classz[0]));
            }
            return objList;
        } else {
            List list = new ArrayList<>(keys.length);
            for (String key : keys) {
                list.add(this.get(key));
            }
            return (List) list;
        }
    }

    @Override
    public boolean del(String key) throws CacheException {
        Object r = cache.remove(key);
        return r != null;
    }

    @Override
    public boolean expire(String key, int seconds) throws CacheException {
        return false;
    }

    @Override
    public boolean exists(String key) throws CacheException {
        return cache.containsKey(key);
    }

    @Override
    public void setByNoExists(String key, Object value) throws CacheException {
        if (!cache.containsKey(key)) {
            cache.put(key, value);
        }
    }

    @Override
    public void setMapValueByNoExists(String key, String entryKey, String value) throws CacheException {
        Map m = cacheMap.get(key);
        if (m != null) {
            m.put(entryKey, value);
        }
    }

    @Override
    public String get(String key) throws CacheException {
        return (String) cache.get(key);
    }

    @Override
    public void setMap(String key, Map value, int... expireSeconds) throws CacheException {
        cacheMap.put(key, value);
    }

    @Override
    public Map getMap(String key) throws CacheException {
        return cacheMap.get(key);
    }

    @Override
    public void setMapValue(String key, String entryKey, String entryValue) throws CacheException {
        Map m = cacheMap.get(key);
        if (m == null) {
            m = new HashMap();
            cacheMap.put(key, m);
        }
        m.put(entryKey, entryValue);
    }

    @Override
    public String getMapValue(String key, String entryKey) throws CacheException {
        Map m = cacheMap.get(key);
        if (m != null) {
            return (String) m.get(entryKey);
        }
        return null;
    }

    @Override
    public void setStringList(String key, List value, int... expireSeconds) throws CacheException {
        cacheList.put(key, value);
    }

    @Override
    public  void setList(String key, List value, int... expireSeconds) throws CacheException {
        cacheList.put(key, value);
    }

    @Override
    public  List getList(String key, Class... classz) throws CacheException {
        return cacheList.get(key);
    }

    @Override
    public void pushQueue(String key, Object value) throws CacheException {
        List list = cacheList.get(key);
        if (list == null) {
            list = new ArrayList();
            cacheList.put(key, list);
        }
        list.add(value);
    }

    @Override
    public  T popQueue(String key, Class... classz) throws CacheException {
        List list = cacheList.get(key);
        if (list != null && list.size() > 0) {
            return (T) list.get(0);
        }
        return null;
    }

    @Override
    public long incrementBy(String key, int num) throws CacheException {
        throw new RuntimeException("暂未实现");
    }

    @Override
    public long incrementMapValueBy(String key, String entryKey, int num) {
        throw new RuntimeException("暂未实现");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy