项目总结71:Caffeine的基本概念和使用

start

pom引用

        <!-- Spring boot Cache-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
        <!--for caffeine cache-->
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.7.0</version>
        </dependency>

  application.properties配置

self.caffeine.maxsize=1000
self.caffeine.expire.time=100

 

  CaffeineConfig配置类

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

//https://www.cnblogs.com/fnlingnzb-learner/p/11025565.html
@EnableCaching
@Configuration
public class CaffeineCacheConfig {

    @Value("${self.caffeine.maxsize}")
    public Long maximumSize;
    @Value("${self.caffeine.expire.time}")
    public Long expireTime;

    @Bean("caffeineCacheManager")
    public CacheManager  caffeineCacheManager(){
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(
                Caffeine.newBuilder()//创建Caffeine实例(并使用默认配置)
                .recordStats()//开启默认打点监控策略
                .expireAfterWrite(maximumSize, TimeUnit.SECONDS)//每次写入10秒后过期
                .maximumSize(expireTime));//缓存最大条数
        return  cacheManager;
    }

}

 

  service类

public interface CaffeineService {

    void putValue(String cacheName,Object key, Object value);

    <T>T getValue(String cacheName, String key);
}

@Service
public class CaffeineServiceImpl implements CaffeineService {

    @Autowired
    private CacheManager caffeineCacheManager;

    private final static String DEFAULT_CACHE = "default";

    @Override
    public void putValue(String cacheName,Object key, Object value) {
        if(key == null || value == null){
            return;
        }
        if(cacheName == null){
            cacheName = DEFAULT_CACHE;
        }
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null){
            cache.put(key,value);
        }
    }

    @Override
    public <T> T getValue(String cacheName, String key) {
        if(cacheName == null || key == null){
            return null;
        }
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null){
            Cache.ValueWrapper valueWrapper = cache.get(key);
            if(valueWrapper != null){
                return (T)valueWrapper.get();
            }
        }
        return null;
    }
}

 

  controller类

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

    @Autowired
    private CaffeineService caffeineService;

    //http://localhost:8080/caffeine/put?key=k1&value=v1
    @GetMapping("/put")
    public String put(
            @RequestParam(value="cacheName",required = false)String cacheName,
            @RequestParam String key,
            @RequestParam String value
    ){
        caffeineService.putValue(cacheName,key,value);
        return "success";
    }


    //http://localhost:8080/caffeine/get?key=k1&cacheName=default
    @GetMapping("/get")
    public String get(
            @RequestParam String cacheName,
            @RequestParam String key
    ){
        String value = caffeineService.getValue(cacheName, key);
        return "success: " + value;
    }

}

 

end 

 

posted on 2020-09-16 20:20  我不吃番茄  阅读(1129)  评论(1编辑  收藏  举报