一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - Spring Boot緩存實戰 EhCache示例

Spring Boot緩存實戰 EhCache示例

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

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

Spring boot默認使用的是SimpleCacheConfiguration,即使用ConcurrentMapCacheManager來實現緩存。但是要切換到其他緩存實現也很簡單

pom文件

在pom中引入相應的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會自動掃描。

?
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會自動為我們配置EhCacheCacheMannager的Bean。

關鍵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() + "數據做了緩存");
    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

?
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";
  }
 
}

啟動類

?
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對象,通過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());
    }
  }
 
}

打印日志

Spring Boot緩存實戰 EhCache示例

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 吃胸膜奶视频456 | 四虎视屏| 嗯好爽视频 | 午夜在线观看免费完整直播网页 | 成人特级毛片69免费观看 | 美女脱一光二净的视频 | 男人的j放进女人的p全黄 | 91次元成年破解版 | 精品久久久久久 | 青青热久麻豆精品视频在线观看 | 男人天堂网站在线 | 婷婷激情综合五月天 | 国产日韩片 | 果冻传媒在线视频观看免费 | 午夜影院网页 | oneday高清在线观看 | 色综合久久中文字幕网 | 日韩精品视频在线播放 | 俄罗斯伦理片 | 性bbwbbwbbwbbw撒尿 | 欧美一区二区三区gg高清影视 | 天美传媒在线视频 | 亚洲国产成人精品不卡青青草原 | 忘忧草在线社区WWW日本-韩国 | 国产码一区二区三区 | 亚洲国产在线播放在线 | 美女沟厕撒尿全过程高清图片 | 国产一区精品视频 | 天天综合天天综合 | 欧美午夜视频一区二区 | 国产精品每日在线观看男人的天堂 | 波多野结衣中文字幕乱七八糟 | 99国产小视频| 日本精品久久久久久久久免费 | ai换脸杨颖啪啪免费网站 | 亚洲国产精品久久久久久网站 | 色综合色综合 | a级aaaaaaaa毛片 | 久久99国产精品二区不卡 | 99热在线这里只有精品 | 男人边吃奶边做好爽视频免费 |