Commit e83cfd26 by stylefeng

整理pom

整合缓存
parent 41a20811
...@@ -15,34 +15,59 @@ ...@@ -15,34 +15,59 @@
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<dependencies> <dependencies>
<!--必选依赖-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.apache.commons</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>commons-lang3</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<!--web-->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>provided</scope>
</dependency>
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId> <groupId>com.google.zxing</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!--缓存-->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.ehcache</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>ehcache</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-cache</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
/**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stylefeng.guns.core.cache;
/**
* 缓存工厂基类
*/
public abstract class BaseCacheFactory implements ICache {
@SuppressWarnings("unchecked")
public <T> T get(String cacheName, Object key, ILoader iLoader) {
Object data = get(cacheName, key);
if (data == null) {
data = iLoader.load();
put(cacheName, key, data);
}
return (T) data;
}
@SuppressWarnings("unchecked")
public <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass) {
Object data = get(cacheName, key);
if (data == null) {
try {
ILoader dataLoader = iLoaderClass.newInstance();
data = dataLoader.load();
put(cacheName, key, data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return (T) data;
}
}
/**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stylefeng.guns.core.cache;
import java.util.List;
/**
* 缓存工具类
*/
public class CacheKit {
private static ICache defaultCacheFactory = new EhcacheFactory();
public static void put(String cacheName, Object key, Object value) {
defaultCacheFactory.put(cacheName, key, value);
}
public static <T> T get(String cacheName, Object key) {
return defaultCacheFactory.get(cacheName, key);
}
@SuppressWarnings("rawtypes")
public static List getKeys(String cacheName) {
return defaultCacheFactory.getKeys(cacheName);
}
public static void remove(String cacheName, Object key) {
defaultCacheFactory.remove(cacheName, key);
}
public static void removeAll(String cacheName) {
defaultCacheFactory.removeAll(cacheName);
}
public static <T> T get(String cacheName, Object key, ILoader iLoader) {
return defaultCacheFactory.get(cacheName, key, iLoader);
}
public static <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass) {
return defaultCacheFactory.get(cacheName, key, iLoaderClass);
}
}
/**
* Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stylefeng.guns.core.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Ehcache缓存工厂
*/
public class EhcacheFactory extends BaseCacheFactory {
private static CacheManager cacheManager;
private static volatile Object locker = new Object();
private static final Logger log = LoggerFactory.getLogger(EhcacheFactory.class);
private static CacheManager getCacheManager() {
if (cacheManager == null) {
synchronized (EhcacheFactory.class) {
if (cacheManager == null) {
cacheManager = CacheManager.create();
}
}
}
return cacheManager;
}
static Cache getOrAddCache(String cacheName) {
CacheManager cacheManager = getCacheManager();
Cache cache = cacheManager.getCache(cacheName);
if (cache == null) {
synchronized(locker) {
cache = cacheManager.getCache(cacheName);
if (cache == null) {
log.warn("无法找到缓存 [" + cacheName + "]的配置, 使用默认配置.");
cacheManager.addCacheIfAbsent(cacheName);
cache = cacheManager.getCache(cacheName);
log.debug("缓存 [" + cacheName + "] 启动.");
}
}
}
return cache;
}
public void put(String cacheName, Object key, Object value) {
getOrAddCache(cacheName).put(new Element(key, value));
}
@SuppressWarnings("unchecked")
public <T> T get(String cacheName, Object key) {
Element element = getOrAddCache(cacheName).get(key);
return element != null ? (T)element.getObjectValue() : null;
}
@SuppressWarnings("rawtypes")
public List getKeys(String cacheName) {
return getOrAddCache(cacheName).getKeys();
}
public void remove(String cacheName, Object key) {
getOrAddCache(cacheName).remove(key);
}
public void removeAll(String cacheName) {
getOrAddCache(cacheName).removeAll();
}
}
/**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stylefeng.guns.core.cache;
import java.util.List;
/**
* 通用缓存接口
*/
public interface ICache {
void put(String cacheName, Object key, Object value);
<T> T get(String cacheName, Object key);
@SuppressWarnings("rawtypes")
List getKeys(String cacheName);
void remove(String cacheName, Object key);
void removeAll(String cacheName);
<T> T get(String cacheName, Object key, ILoader iLoader);
<T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass);
}
/**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stylefeng.guns.core.cache;
/**
* 数据重载
*/
public interface ILoader {
Object load();
}
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
<beetl.version>2.7.15</beetl.version> <beetl.version>2.7.15</beetl.version>
<swagger.version>2.2.2</swagger.version> <swagger.version>2.2.2</swagger.version>
<commons-lang3.version>3.6</commons-lang3.version> <commons-lang3.version>3.6</commons-lang3.version>
<ehcache.core.version>2.6.11</ehcache.core.version>
<mysql-connector-java.version>5.1.38</mysql-connector-java.version> <mysql-connector-java.version>5.1.38</mysql-connector-java.version>
</properties> </properties>
...@@ -130,6 +131,11 @@ ...@@ -130,6 +131,11 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcache.core.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment