• 用hashmap实现自己的缓存


    @SuppressWarnings({"unchecked", "rawtypes"})
    public class DefaultCache implements Cache {
    private Map cache = new HashMap();

    private DefaultCache() {
    }

    public static DefaultCache getInstance() {
    return DefaultCacheFactory.getInstance();
    }

    private static class DefaultCacheFactory{
    private static DefaultCache instance = new DefaultCache();

    private static DefaultCache getInstance(){
    return instance;
    }
    }

    public void init() {
    cache = new HashMap();
    }

    public void stop() {
    }

    public void add(String key, Object value) {
    cache.put(key, value);
    }

    public void add(String fqn, String key, Object value) {
    Map map = (Map) cache.get(fqn);
    if (map == null) {
    synchronized (this) {
    map = (Map) cache.get(fqn);
    if(map == null){
    map = new HashMap();
    map.put(key, value); // 防止指令重排序
    cache.put(fqn, map);
    }
    }
    }
    map.put(key, value);
    }

    public Object get(String fqn, String key) {
    Map map = (Map) cache.get(fqn);
    if (map == null) {
    return null;
    }
    return map.get(key);
    }

    public Object get(String key) {
    return cache.get(key);
    }

    public Collection getValues(String key) {
    Map map = (Map) cache.get(key);
    if (map == null) {
    return new ArrayList();
    }
    return map.values();
    }

    public void remove(String fqn, String key) {
    Map map = (Map) cache.get(fqn);
    if (map != null) {
    map.remove(key);
    }
    }

    public void remove(String key) {
    cache.remove(key);
    }
    }

  • 相关阅读:
    个人项目作业
    软件工程个人博客作业
    软件工程热身作业
    OO第四单元作业总结
    OO第三单元作业总结
    OO第二单元作业总结
    酸甜苦辣皆阅历,悲欢离合尽人生——软件工程个人总结
    Centos7里yum出问题可以试试
    idea 报错 Two modules in a project cannot share the same content root
    关于本地git的补充
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7834619.html
Copyright © 2020-2023  润新知