springboot集成springcache

今天尝试了一下springboot集成springcache做缓存,springcache是基于annotation(注释)的一个缓存技术

特点总结如下:

  • 通过少量的配置 annotation 注释即可使得既有代码支持缓存
  • 支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存
  • 支持 Spring Express Language,能使用对象的任何属性或者方法来定义缓存的 key 和 condition
  • 支持 AspectJ,并通过其实现任何方法的缓存支持
  • 支持自定义 key 和自定义缓存管理者,具有相当的灵活性和扩展性

使用起来也非常的简单

第一步:添加maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

第二步:新建一个TestTime类

import java.util.Date;

public class TestTime {
    private Date time;
    public TestTime(Date time){
        this.time=time;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "{ 时间=‘" + time + ‘\‘‘ + ‘}‘;
    }
}

第三步,再添加一个TestTime接口

import hello.entity.TestTime;

public interface TestTimeService {
    TestTime getTestTime();
}

第四步,到主函数添加:

@EnableCaching

开启缓存,这个非常重要

第五步,实现该接口:

import hello.Service.TestTimeService;
import hello.entity.TestTime;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TestTimeController implements TestTimeService {

    @Override
    @Cacheable(value = "time")
    public TestTime getTestTime(){
        return new TestTime(new Date());
    }

}

使用@Cachable就可以使用缓存了,非常简单。

关于缓存注解的仔细介绍可以参考文章:https://www.cnblogs.com/fashflying/p/6908028.html

第六步,创建一个测试类TestRun:

import hello.Service.TestTimeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRun {

    private static final Logger log = LoggerFactory.getLogger(TestRun.class);

    @Autowired
    TestTimeService testTimeService;

    @Test
    public void getTime() throws InterruptedException {
        int i = 1;
        while (i <= 20) {
            log.info("" + "第" + i + "次获取时间" + testTimeService.getTestTime());
            i++;
            Thread.sleep(1000);
        }
    }

}

创建的目录如下:

最后:运行getTime测试方法:

可以看到,20次去调用gettesttime,获取的并不是最新的时间,而是缓存内的时间。

本文源码:https://gitee.com/Hiro-D/Java/tree/master/Spring-cache

直接使用springcahce是无法设置缓存有效时间的,是要使用ehcache或者redis 或者guava设置,下篇讲一下springcahce设置过期时间。

原文地址:https://www.cnblogs.com/a565810497/p/10931426.html

时间: 2024-08-30 17:43:09

springboot集成springcache的相关文章

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成swagger2构建RESTful API文档

在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可以在访问接口上,直接添加注释 先介绍一下开发环境: jdk版本是1.8 springboot的版本是1.4.1 开发工具为 intellij idea 我们先引入swagger2的jar包,pom文件引入依赖如下: <dependency> <groupId>io.springfox&

spring-boot集成Springfox-Swagger2

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documenta

【spring-boot】spring-boot集成ehcache实现缓存机制

EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题. spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 由于spring-boot无需任何样板化的配置文件,所以spring-boot集成一些其他框架时会有略微的

SpringBoot集成MyBatis的分页插件PageHelper(回头草)

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成redis详解

欢迎扫码加入Java高知群交流 springboot集成redis非常简单 1.引入maven依赖redis包 <!-- springboot整合 redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> 2.appli

springboot集成jsp

springboot集成jsp需要的依赖如下: <!--配置servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!--配置jsp jstl的支持--> <dependency> <groupId>javax.se

eclipse创建Maven Project(springboot集成dubbo第一步)

!!!每一种方式都是自己的工具,熟悉就好,献给最初的我与你 创建maven项目 注意此处:我勾选了第一个按钮 1:file->New Maven Project->next     2:输入内容->完成 Group Id :组名 常指公司名(任意) ArtiFact Id :项目名(任意),其它默认   3:项目初步总览(此时项目有感叹号)     3:修改java版本:项目名 右键->properties ->选中 java Build Path ->JRE Syst

springboot 集成mybatis

1:依赖mybaits分页插件 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> 2:启动类中@MapperScan 扫描接口包 @MapperScan("