国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot緩存實(shí)戰(zhàn) EhCache示例

Spring Boot緩存實(shí)戰(zhàn) EhCache示例

2020-12-16 11:31xiaolyuh Java教程

本篇文章主要介紹了Spring Boot緩存實(shí)戰(zhàn) EhCache示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Spring boot默認(rèn)使用的是SimpleCacheConfiguration,即使用ConcurrentMapCacheManager來實(shí)現(xiàn)緩存。但是要切換到其他緩存實(shí)現(xiàn)也很簡單

pom文件

在pom中引入相應(yīng)的jar包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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>
  
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
  </dependency>
 
  <dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
  </dependency>
 
</dependencies>

配置文件

EhCache所需要的配置文件,只需要放到類路徑下,Spring Boot會(huì)自動(dòng)掃描。

?
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
  <cache name="people" maxElementsInMemory="1000"/>
</ehcache>

也可以通過在application.properties文件中,通過配置來指定EhCache配置文件的位置,如:

?
1
spring.cache.ehcache.config= # ehcache配置文件地址

Spring Boot會(huì)自動(dòng)為我們配置EhCacheCacheMannager的Bean。

關(guān)鍵Service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
 
@Service
public 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() + "數(shù)據(jù)做了緩存");
    return p;
  }
 
  @Override
  @CacheEvict(value = "people")//2
  public void remove(Long id) {
    System.out.println("刪除了id、key為" + id + "的數(shù)據(jù)緩存");
    //這里不做實(shí)際刪除操作
  }
 
  @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() + "數(shù)據(jù)做了緩存");
    return p;
  }
}

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.xiaolyuh.controller;
 
import com.xiaolyuh.entity.Person;
import com.xiaolyuh.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class CacheController {
 
  @Autowired
  PersonService personService;
 
  @Autowired
  CacheManager cacheManager;
 
  @RequestMapping("/put")
  public long put(@RequestBody Person person) {
    Person p = personService.save(person);
    return p.getId();
  }
 
  @RequestMapping("/able")
  public Person cacheable(Person person) {
    System.out.println(cacheManager.toString());
    return personService.findOne(person);
  }
 
  @RequestMapping("/evit")
  public String evit(Long id) {
 
    personService.remove(id);
    return "ok";
  }
 
}

啟動(dòng)類

?
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableCaching// 開啟緩存,需要顯示的指定
public class SpringBootStudentCacheApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(SpringBootStudentCacheApplication.class, args);
  }
}

測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
@SpringBootTest
public class SpringBootStudentCacheApplicationTests {
 
  @Test
  public void contextLoads() {
  }
 
  private MockMvc mockMvc; // 模擬MVC對(duì)象,通過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發(fā)送get請(qǐng)求
          .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))// 預(yù)期返回值的媒體類型text/plain;
          // charset=UTF-8
          .andReturn();// 返回執(zhí)行請(qǐng)求的結(jié)果
 
      System.out.println(result.getResponse().getContentAsString());
    }
  }
 
}

打印日志

Spring Boot緩存實(shí)戰(zhàn) EhCache示例

從上面可以看出第一次走的是數(shù)據(jù)庫,第二次走的是緩存

源碼:https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.jianshu.com/p/31e666f1ff57

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草超碰在线 | 亚洲色视频 | 激情综合在线 | 久热精品免费 | 欧美三级网站 | 亚洲精品免费av | 九九热这里都是精品 | 一区二区三区高清 | 欧美日韩国产精品 | 久久综合伊人77777蜜臀 | www.青青草原| 午夜色福利 | 久久午夜精品 | 成年片 | 精品视频免费观看 | 国产97人人超碰caoprom | 日韩欧美二区 | 日韩精品久久 | 成人午夜影院 | 中文字幕欧美在线 | 一区二区三区视频 | 国产精品自拍视频 | 黄色三级网站在线观看 | 国产精品欧美大片 | 免费黄色大片 | 一区二区三区四区在线 | 日韩在线不卡 | 亚洲www啪成人一区二区 | 国产在线精品一区 | 精品国产乱码一区二区三区 | 欧美一级裸体视频 | 91精品国产一区二区 | 亚洲视频欧美视频 | 日本中文字幕在线 | 1区2区在线观看 | 色狠狠综合天天综合综合 | 伊人99 | 免费日韩视频 | 精品国产一区探花在线观看 | 久草青青草| 中文字幕一级毛片 |