springboot2.x整合ehcache3.x

正文

添加依赖

maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- cache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.7.1</version>
</dependency>
<!-- JSR107 API -->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- cache -->
</dependencies>

添加配置

application.properties中添加如下配置

1
2
spring.cache.type=jcache
spring.cache.jcache.config=classpath:ehcache.xml

resources文件夹下面增加文件ehcache.xml,内容如下

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
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
<!-- 持久化路径 -->
<persistence directory="/opt/wacos/server/data/antitamperserver"/>

<!-- 缓存模版,此处为了显示其用法,也可以不用模版直接在cache中配置与模版参数相同 -->
<cache-template name="template">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<!-- 单位默认为entries当用entries作单位时,可以不填-->
<heap>1</heap>
<offheap unit="MB">1</offheap>
<!-- persistent 默认为false可以不填-->
<disk unit="MB">200</disk>
</resources>
</cache-template>

<!-- 缓存对象,如果使用了模版会覆盖模版中的内容,使用uses-template=""来引用模版 -->
<cache alias="md5cache" uses-template="template">
<value-type>com.utstar.filemonitoring.entity.Md5Cache</value-type>
<resources>
<disk unit="MB" persistent="true">5000</disk>
</resources>
</cache>

</config>

注意:以上ehcache配置表示支持持久化,在写入内存得时候同样会写入文件,重启后数据再次写入内存

编写配置类和服务类

如下是根据上面ehcache.xml中配置得所编写配置类和服务类
配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.utstar.ucloud.springcloud.ucloudmpc.conf;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

/**
* FileName: CacheConfiguration
* Author: creambing
* Description: 缓存
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
@Configuration
@EnableCaching
public class CacheConfiguration {
}

服务类

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
package com.utstar.filemonitoring.service;

import com.utstar.filemonitoring.entity.Md5Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import javax.cache.Cache;
import javax.cache.CacheManager;

import static com.utstar.filemonitoring.constants.Constants.CACHE_NAME_MD5;

/**
* FileName: Md5CacheService
* Author: creambing
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
@CacheConfig(cacheNames = "md5cache")
@Slf4j
@Service
public class Md5CacheService {

private final CacheManager cacheManager;

public Md5CacheService(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

@SuppressWarnings("UnusedReturnValue")
@CachePut(key = "#id")
public Md5Cache save(String id, Md5Cache value) {
return value;
}

public boolean isKeyExists(String key) {
Cache<String, Md5Cache> cache = cacheManager.getCache(CACHE_NAME_MD5, String.class, Md5Cache.class);
return cache.containsKey(key);
}

public Md5Cache get(String key) {
Cache<String, Md5Cache> cache = cacheManager.getCache(CACHE_NAME_MD5, String.class, Md5Cache.class);
return cache.get(key);
}


@CacheEvict(key = "#id")
public void remove(String id) {
}

}

编写测试类测试上面服务类

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
package com.utstar.filemonitoring.test;

import com.utstar.filemonitoring.init.AfterServiceStarted;
import com.utstar.filemonitoring.init.FileMonitoringInit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AntitamperServerApplicationTests {

@MockBean
FileMonitoringInit fileMonitoringInit;

@MockBean
AfterServiceStarted afterServiceStarted;

@Test
public void contextLoads() {
}

}

mockbean的这些类中的逻辑是springboot启动后需要执行的并且比较耗时,可以利用mockbean注入一个空类

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
package com.utstar.filemonitoring.test.service;

import com.utstar.filemonitoring.entity.Md5Cache;
import com.utstar.filemonitoring.test.AntitamperServerApplicationTests;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.annotation.Resource;

/**
* Created by [creambing.github.io]
* Date: 2019/9/16
* Time: 11:22
*/
public class Md5CacheServiceTest extends AntitamperServerApplicationTests {

@Resource
Md5CacheService md5CacheService;

private Md5Cache md5Cache = new Md5Cache();
private String key = "/topic/4600000133/json/ setting.json";
private String md5 = "sdfsfsffsdf";
@Before
public void before(){
md5Cache.setMd5(md5);
md5Cache.setOptime("121313131");
}

@Test
public void save() {
md5CacheService.save(key,md5Cache);
}

@Test
public void isKeyExists() {
Boolean f = md5CacheService.isKeyExists(key);
Assert.assertEquals(true,f);
}

@Test
public void get() {
Md5Cache md5Cache = md5CacheService.get(key);
Assert.assertEquals(md5,md5Cache.getMd5());
}

@Test
public void remove() {
md5CacheService.remove(key);
Boolean f = md5CacheService.isKeyExists(key);
Assert.assertEquals(false,f);
}
}

参考资料

Cream Bing wechat
subscribe to my blog by scanning my public wechat account