SpringBoot——Cache缓存初探,java基础入门第二版知识点总结

public List testCache(String key){

List 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 testPutCache(String key){

List 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.annot

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ations.ApiOperation;

import java.util.List;

/**

  • //TODO 添加类/接口功能描述

  • @author 刘朋


  • date 2018-09-06

*/

@Api(description = “第一个接口”)

@RestController

@RequestMapping("/hello")

public class HelloWorldController {

@Autowired

private CacheService cacheService ;

@ApiOperation(value = “测试缓存” )

@GetMapping("/testCache")

public String testCache(){

//第一次中缓存中查询

List test = cacheService.testCache(“test”);

System.out.println(test);

//修改缓存中的值

List test2 = cacheService.testPutCache(“test”);

System.out.println(test2);

//再次从缓存中查询

List test3 = cacheService.testCache(“test”);

System.out.println(test3);

return “”;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值