parent
ed53659c0e
commit
849afdd0dc
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>opsli-plugins</artifactId>
|
||||
<groupId>org.opsliframework.boot</groupId>
|
||||
<version>1.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>opsli-plugins-ehcache</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
|
||||
<dependencies>
|
||||
<!-- 集成EhCache缓存 BEGIN -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
<version>${ehcache.version}</version>
|
||||
</dependency>
|
||||
<!-- 集成EhCache缓存 END -->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,45 @@
|
||||
package org.opsli.plugins.cache;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.cache
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-16 19:19
|
||||
* @Description: EhCache 缓存接口
|
||||
*/
|
||||
public interface EhCachePlugin {
|
||||
|
||||
/**
|
||||
* 添加缓存数据
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
boolean put(String cacheName, String key, Object value);
|
||||
|
||||
/**
|
||||
* 获取缓存数据
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
Object get(String cacheName, String key);
|
||||
|
||||
/**
|
||||
* 获取缓存数据
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
<V> V get(String cacheName, String key ,Class<V> vClass);
|
||||
|
||||
|
||||
/**
|
||||
* 删除缓存数据
|
||||
* @param cacheName
|
||||
* @param key
|
||||
*/
|
||||
boolean delete(String cacheName, String key);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.opsli.plugins.cache.conf;
|
||||
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.cache.conf
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-16 21:35
|
||||
* @Description: EhCache
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class EhCacheConfig {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package org.opsli.plugins.cache.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ehcache.core.EhcacheManager;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.opsli.plugins.cache.EhCachePlugin;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.cache.service
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-16 21:08
|
||||
* @Description: EhCachePlugin 实现类
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class EhCachePluginImpl implements EhCachePlugin {
|
||||
|
||||
@Autowired(required = false)
|
||||
CacheManager cacheManager;
|
||||
|
||||
@Override
|
||||
public boolean put(String cacheName, String key, Object value) {
|
||||
boolean ret = false;
|
||||
try {
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if(cache != null){
|
||||
cache.put(key,value);
|
||||
ret = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("添加缓存失败:{}",e.getMessage());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String cacheName, String key) {
|
||||
try {
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if(cache != null){
|
||||
return cache.get(key);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取缓存数据失败:{}",e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> V get(String cacheName, String key, Class<V> vClass) {
|
||||
try {
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if(cache != null){
|
||||
return cache.get(key,vClass);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取缓存数据失败:{}",e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String cacheName, String key) {
|
||||
boolean ret = false;
|
||||
try {
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if(cache != null){
|
||||
cache.evict(key);
|
||||
ret = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("删除缓存数据失败:{}",e.getMessage());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue