springboot中的缓存介绍之cache

前言
Spring框架支持透明地向应用程序添加缓存对缓存进行管理,其管理缓存的核心是将缓存应用于操作数据的方法(包括增删查改等),从而减少操作数据的执行次数(主要是查询,直接从缓存中读取数据),同时不会对程序本身造成任何干扰。

SpringBoot继承了Spring框架的缓存管理功能,通过使用@EnableCaching注解开启基于注解的缓存支持,SpringBoot就可以启动缓存管理的自动化配置

常用注解介绍
Spring Boot整合Redis缓存并使用注解
在Spring Boot应用程序中,您可以使用Spring Cache库与Redis缓存进行集成,以提高应用程序的性能和响应速度。Spring Cache库提供了一组注解,包括@Cacheable、@CachePut和@CacheEvict,可以方便地将方法的返回值缓存到Redis中,并根据需要进行刷新和清除。

本篇博客将向您展示如何在Spring Boot项目中整合Redis缓存,并使用注解来管理缓存操作

@EnableCaching、@Cacheable

这两个注解都是spring提供的,可以结合不同的缓存技术使用

@EnableCaching

@EnableCaching是开启缓存功能,作用于缓存配置类上或者作用于springboot启动类上

@Cacheable

@Cacheable 注解在方法上,表示该方法的返回结果是可以缓存的。也就是说,该方法的返回结果会放在缓存中,以便于以后使用相同的参数调用该方法时,会返回缓存中的值,而不会实际执行该方法。如果缓存过期,则重新执行。
注解常用的几个属性:

cacheNames/value(二选一) :用来指定缓存组件的名字
key :缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写)
keyGenerator :key 的生成器。 key 和 keyGenerator 二选一使用
cacheManager :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。
condition :可以用来指定符合条件的情况下才缓存
unless :否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过#result 获取方法结果)
sync :是否使用异步模式

1、首先,在您的Spring Boot项目的pom.xml文件中添加必要的依赖项以使用Redis和Spring Cache:

<dependencies>
    <!-- 其他依赖项 -->

    <!-- Redis依赖项 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- Spring Cache依赖项 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
</dependencies>

接下来,您需要在application.properties或application.yml配置文件中添加Redis连接的相关配置信息:
在您的Spring Boot应用程序主类上或者配置类redisConfig添加@EnableCaching注解,以启用缓存支持:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class YourApplication {

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }

}

现在,您可以在需要缓存的方法上使用@Cacheable、@CachePut和@CacheEvict注解:

Cacheable(查询缓存)
@Cacheable注解用于缓存方法的返回值,并在后续调用时从缓存中获取结果,而不再执行实际的方法体。

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @Cacheable("books")
    public Book findBookById(String id) {
        // 从数据库或其他数据源获取书籍信息
        return book;
    }

}

在上述示例中,findBookById方法的返回值将被缓存到名为"books"的缓存中。当再次调用该方法时,将从缓存中获取结果,而不会执行方法体。

@CachePut(更新缓存)
@CachePut注解用于将方法的返回值存储到缓存中,类似于@Cacheable注解,但它每次都会执行方法体

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @CachePut("books")
    public Book updateBook(Book book) {
        // 更新数据库或其他数据源中的书籍信息
        return book;
    }

}

在上述示例中,每次调用updateBook方法时,都会执行方法体,并将返回的书籍信息存储到名为"books"的缓存中。
@CacheEvict(使缓存失效)
@CacheEvict注解用于从缓存中移除指定的条目,可以在方法调用之前、之后或同时触发。

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @CacheEvict("books")
    public void deleteBook(String id) {
        // 从数据库或其他数据源中删除书籍信息
    }

}

在上述示例中,每次调用deleteBook方法时,都会从名为"books"的缓存中移除相应的条目。
步骤5:使用缓存注解进行方法缓存
在步骤4中,我们已经介绍了@Cacheable、@CachePut和@CacheEvict注解的基本用法。现在,让我们更详细地了解这些注解的使用方法。
@Cacheable
@Cacheable注解可用于方法级别,用于指定方法的返回值应该被缓存起来。可以使用value属性指定要使用的缓存名称,还可以使用key属性来定义缓存的键。
示例:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @Cacheable(value = "books", key = "#id")
    public Book findBookById(String id) {
        // 从数据库或其他数据源获取书籍信息
        return book;
    }

}

在上述示例中,findBookById方法的返回值将被缓存到名为"books"的缓存中,并且使用id作为缓存的键。
@CachePut
@CachePut注解可用于方法级别,用于将方法的返回值存储到缓存中。与@Cacheable注解不同的是,@CachePut注解每次都会执行方法体。
示例:

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @CachePut(value = "books", key = "#book.id")
    public Book updateBook(Book book) {
        // 更新数据库或其他数据源中的书籍信息
        return book;
    }
}

在上述示例中,每次调用updateBook方法时,都会执行方法体,并将返回的书籍信息存储到名为"books"的缓存中,使用book.id作为缓存的键。
@CacheEvict
@CacheEvict注解可用于方法级别,用于从缓存中移除指定的条目。可以使用value属性指定要清除的缓存名称,还可以使用key属性来定义要清除的缓存键。
示例:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @CacheEvict(value = "books", key = "#id")
    public void deleteBook(String id) {
        // 从数据库或其他数据源中删除书籍信息
    }

}

在上述示例中,每次调用deleteBook方法时,将从名为"books"的缓存中移除具有给定id的条目。
结论
通过使用@Cacheable、@CachePut和@CacheEvict注解,您可以方便地使用Redis缓存来提高Spring Boot应用程序的性能和响应速度。这些注解使得方法的结果可以被缓存、更新或清除,从而减少对后端资源的访问。

其他工具类封装:

import lombok.AllArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

/**
 * @author lanhai
 */
@Component
@AllArgsConstructor
public class CacheManagerUtil {

    private CacheManager cacheManager;

    @SuppressWarnings({"unchecked"})
    public <T> T getCache(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            return null;
        }
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper == null) {
            return null;
        }
        return (T)valueWrapper.get();
    }

    public void putCache(String cacheName,String key, Object value) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            cache.put(key, value);
        }
    }

    public void evictCache(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            cache.evict(key);
        }
    }
}

使用方法:

  List<T>list= cacheManagerUtil.getCache("cacheName", key);
  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值