Spring Boot缓存实战 默认Cache(ConcurrentMapCacheManager)
来源:互联网 发布:帝国时代2mac 编辑:程序博客网 时间:2024/05/15 22:42
Spring boot默认使用的是SimpleCacheConfiguration,即使用ConcurrentMapCacheManager来实现缓存。
pom文件
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency></dependencies>
关键Service
package com.xiaolyuh.service.impl;import com.xiaolyuh.entity.Person;import com.xiaolyuh.repository.PersonRepository;import com.xiaolyuh.service.PersonService;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.stereotype.Service;@Servicepublic class PersonServiceImpl implements PersonService { @Autowired PersonRepository personRepository; @Override @CachePut(value = "people", key = "#person.id") public Person save(Person person) { Person p = personRepository.save(person); System.out.println("为id、key为:" + p.getId() + "数据做了缓存"); return p; } @Override @CacheEvict(value = "people")//2 public void remove(Long id) { System.out.println("删除了id、key为" + id + "的数据缓存"); //这里不做实际删除操作 } @Override @Cacheable(value = "people", key = "#person.id")//3 public Person findOne(Person person) { Person p = personRepository.findOne(person.getId()); System.out.println("为id、key为:" + p.getId() + "数据做了缓存"); return p; }}
Controller
package com.xiaolyuh.controller;import com.xiaolyuh.entity.Person;import com.xiaolyuh.service.PersonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class CacheController { @Autowired PersonService personService; @RequestMapping("/put") public long put(@RequestBody Person person) { Person p = personService.save(person); return p.getId(); } @RequestMapping("/able") public Person cacheable(Person person) { return personService.findOne(person); } @RequestMapping("/evit") public String evit(Long id) { personService.remove(id); return "ok"; }}
启动类
@SpringBootApplication@EnableCaching// 开启缓存,需要显示的指定public class SpringBootStudentCacheApplication { public static void main(String[] args) { SpringApplication.run(SpringBootStudentCacheApplication.class, args); }}
测试类
package com.xiaolyuh;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import java.util.HashMap;import java.util.Map;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import net.minidev.json.JSONObject;@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringBootStudentCacheApplicationTests { @Test public void contextLoads() { } private MockMvc mockMvc; // 模拟MVC对象,通过MockMvcBuilders.webAppContextSetup(this.wac).build()初始化。 @Autowired private WebApplicationContext wac; // 注入WebApplicationContext // @Autowired // private MockHttpSession session;// 注入模拟的http session // // @Autowired // private MockHttpServletRequest request;// 注入模拟的http request\ @Before // 在测试开始前初始化工作 public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void testAble() throws Exception { for (int i = 0; i < 2; i++) { MvcResult result = mockMvc.perform(post("/able").param("id", "2")) .andExpect(status().isOk())// 模拟向testRest发送get请求 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))// 预期返回值的媒体类型text/plain; // charset=UTF-8 .andReturn();// 返回执行请求的结果 System.out.println(result.getResponse().getContentAsString()); } }}
打印日志
从上面可以看出第一次走的是数据库,第二次走的是缓存
源码:
https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
spring-boot-student-cache 工程
阅读全文
0 0
- Spring Boot缓存实战 默认Cache(ConcurrentMapCacheManager)
- Spring Boot 数据缓存 Cache
- Spring Boot缓存实战 EhCache
- Spring Boot缓存实战 Redis
- Spring Boot缓存实战 Caffeine
- Spring Cache集成spring默认缓存实现
- Spring Boot 菜鸟教程 17 Cache-缓存
- Spring Boot 轻量级缓存 Ace-Cache
- spring boot redis cache 缓存学习
- spring boot guava cache 缓存学习
- spring boot caffeine cache 缓存学习
- Spring boot的数据缓存Cache
- spring boot guava cache 缓存学习
- Spring Boot缓存实战 Redis + Caffeine 实现多级缓存
- Spring缓存简单介绍(Spring Cache)
- Spring Boot实战(二)
- Spring Boot实战(三)
- spring-boot配置文件-cache(中文翻译)
- ibatis查询数据显示为ASCII码的问题
- java编程思想笔记-并发之死锁
- spring boot学习指南
- 观察者模式
- 详解PHP中instanceof关键字及instanceof关键字有什么作用
- Spring Boot缓存实战 默认Cache(ConcurrentMapCacheManager)
- 关于protobuf 枚举未指定默认值的情况
- 进程、线程、内存池及实现简易线程池
- 使用Atomikos配置jta来实现activiti和业务数据库事务性
- Echarts自适应浏览器大小
- SystemUI 5.1 自定义额外功能点
- 《.NET 规范》第 3 章 命名规范
- Mysql常用命令行大全
- HDU 6103 Kirinriki(尺取法 思维)