一.引入jar
<!--springcache 2級緩存-->
<dependency>
<groupId>io.ifa.chaos.starters</groupId>
<artifactId>chaos-starter-redis-caffeine-cache</artifactId>
<version>1.0</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二,修改application.yml
spring:
application:
name: appcache
cache:
cache-names: cachename
redis:
host: 127.0.0.1
port: 6379
三.修改啟動類
@EnableCaching (加上這個注解)
@SpringBootApplication
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
四,編寫Service
package com.springcache.app.controller;
import com.springcache.app.entity.Stu;
import com.springcache.app.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author ly
* @since 2020-11-01
*/
@Controller
@RequestMapping("/stu")
@ResponseBody
public class StuController {
@Autowired
private StuService stuService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
/**
* @Cacheable(key="'stu'+#stu.id",value = "cachename")
*
* stu字符串+stu.id 當做Redis的Key 這個方法返回的結果集當做這個key的value。
* value = "cachename" 這個是application.yml 配置的緩存名稱
* @Cacheable的作用場景為:
* 首先會檢查上述的Key是否存在,存在直接返回Key對應的緩存結果
* @param stu
* @return
*/
@RequestMapping("/getStuInfo")
@Cacheable(key="'stu'+#stu.id",value = "cachename")
public List<Stu> getStuInfo(@RequestBody Stu stu) {
List<Stu> list=stuService.list();
//redisTemplate.opsForValue().set("bb","123456");
System.out.println("Redis========Test========="+redisTemplate.opsForValue().get("bb"));
return list;
}
/**
* @CachePut(key="'stu'+#stu.id",value = "cachename")
*
* stu字符串+stu.id 當做Redis的Key 這個方法返回的結果集當做這個key的value。
* value = "cachename" 這個是application.yml 配置的緩存名稱
* @CachePut的作用場景為:
* 檢車上述的Key是否存在,存在不會返回緩存中的結果,依然會走業務邏輯查詢數據庫,不存在就將上述的key和方法返回的結果
* 緩存到Redis中,然后還是會走業務邏輯去查詢數據庫
* @param stu
* @return
*/
@RequestMapping("/getStuInfoPut")
@CachePut(key="'stu'+#stu.id",value = "cachename")
public List<Stu> getStuInfoPut(@RequestBody Stu stu) {
List<Stu> list=stuService.list();
//redisTemplate.opsForValue().set("bb","123456");
System.out.println("Redis========Test========="+redisTemplate.opsForValue().get("bb"));
return list;
}
/**
* @CacheEvict(key="'stu'+#stu.id",value = "cachename")
*
* @CacheEvict會檢車Key是否存在,存在就刪除Key對應的緩存。
* @param stu
* @return
*/
@RequestMapping("/getStuInfoEvict")
@CacheEvict(key="'stu'+#stu.id",value = "cachename")
public List<Stu> getStuInfoEvict(@RequestBody Stu stu) {
List<Stu> list=stuService.list();
//redisTemplate.opsForValue().set("bb","123456");
System.out.println("Redis========Test========="+redisTemplate.opsForValue().get("bb"));
return list;
}
}
五,總結
這里只是介紹了springcache簡單使用,相信學會使用還是不難的嘿嘿,如果有問題,歡迎大家留言,我會在第一時間及時改正。