Caffeine的集成Springboot的简单使用

在使用本地缓存的时候,可以很好解决分布式缓存的单点问题,所以一般本地缓存有ehcache,guava和Caffeine。
guava已经在Spring5种不支持了,通过sprinboot的自动配置可以看到没有了guava。
在这里插入图片描述
现在将Caffeine的使用简单描述如下:

第一步、引入依赖

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

第二步、设置CacheManager

package com.jd.ins.qsm.demo.web.commom.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import org.springframework.cache.CacheManager;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class CacheConfig {

    @Bean
    public CacheManager caffeineCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<CaffeineCache> caffeineCaches = new ArrayList<>();
        caffeineCaches.add(new CaffeineCache("QSM",//缓存名,可以理解为一个缓存库
                Caffeine.newBuilder()
                        .expireAfterWrite(10, TimeUnit.SECONDS)//失效策略,写入10秒之后失效
                        .build()));
        cacheManager.setCaches(caffeineCaches);
        return cacheManager;
    }

}

第三步,写业务
service层

package com.jd.ins.qsm.demo.web.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CaffeineService {
    /**
     * 查询
     * @param key 作为缓存里面的key
     * @return 回源方法,若缓存没有值,则回源查询
     */
    @Cacheable(value = "QSM", key = "#key")
    public String cacheQSM(String key) {
        log.info("cacheQSM方法正在运行");
        try {
            log.info("查询后端数据中");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //可以访问redis或者数据库获取真实值
        return RandomStringUtils.randomNumeric(32);
    }

    @CachePut(value = "QSM", key = "#key")
    public String cachePutQSM(String key) {
        log.info("cachePutQSM方法正在运行");
        return key;
    }
}


controller层

package com.jd.ins.qsm.demo.web.controller;


import com.jd.ins.qsm.demo.web.service.impl.CaffeineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CaffeineController {

    @Autowired
    private CaffeineService caffeineService;

    @GetMapping("/caffeine/select/{key}")
    public String cacheQSM(@PathVariable String key) {
        return caffeineService.cacheQSM(key);
    }

    @GetMapping("/caffeine/put/{key}")
    public String cachePutQSM(@PathVariable String key) {
        return caffeineService.cachePutQSM(key);
    }
}

第四步调用
http://localhost:8080/caffeine/select/qsm01
会发现,第一次调用需要等5秒,接下来10秒都秒回。过了这10s,再次获取再次等5s。

总结:多级缓存和redis集群很好解决了大流量访问的非常有效的方法。

正在去往BAT的路上修行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值