SpringBoot缓存使用方式

简介: spring boot 提供的缓存技术除了提供默认的缓存方案,还可以对其他缓存技术进行整合,统一接口,方便缓存技术的开发与管理。

Simple(默认)

Ehcache

Redis

memcached

jetcache(阿里)

SpringBoot默认缓存方案

导入依赖

   <!--cache缓存技术-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

package com.lele;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
 
@SpringBootApplication
//开启缓存功能
@EnableCaching
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootCacheApplication.class, args);
    }
 
}

在Service层上的方法上加注解 key保证唯一性


package com.lele.Service.ServiceImpl;
 
import com.lele.Dao.BookDao;
import com.lele.Service.BookService;
import com.lele.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
@Service
public class BookServiceImpl implements BookService {
 
    @Autowired
    private BookDao bookDao;
 
    @Override
    //开启缓存注解
    @Cacheable(value = "cacheSpace",key = "#id")
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }
 
    @Override
    public boolean save(Book book) {
        int insert = bookDao.insert(book);
        return true;
    }
}

ehcache

pom


        <!--ehcache 缓存技术-->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

#缓存
spring:
  cache:
    type: ehcache

2.配置eache.xml文件,并放置到ClassPath下


<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <!-- 磁盘保存路径 -->
    <diskStore path="E:/cache/cache"/>
 
    <defaultCache
            eternal="false"
            diskExpiryThreadIntervalSeconds="120"
            maxElementsInMemory="1000"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            memoryStoreEvictionPolicy="LRU"/>
        <cache
                name="smsCode"
                eternal="false"
                diskExpiryThreadIntervalSeconds="120"
                maxElementsInMemory="1000"
                overflowToDisk="true"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                memoryStoreEvictionPolicy="LRU"/>
 
 
</ehcache>
 
        <!--
        属性说明:
        l diskStore:当内存中不够存储时,存储到指定数据在磁盘中的存储位置。
        l defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
        以下属性是必须的:
        l maxElementsInMemory - 在内存中缓存的element的最大数目
        l maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
        l eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
        l overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
        以下属性是可选的:
        l timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
        l timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
         diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
        l diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
        l diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
        l memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
        -->
目录
相关文章
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1
|
1月前
|
缓存 NoSQL Java
【九】springboot整合redis实现启动服务时热点数据保存在全局和缓存
【九】springboot整合redis实现启动服务时热点数据保存在全局和缓存
43 0
|
5月前
|
缓存 NoSQL Java
分布式系列教程(03) -分布式Redis缓存(SpringBoot整合Redis)
分布式系列教程(03) -分布式Redis缓存(SpringBoot整合Redis)
103 0
|
5月前
|
缓存 Java Maven
微服务技术系列教程(07) - SpringBoot - 缓存的使用
微服务技术系列教程(07) - SpringBoot - 缓存的使用
45 0
|
6月前
|
缓存 数据可视化 NoSQL
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
158 1
|
3月前
|
缓存 NoSQL Java
面试官:SpringBoot如何实现缓存预热?
面试官:SpringBoot如何实现缓存预热?
83 0
|
4月前
|
缓存 NoSQL Java
springboot集成图片验证+redis缓存一步到位2
springboot集成图片验证+redis缓存一步到位2
|
4月前
|
缓存 NoSQL Java
springboot集成图片验证+redis缓存一步到位
springboot集成图片验证+redis缓存一步到位
|
4月前
|
缓存 NoSQL Java
SpringBoot - Spring缓存默认配置与运行流程
SpringBoot - Spring缓存默认配置与运行流程
30 1
|
4月前
|
缓存 NoSQL Java
SpringBoot - 缓存入门详解与注解使用实例
SpringBoot - 缓存入门详解与注解使用实例
95 1

热门文章

最新文章