zoukankan      html  css  js  c++  java
  • SpringBoot集成Caffeine作本地缓存

    1. 依赖引入
    spring-cache
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    
    caffeine
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
    
    1. 参数配置(一般配置可以完全放在yaml文件内,不用专门写配置bean)
    spring:
      cache:
        type: caffeine
        cache-names: 'ruleCache'
        caffeine:
          spec: initialCapacity=1000, maximumSize=18000, expireAfterAccess=20D
    
    1. 缓存逻辑编写
    package com.cfuture.dataqualitycontrol.service.impl;
    
    import com.cfuture.dataqualitycontrol.domain.po.Rule;
    import com.cfuture.dataqualitycontrol.service.RuleService;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    
    /**
     * @author yangjx
     * @date 2021/1/21 14:04
     * @description
     */
    @Service
    public class RuleServiceImpl implements RuleService {
    
        @Override
        @CachePut(value = "ruleCache", key = "#rule.id")
        public Rule addRule(Rule rule) {
            return rule;  // 必须返回被缓存对象
        }
    
        @Override
        @Cacheable(value = "ruleCache", key = "#id")
        public Rule getRule(Long id) {
            Rule rule = Rule.builder().expression(String.valueOf(id)).message(String.valueOf(System.currentTimeMillis())).build();
            rule.setId(id);
            return rule;  // 必须返回被缓存对象
        }
    
        @Override
        @CacheEvict(value = "ruleCache", key = "#id")
        public boolean deleteRule(Long id) {
            return true;
        }
    
        @Override
        @CachePut(value = "ruleCache", key = "#rule.id")
        public Rule updateRule(Rule rule) {
            return rule;  // 必须返回被缓存对象
        }
    
    }
    
    学习使我充实,分享给我快乐!
  • 相关阅读:
    mysql5.6.20安装
    唯一识别Windows机器的最佳方法
    不用Root在安卓手机上运行Kali_Linux
    在Windows 7和10上显示上次登录帐户信息
    优化非活动窗口的颜色
    Windows10中以管理员身份打开命令提示符
    在Windows10中更改”WIN+E“快捷键打开目标
    在任务管理器中显示所有CPU内核性能
    Windows启动控制台登录模式
    Fluent Terminal
  • 原文地址:https://www.cnblogs.com/JaxYoun/p/14313070.html
Copyright © 2011-2022 走看看