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;  // 必须返回被缓存对象
        }
    
    }
    
    学习使我充实,分享给我快乐!
  • 相关阅读:
    单点登录
    企业SOA架构案例分析
    京东峰值系统设计
    阿里搜索离线大数据平台架构
    国内不FQ使用ARCore
    Unity Android上视频使用Seek方法跳转有误差
    《搬砖日记》Obi Rope插件的简单使用
    《搬砖日记》AssetsBundle实现资源更新及通过反射添加脚本
    入园一周年
    《搬砖日记》Unity原生MicroPhone的使用
  • 原文地址:https://www.cnblogs.com/JaxYoun/p/14313070.html
Copyright © 2011-2022 走看看