Skip to content

Commit 2939e65

Browse files
committed
1.springboot2 ehcache2 demo
1 parent c247b22 commit 2939e65

File tree

9 files changed

+294
-0
lines changed

9 files changed

+294
-0
lines changed

spring-boo2-ehcache2/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.7.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.zzy</groupId>
12+
<artifactId>spring-boo2-ehcache2</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boo2-ehcache2</name>
15+
<description>springboot2结合ehcache2 demo</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-actuator</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<!-- caching -->
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-cache</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>net.sf.ehcache</groupId>
47+
<artifactId>ehcache</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.google.guava</groupId>
51+
<artifactId>guava</artifactId>
52+
<version>22.0</version>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.zzy;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cache.annotation.EnableCaching;
6+
7+
@SpringBootApplication
8+
@EnableCaching
9+
public class SpringBoo2Ehcache2Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBoo2Ehcache2Application.class, args);
13+
}
14+
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.zzy.controller;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import com.zzy.entity.User;
5+
import com.zzy.service.UserService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
/**
16+
* <p></p>
17+
* Created by zhezhiyong@163.com on 2017/9/21.
18+
*/
19+
@RestController
20+
public class IndexController {
21+
22+
@Autowired
23+
private UserService userService;
24+
25+
@GetMapping("/users")
26+
@ResponseBody
27+
public List<User> users() {
28+
return userService.list();
29+
}
30+
31+
@GetMapping("/findUser/{id}")
32+
@ResponseBody
33+
public User findUserById(@PathVariable("id") Long id) {
34+
return userService.findUserById(id);
35+
}
36+
37+
@GetMapping("/updateUser/{id}")
38+
@ResponseBody
39+
public User updateUserById(@PathVariable("id") Long id) {
40+
User user = userService.findUserById(id);
41+
user.setName("updateUserById");
42+
userService.update(user);
43+
return user;
44+
}
45+
46+
@GetMapping("/deleteUser/{id}")
47+
@ResponseBody
48+
public Map deleteUser(@PathVariable("id") Long id) {
49+
userService.remove(id);
50+
return ImmutableMap.of("ret", 0, "msg", "ok");
51+
}
52+
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.zzy.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* <p></p>
10+
* Created by zhezhiyong@163.com on 2017/9/22.
11+
*/
12+
@Data
13+
@AllArgsConstructor
14+
public class Info implements Serializable {
15+
private String phone;
16+
private String address;
17+
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.zzy.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* <p></p>
11+
* Created by zhezhiyong@163.com on 2017/9/21.
12+
*/
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class User implements Serializable{
17+
18+
private Long id;
19+
private String name;
20+
private String password;
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.zzy.service;
2+
3+
4+
import com.zzy.entity.User;
5+
6+
import java.util.List;
7+
8+
/**
9+
* <p></p>
10+
* Created by zhezhiyong@163.com on 2017/9/21.
11+
*/
12+
public interface UserService {
13+
14+
List<User> list();
15+
16+
User findUserById(Long id);
17+
18+
User update(User user);
19+
20+
void remove(Long id);
21+
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.zzy.service.impl;
2+
3+
import com.zzy.entity.User;
4+
import com.zzy.service.UserService;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.cache.annotation.CacheConfig;
7+
import org.springframework.cache.annotation.CacheEvict;
8+
import org.springframework.cache.annotation.CachePut;
9+
import org.springframework.cache.annotation.Cacheable;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
/**
18+
* <p></p>
19+
* Created by zhezhiyong@163.com on 2017/9/21.
20+
*/
21+
@Service
22+
@Slf4j
23+
@CacheConfig(cacheNames = "user")
24+
public class UserServiceImpl implements UserService {
25+
26+
private Map<Long, User> userMap = new HashMap<>();
27+
28+
public UserServiceImpl() {
29+
userMap.put(1L, new User(1L, "aaa", "666666"));
30+
userMap.put(2L, new User(2L, "bbb", "666666"));
31+
userMap.put(3L, new User(3L, "ccc", "666666"));
32+
}
33+
34+
@Override
35+
public List list() {
36+
return Arrays.asList(userMap.values().toArray());
37+
}
38+
39+
@Override
40+
@Cacheable(key = "#id")
41+
public User findUserById(Long id) {
42+
log.info("findUserById query from db, id: {}", id);
43+
return userMap.get(id);
44+
}
45+
46+
@Override
47+
@CachePut(key = "#user.id")
48+
public User update(User user) {
49+
log.info("update db, user: {}", user.toString());
50+
userMap.put(user.getId(), user);
51+
return user;
52+
}
53+
54+
@Override
55+
@CacheEvict(key = "#id")
56+
public void remove(Long id) {
57+
log.info("remove from db, id: {}", id);
58+
// userMap.remove(id);
59+
}
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.application.name=springboot2ehcache2
2+
spring.cache.type=ehcache
3+
spring.cache.ehcache.config=classpath:ehcache.xml
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
4+
updateCheck="false">
5+
6+
<diskStore path="user.home/ehcachefile/" />
7+
8+
<!-- name:Cache的唯一标识
9+
maxElementsInMemory:内存中最大缓存对象数
10+
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
11+
eternal:Element是否永久有效,一但设置了,timeout将不起作用
12+
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
13+
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
14+
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
15+
diskPersistent:是否缓存虚拟机重启期数据
16+
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
17+
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
18+
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用) -->
19+
20+
<defaultCache
21+
maxElementsInMemory="10000"
22+
eternal="false"
23+
overflowToDisk="true"
24+
timeToIdleSeconds="900"
25+
timeToLiveSeconds="900"
26+
diskPersistent="false"
27+
memoryStoreEvictionPolicy="LRU"
28+
diskExpiryThreadIntervalSeconds="120"/>
29+
30+
<!-- 30分钟刷新一次缓存 -->
31+
<cache name="user" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="900"
32+
timeToLiveSeconds="900" overflowToDisk="true" maxElementsOnDisk="10000" diskPersistent="true"
33+
diskExpiryThreadIntervalSeconds="120"
34+
memoryStoreEvictionPolicy="LRU"/>
35+
36+
</ehcache>

0 commit comments

Comments
 (0)