• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Springboot2.X + 本地缓存之王Caffeine(79)

互联网 diligentman 3天前 9次浏览

对比性能,摘自网络

Springboot2.X + 本地缓存之王Caffeine(79)

Springboot2.X + 本地缓存之王Caffeine(79)

1、pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

2、配置类

默认配置本地缓存的容量、过期时间等

@Configuration
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        List<CaffeineCache> list        = new ArrayList<>(8);


        for (CacheNamesEnum cacheNamesEnum : CacheNamesEnum.values()){
            list.add(new CaffeineCache(cacheNamesEnum.getName(),
                    Caffeine.newBuilder()
                            .initialCapacity(50)
                            .maximumSize(1000)
                            .expireAfterWrite(cacheNamesEnum.getExpires(),TimeUnit.SECONDS)
                            .build()));
        }

        cacheManager.setCaches(list);
        return cacheManager;
    }
}

3、需要存储的集合统一管理到枚举类中

/**
 * caffeine所有cacheName
 * @author mujiutian

 */
@Getter
@AllArgsConstructor
public enum CacheNamesEnum {
    /**
     * 所有本地缓存name,各个map对应的key,相当于redis中hash存储 field,key,value中的field
     */
    CJ("CJ",20),
    FAVORITE_MAP("favorite",30),
    ZY_MANAGER_CHANNEL_MAP("zyManagerChannel",30),
    ZY_USER_MAP("zyUserMap",30),
    URL_UID_MAP("urlUidMap",60),
    CACHE_MISS_MAP("cacheMissMap",15),
    ALL_CHANNEL_CACHE_MAP("allChannelMapCache",60),
    SYSTEM_CHANNEL_FLAG("systemChannelFlag",30),
    TAG_ONLINE("onLineTag",120),
    TAG_SON_LINE("",120);

    /** name **/
    private final String name;
    /** 过期时间,时间单位秒 **/
    private final int expires;
}

4、配置通用类

/**
 * 缓存存储通用方法
 * @author mujiutian
 * @date 2020/10/28
 */
@Service
@Slf4j
public class CaffeineService {
    @Resource
    CacheManager cacheManager;

    public void put(CacheNamesEnum cacheNamesEnum, Object k, Object v){
        if (k == null){
            return;
        }
        CaffeineCache cache = (CaffeineCache) cacheManager.getCache(cacheNamesEnum.getName());
        if (cache == null){
            return;
        }

        cache.getNativeCache().asMap().put(k,v);
    }

    public Object get(CacheNamesEnum cacheNamesEnum, Object k){
        if (k == null){
            return null;
        }
        CaffeineCache cache = (CaffeineCache) cacheManager.getCache(cacheNamesEnum.getName());
        if (cache == null){
            return null;
        }

        ConcurrentMap<Object,Object> map = cache.getNativeCache().asMap();
        if (CollUtil.isEmpty(map)){
            return null;
        }

        return cache.getNativeCache().asMap().getOrDefault(k,null);
    }
}

5、相关测试OK

@RestController
@RequestMapping(value = "/caffeine")
public class CaffeineController {

    @Autowired
    CacheManager cacheManager;

    @Autowired
    CaffeineService caffeineService;

    @GetMapping(value = "/test1")
    public String test1(String id){
        caffeineService.put(CacheNamesEnum.CJ,id,"6666");
        System.out.println(caffeineService.get(CacheNamesEnum.CJ,id));
        return "OK";
    }

    @GetMapping(value = "/test2")
    public String test2(String id){
        return (String) caffeineService.get(CacheNamesEnum.CJ,id);
    }
}

 

{{o.name}}


{{m.name}}


喜欢 (0)