From 849afdd0dcaf8a9ab493e38c4f5dc180cef15c6f Mon Sep 17 00:00:00 2001 From: Parker Date: Wed, 16 Sep 2020 23:55:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=20EhCache=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opsli-plugins/opsli-plugins-ehcache/pom.xml | 34 ++++++++ .../opsli/plugins/cache/EhCachePlugin.java | 45 ++++++++++ .../plugins/cache/conf/EhCacheConfig.java | 18 ++++ .../cache/service/EhCachePluginImpl.java | 82 +++++++++++++++++++ opsli-plugins/pom.xml | 1 + 5 files changed, 180 insertions(+) create mode 100644 opsli-plugins/opsli-plugins-ehcache/pom.xml create mode 100644 opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java create mode 100644 opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/conf/EhCacheConfig.java create mode 100644 opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java diff --git a/opsli-plugins/opsli-plugins-ehcache/pom.xml b/opsli-plugins/opsli-plugins-ehcache/pom.xml new file mode 100644 index 0000000..a8f14da --- /dev/null +++ b/opsli-plugins/opsli-plugins-ehcache/pom.xml @@ -0,0 +1,34 @@ + + + + opsli-plugins + org.opsliframework.boot + 1.0.0 + ../pom.xml + + + 4.0.0 + opsli-plugins-ehcache + ${project.parent.version} + + + + + org.springframework.boot + spring-boot-starter-cache + + + javax.cache + cache-api + + + org.ehcache + ehcache + ${ehcache.version} + + + + + \ No newline at end of file diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java new file mode 100644 index 0000000..5866631 --- /dev/null +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java @@ -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 get(String cacheName, String key ,Class vClass); + + + /** + * 删除缓存数据 + * @param cacheName + * @param key + */ + boolean delete(String cacheName, String key); + +} diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/conf/EhCacheConfig.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/conf/EhCacheConfig.java new file mode 100644 index 0000000..e283fd8 --- /dev/null +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/conf/EhCacheConfig.java @@ -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 { + + +} \ No newline at end of file diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java new file mode 100644 index 0000000..0c56813 --- /dev/null +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java @@ -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 get(String cacheName, String key, Class 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; + } + +} diff --git a/opsli-plugins/pom.xml b/opsli-plugins/pom.xml index 28fc6fb..d1ed3eb 100644 --- a/opsli-plugins/pom.xml +++ b/opsli-plugins/pom.xml @@ -18,6 +18,7 @@ opsli-plugins-mail opsli-plugins-redis + opsli-plugins-ehcache