美文网首页
Guava简单本地缓存使用例子

Guava简单本地缓存使用例子

作者: 长坡雪球笑眯眯 | 来源:发表于2018-07-12 10:41 被阅读0次

简单的本地缓存
整体思路:
把缓存作为全局的静态变量,调用方法时,在方法里从cache获取,没有的话去数据库查,再放入cache

import com.nn.ead.common.dict.DictType;
import com.nn.ead.common.domain.Dict;
import com.nn.ead.common.mapper.DictMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictMapper dictMapper;
    //建立本地cache
    private static final Cache<DictType, List<Dict>> cache = CacheBuilder.newBuilder()
            .maximumSize(100)
            .expireAfterAccess(1, TimeUnit.DAYS)
            .build();

    @Override
    public String getDesc(DictType dictType, int value) {
        List<Dict> dictList = cache.getIfPresent(dictType);
        if (dictList == null) {
            dictList = dictMapper.selectAll(dictType);
            if (CollectionUtils.isNotEmpty(dictList)) {
                cache.put(dictType, dictList);
            } else {
                return null;
            }
        }
        for (Dict dict : dictList) {
            if (dict.getValue() == value) {
                return dict.getDesc();
            }
        }
        return null;
    }

    @Override
    public Map<Integer, String> getMap(DictType dictType) {
        List<Dict> dictList = cache.getIfPresent(dictType);
        if (dictList == null) {
            dictList = dictMapper.selectAll(dictType);
            if (CollectionUtils.isNotEmpty(dictList)) {
                cache.put(dictType, dictList);
            }
        }
        if (dictList == null) {
            return new HashMap<>();
        }
        return dictList.stream().collect(Collectors.toMap(Dict::getValue, Dict::getDesc));
    }

}

相关文章

  • Guava简单本地缓存使用例子

    简单的本地缓存整体思路:把缓存作为全局的静态变量,调用方法时,在方法里从cache获取,没有的话去数据库查,再放入...

  • Guava Cache

    原文 使用Guava cache构建本地缓存 - sameLuo的个人空间 - OSCHINA Guava Cac...

  • springboot整合guava cache本地缓存

    guava cache 是本地缓存应用比较广的,支持定制化设置缓存,包括缓存数量、缓存时间,简单几步就可以完成本地...

  • guavaCache本地缓存失效方案expireAfterWri

    Guava实现本地缓存,为了保证缓存一致性,本地缓存需要被动的失效(即设置失效时间)。Guava Cache有两种...

  • 三、为什么要用redis而不用map做缓存?

    缓存分为本地缓存和分布式缓存。以java为例,使用自带的map或者guava实现的是本地缓存,最主要的特点是轻量以...

  • Guava Cache实现原理浅析

    一.概述 在上篇文章《Guava Cache做本地缓存那些事》中我介绍了Guava Cache作为本地缓存的一般用...

  • Guava缓存

    Guava缓存是谷歌的一种本地缓存,缓存是通过使用本机的内存来存储的,实现原理类似于ConcurrentHashM...

  • guava本地缓存getAll方法

    最近一个项目大量使用了guava的本地缓存服务。使用方式是对第三方数据接口返回的结果做本地缓存。 当时太年轻了,为...

  • 高并发系统技术梳理

    缓存 缓存使用常见问题归纳Guava Cache系列之一Guava Cache系列之二Guava Cache系列之...

  • guava 本地缓存

    介绍 Guava cache是本地缓存的一种实现。 Guava Cache与ConcurrentMap很相似,但也...

网友评论

      本文标题:Guava简单本地缓存使用例子

      本文链接:https://www.haomeiwen.com/subject/jpulpftx.html