首页 > 其他分享 >SpringBoot整合Caffeine(通过注解)

SpringBoot整合Caffeine(通过注解)

时间:2023-01-06 00:33:05浏览次数:53  
标签:缓存 SpringBoot CACHE Caffeine springframework caffeine import 注解

一、了解缓存配置

  先来了解一下配置方法吧,SimpleCacheManager和CaffeineCacheManager配置的区别:

SimpleCacheManager:
	这种缓存管理器允许你在应用程序启动时通过配置多个CaffeineCache来创建多个缓存。
	这种方式可以让你为每个方法单独配置缓存过期时间。
CaffeineCacheManager:
	这种缓存管理器使用了一个全局的Caffeine配置来创建所有的缓存。
	这种方式不能为每个方法单独配置缓存过期时间,但是可以在程序启动时配置全局的缓存配置,这样就可以轻松地设置所有方法的缓存过期时间。
总结:
	如果你希望为每个方法单独配置缓存过期时间,那么建议使用第一种方式。
	否则,如果你希望设置全局的缓存配置,那么建议使用第二种方式。

pom文件

<!--Spring缓存组件-->
<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>
    <version>2.6.2</version>
</dependency>

二、使用缓存:SimpleCacheManager方式

CacheNameTimeConstant:定义缓存的过期时间

/**
 * @ClassName CacheNameTimeConstant
 * @Author zhangzhixi
 * @Description 定义键的过期时间
 * @Date 2023-01-05 22:30
 * @Version 1.0
 */
public interface CacheNameTimeConstant {
    String CACHE_DEFAULT = "CACHE_DEFAULT";
    String CACHE_5SECS = "CACHE_5SECS";
    String CACHE_10SECS = "CACHE_10SECS";
    String CACHE_30SECS = "CACHE_30SECS";
}

CaffeineConfig:缓存配置

import com.github.benmanes.caffeine.cache.Caffeine;
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.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CaffeineConfig {

    @Bean
    public CacheManager localEntityCacheManager() {
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                // 初始大小
                .initialCapacity(10)
                // 最大容量
                .maximumSize(100)
                // 打开统计
                .recordStats()
                // 5分钟不访问自动丢弃
                .expireAfterAccess(5, TimeUnit.MINUTES);
        caffeineCacheManager.setCaffeine(caffeine);
        // 设定缓存器名称
        caffeineCacheManager.setCacheNames(getNames());
        // 值不可为空
        caffeineCacheManager.setAllowNullValues(false);
        return caffeineCacheManager;
    }

    private static List<String> getNames() {
        List<String> names = new ArrayList<>(1);
        names.add("localEntityCache");
        return names;
    }
}

Service层

UserService

/**
* @author zhangzhixi
* @description 针对表【user】的数据库操作Service
* @createDate 2022-05-01 12:15:07
*/
public interface UserService extends IService<User> {

    /**
     * 根据id查询用户
     *
     * @param userId 用户id
     * @return 用户信息
     */
    User selectByIdUser(Long userId);
    /**
     * 更新用户
     * @param user 实体
     * @return =1更新成功,否则更新失败
     */
    User updateUser(User user);

    /**
     * 删除用户
     * @param userId 用户id
     * @return =1删除成功,否则删除失败
     */
    boolean delUser(Long userId);
}

UserServiceImpl

 

Controller层

 

标签:缓存,SpringBoot,CACHE,Caffeine,springframework,caffeine,import,注解
From: https://www.cnblogs.com/zhangzhixi/p/17029258.html

相关文章

  • 8.注解开发
    对于简单的数据库操作可以使用注解开发,对于复杂的数据库操作不推荐使用!!mybatis的常用注解:@Insert:新增@Update:更新@Delete:删除@Select:查询@Result:实现结果集封......
  • 同事说不会在SpringBoot中集成日志记录操作我向他推荐了这篇文章
    SpringBoot中记录操作日志记一次SpringBoot中记录管理员的操作日志,记录管理员对用户、管理员、分类、视频等模块的操作,如:删除管理员、用户、视频、修改密码、信息等等,都......
  • @Import注解详解
    @Import注解详解1.原文链接:https://blog.csdn.net/weixin_45453628/article/details/1242343172.AnnotationConfigApplicationContext容器创建过程https://www.cnblogs......
  • springboot 资源分离 打包
    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</a......
  • 反射5 - 获取注解信息
    反射获取注解信息什么是ORM?ObjectrelationshipMapping--对象关系映射(数据库关系)类->表类中属性->表的字段类的对象->表中的记录通过注解联系属性与数据......
  • 注解2 - 元注解
    元注解meta-annotation注解其他注解这些类型和它们所支持的类在java.lang.annotation包中找到@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)@Retent......
  • Java Annotation-注解
    Java注解(Annotation)是JDK5.0引入的一种注释机制。Java语言中的类、方法、变量、参数和包等都可以被注解。和Javadoc不同,Java注解可以通过反射获取标注内容。在编译器生成......
  • 【SpringBoot】创建项目与启动
     创建项目说明  查看JDK配置    ......
  • RESTful风格与Spring注解
    RESTfulL是一种网络应用程序的设计风格和开发方式,即接口请求方式和路径的一种风格。普通风格:localhost:8080/add?a=1&b=2RestFul风格:localhost:8080/add/1/2GET获......
  • SpringBoot之HandlerInterceptor拦截器的使用
    ​​1、SpringBoot之HandlerInterceptor拦截器的使用——(一)​​​​2、SpringBoot之HandlerInterceptor拦截器的使用——(二)自定义注解​​​​3、SpringBoot之HandlerInte......