SpringCache使用-基础使用


一.引入jar      io.ifa.chaos.starters    chaos-starter-redis-caffeine-cache    1.0   org.springframework.boot   spring-boot-starter-data-redis 二,修改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;   /** *

* 前端控制器 *

* * @author ly * @since 2020-11-01 */ @Controller @RequestMapping("/stu") @ResponseBody public class StuController {   @Autowired private StuService stuService;   @Autowired private RedisTemplate 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 getStuInfo(@RequestBody Stu stu) { List 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 getStuInfoPut(@RequestBody Stu stu) { List 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 getStuInfoEvict(@RequestBody Stu stu) { List list=stuService.list(); //redisTemplate.opsForValue().set("bb","123456"); System.out.println("Redis========Test========="+redisTemplate.opsForValue().get("bb")); return list; } }   五,总结  这里只是介绍了springcache简单使用,相信学会使用还是不难的嘿嘿,如果有问题,欢迎大家留言,我会在第一时间及时改正。