SpringBoot默认缓存

依赖引入

  1. <!-- 缓存依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-cache</artifactId>
  5. </dependency>

开启缓存

在启动类上添加注解 @EnableCaching 开启缓存功能。

服务层方法

@Service
@CacheConfig(cacheNames = "BooksCache")
public class BookServiceImpl implements BookService{

    @Resource
    private BookMapper bookMapper;

    @Override
    @CacheEvict(key = "#id")
    public int deleteByPrimaryKey(Integer id) {
        return bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    @CachePut(key = "#book.id")
    public int insert(Book book) {
        return bookMapper.insert(book);
    }

    @Override
    @CachePut(key = "#book.id")
    public int insertSelective(Book book) {
        return bookMapper.insertSelective(book);
    }

    @Override
    @Cacheable
    public Book selectByPrimaryKey(Integer id) {
        return bookMapper.selectByPrimaryKey(id);
    }

    @Override
    @CachePut(key = "#book.id")
    public int updateByPrimaryKeySelective(Book book) {
        return bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    @CachePut(key = "#book.id")
    public int updateByPrimaryKey(Book book) {
        return bookMapper.updateByPrimaryKey(book);
    }

}
  1. @CacheConfig 注解用于指定本类中方法使用的缓存名称,该类使用的缓存名称为 BooksCache ,与其他缓存区域是隔离的。
  2. @Cacheable 用于开启方法缓存,缓存的键是方法的参数,缓存的值是方法的返回值。如果多次调用该方法时参数 id 值相同,则第一次会执行方法体,并将返回值放入缓存;后续方法不会再执行方法体,直接将缓存的值返回。
  3. @CachePut 可以更新缓存,key = “#id” 表示采用参数中的 id 属性作为键。当缓存中该键的值不存在时,则将返回值放入缓存;当缓存中该键的值已存在时,会更新缓存的内容。
  4. @CacheEvict 可以移除缓存,当调用该方法时,会移除 goods 中 id 属性对应的缓存内容。

使用 Ehcache 缓存

Ehcache 是 Java 编程领域非常著名的缓存框架,具备两级缓存数据——内存和磁盘,因此不必担心内存容量问题。另外 Ehcache 缓存的数据会在 JVM 重启时自动加载,不必担心断电丢失缓存的问题。

依赖引入

        <!-- Ehcache 依赖 -->
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
        <!-- cache-api 依赖 -->
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>

配置文件

spring.cache.jcache.config=classpath:ehcache.xml
spring.cache.type=jcache
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
    <!-- 持久化路径 -->
    <persistence directory="C://ehcache" />
    <!--缓存模板 -->
    <cache-template name="CacheTemplate">
        <expiry>
            <!--存活时间 -->
            <tti>60</tti>
        </expiry>
        <resources>
            <!--堆空间 -->
            <heap unit="entries">2000</heap>
            <!-- 堆外空间 -->
            <offheap unit="MB">500</offheap>
        </resources>
    </cache-template>

    <!--缓存对象 -->
    <cache alias="GoodsCache" uses-template="CacheTemplate">
    </cache>
</config>

使用 Redis 缓存

修改依赖为redis


        <!-- 缓存依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- Redis 相关依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

redis配置

# 过期时间
spring.cache.redis.time-to-live=6000s

# Redis库的编号
spring.redis.database=0
# Redis实例地址
spring.redis.host=127.0.0.1
# Redis实例端口号,默认6379
spring.redis.port=6379
# Redis登录密码
spring.redis.password=Easy@0122
# Redis连接池最大连接数
spring.redis.jedis.pool.max-active=10
# Redis连接池最大空闲连接数
spring.redis.jedis.pool.max-idle=10
# Redis连接池最小空闲连接数
spring.redis.jedis.pool.min-idle=0