SpringBoot——Cache缓存初探

SpringBoot——Cache缓存初探SpringBoot——Cache缓存初探

大家好,又见面了,我是你们的朋友全栈君。

@Cacheable:查询数据,如果缓存中有,直接从缓存中取,如果没有执行方法中代码获取结果,并存入缓存中。 
@CachePut:修改缓存的值 
@EnableCaching:项目启动时扫描缓存注解 

话不多说,直接开始上代码。

1.创建缓存服务

下面service中写了两个方法,一个是查询缓存,一个是修改缓存

 package com.youyou.address.service;

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

import java.util.ArrayList;
import java.util.List;

/**
 * 这是一个测试缓存service
 *
 * @author 刘朋
 * <br/>date 2018-10-24
 */

@Service
public class CacheService {

    /**
     * 查询缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @Cacheable(cacheNames = "testList" , key = "#key")
    public List<String> testCache(String key){
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add(key);
        return list;
    }

    /**
     * 修改缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @CachePut(cacheNames = "testList" , key = "#key")
    public List<String> testPutCache(String key){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add(key);
        return list;
    }
}

2.添加缓存扫描

在启动类上添加缓存扫描注解

 package com.youyou;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@MapperScan("com.youyou.*")//将项目中对应的mapper类的路径加进来就可以了
@EnableCaching  //如果想启动缓存,需要加此注解
public class WorldMainApplication {

    public static void main(String[] args) {
        //启动项目
        SpringApplication.run(WorldMainApplication.class, args);
    }
}

到此为止缓存服务已经创建完成。

来让我们测试一下!

 package com.youyou.address;

import com.youyou.address.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * //TODO 添加类/接口功能描述
 *
 * @author 刘朋
 * <br/>date 2018-09-06
 */

@Api(description = "第一个接口")
@RestController
@RequestMapping("/hello")
public class HelloWorldController {

    @Autowired
    private CacheService cacheService ;

    @ApiOperation(value = "测试缓存" )
    @GetMapping("/testCache")
    public String testCache(){
       //第一次中缓存中查询
        List<String> test = cacheService.testCache("test");
        System.out.println(test);

        //修改缓存中的值
        List<String> test2 = cacheService.testPutCache("test");
        System.out.println(test2);

        //再次从缓存中查询
        List<String> test3 = cacheService.testCache("test");
        System.out.println(test3);
        return "";
    }

}

访问接口查看打印结果如下:

SpringBoot——Cache缓存初探

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106466.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月23日 上午8:20
下一篇 2022年4月23日 上午8:20


相关推荐

  • 设计模式——行为型模式

    设计模式——行为型模式一 目录 1 策略模式 Strategy 2 状态模式 State 3 责任链模式 ChainOfRespo 4 解释器模式 Interpreter 5 命令模式 Command 6 观察者模式 Observer 7 备忘录模式 Memento 8 迭代器模式 Iterator 9 模板方法模式 TemplateMeth 10 访问者模式 Visit

    2025年10月25日
    4
  • 超标量、超流水、超线程

    超标量、超流水、超线程一 超流水线技术超流水线 SuperPieline 技术是 RISC 采用的一种并行处理技术 他通过细化流水 增加级数和提高主频 使得在每个机器周期内能完成一个甚至两个浮点操作 其实质就是以时间换取空间 超流水机器的特征就是在所有的功能单元都才用流水 并有更高的时钟频率和更深的流水深度 二 超标量技术超标量 SuperScalar 技术是 RISC 采用的有一种处理技术 它通过内装多

    2026年3月16日
    3
  • 易语言脚本开发入门教程

    易语言脚本开发入门教程天蓝易语言脚本入门开发系列教程第 1 讲初识易语言天蓝易语言脚本入门开发系列教程第 2 课简单控件变量天蓝易语言脚本入门开发系列教程第 3 讲简单登录器制作天蓝易语言脚本入门开发系列教程第 4 讲计算器实现天蓝易语言脚本入门开发系列教程第 5 讲时钟随机数天蓝易语言脚本入门开发系列教程第 6 讲子程序的应用天蓝易语言脚本入门开发系列教程第 7 讲选择夹列表框天蓝易语言脚本入门开发系列教程第 8 讲读写配置项天蓝易语言脚本入门开发系列教程第 9 讲数组天蓝易语言脚本入门开发系列教程第 10 讲分割文本读取帐号密码天蓝易

    2026年3月18日
    2
  • heap和stack区别Java_Java中Heap与Stack的区别

    heap和stack区别Java_Java中Heap与Stack的区别1)Heap是Stack的一个子集.——扩展—>从内存观点考虑。优化2)Stack存取速度仅次于寄存器,存储效率比heap高,可共享存储数据,可是其中数据的大小和生存期必须在运行前肯定。spa3)Heap是运行时可动态分配的数据区,从速度看比Stack慢,Heap里面的数据不共享,大小和生存期均可以在运行时再肯定。指针4)new关键字是运行时在Heap里面建立对象,每ne…

    2025年8月1日
    4
  • 腾讯元宝混元ai在哪儿

    腾讯元宝混元ai在哪儿

    2026年3月12日
    2
  • c语言删除数组中的元素「建议收藏」

    c语言删除数组中的元素「建议收藏」删除一个元素,相同也可删除核心思想:1.找到元素用if语句2.删除就是用后面的代替该元素(需要删除的元素),用for语句3.遍历(就是用for循环看一遍数列)就可以找到想要删除的元素,4.注意最后要给末尾换成零,因为后面的是随机的不一定为零#include<stdio.h>intmain(){ inti,a[10]; intb,c; //输入数组值 printf(“输入数组的值”); for(i=0;i<10;i++) { scanf(“%d”

    2022年7月22日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号