Ehcache 与 SpringBoot 整合
1、maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
2、ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
overflowToDisk="true"
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="USER"
overflowToDisk="true"
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
3、application.properties
spring.cache.ehcache.config=classpath:ehcache.xml
4、SpringBoot启动类中添加注解
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(CookieDemoApplication.class, args);
}
}
5、ICacheService.java
package com.demo.service;
public interface ICacheService {
void set(String cacheName, String key, Object value);
<T> T get(String cacheName, String key, Class<T> cls);
void del(String cacheName, String key);
}
6、EhcacheServiceImpl.java
package com.demo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.demo.service.ICacheService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
@Service
public class EhcacheServiceImpl implements ICacheService {
@Autowired
private CacheManager cacheManager;
@Override
public void set(String cacheName, String key, Object value) {
this.cacheManager.getCache(cacheName).put(key, JSONObject.toJSONString(value));
}
@Override
public <T> T get(String cacheName, String key, Class<T> cls) {
String str = this.cacheManager.getCache(cacheName).get(key, String.class);
if (StringUtils.isBlank(str)) {
return null;
}
return JSONObject.parseObject(str, cls);
}
@Override
public void del(String cacheName, String key) {
this.cacheManager.getCache(cacheName).evictIfPresent(key);
}
}
7、User.java
package com.demo.model;
public class User {
private String userId;
private String userName;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
8、单元测试
package com.demo.utils;
import com.demo.model.User;
import com.demo.service.impl.EhcacheServiceImpl;
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.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheUtilsTest {
private static final String CACHE_NAME = "USER";
@Autowired
private EhcacheServiceImpl cacheService;
@Test
public void test() {
this.cacheService.set(CACHE_NAME, "lbb", new User() {{
setUserId("1");
setUserName("lbb");
setPassword("123456");
}});
User user = this.cacheService.get(CACHE_NAME, "lbb", User.class);
System.out.println(user);
this.cacheService.del(CACHE_NAME, "lbb");
user = this.cacheService.get(CACHE_NAME, "lbb", User.class);
System.out.println(user);
}
}
9、结果输出
User{userId='1', userName='lbb', password='123456'}
null