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;  // 必须返回被缓存对象
        }
    
    }
    
    学习使我充实,分享给我快乐!
  • 相关阅读:
    java_doc
    zai~~myGODDDD
    get span time
    someThing about thread
    互斥
    http://www.vchelp.net/services/about_us/itbookreview_intro.asp
    (十三)sealed、new、virtual、abstract 和 override java程序员
    (15) 常用基础知识 java程序员
    (14)abstract class 和 interface java程序员
    (16) 结构和类 java程序员
  • 原文地址:https://www.cnblogs.com/JaxYoun/p/14313070.html
Copyright © 2011-2022 走看看