-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from yg3630536/feature/add-cache
* Add cache code
- Loading branch information
Showing
7 changed files
with
308 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/com/webank/weid/suite/cache/CacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright© (2018-2020) WeBank Co., Ltd. | ||
* | ||
* This file is part of weid-java-sdk. | ||
* | ||
* weid-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weid-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.suite.cache; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.webank.weid.exception.WeIdBaseException; | ||
import com.webank.weid.util.PropertyUtils; | ||
|
||
|
||
/** | ||
* 缓存管理器. | ||
* @author v_wbgyang | ||
* | ||
*/ | ||
public class CacheManager { | ||
|
||
//全局缓存上下文 | ||
private static final ConcurrentHashMap<String, CacheNode<Object>> context = | ||
new ConcurrentHashMap<String, CacheNode<Object>>(); | ||
|
||
//默认缓存个数 | ||
private static final Integer MAX_SIZE = 1000; | ||
private static final String CACHE_MAXSIZE_KEY = "caffeineCache.maximumSize."; | ||
|
||
/** | ||
* 根据缓存名获取缓存节点最大缓存个数,如果没有配置则使用默认大小配置. | ||
* @param cacheKey 缓存名 | ||
* @return | ||
*/ | ||
private static Integer getMaxSize(String cacheName) { | ||
String maximumSize = PropertyUtils.getProperty(CACHE_MAXSIZE_KEY + cacheName); | ||
if (StringUtils.isNotBlank(maximumSize)) { | ||
return Integer.parseInt(maximumSize); | ||
} | ||
return MAX_SIZE; | ||
} | ||
|
||
/** | ||
* 注册缓存节点,如果存在则直接返回,不存在则注册. | ||
* @param <T> 需要存放的数据类型 | ||
* @param cacheName 缓存名 | ||
* @param timeout 超时时间 | ||
* @return 返回缓存节点 | ||
*/ | ||
public static <T> CacheNode<T> registerCacheNode(String cacheName, Long timeout) { | ||
return registerCacheNode(cacheName, timeout, getMaxSize(cacheName)); | ||
} | ||
|
||
/** | ||
* 注册缓存节点,如果存在则直接返回,不存在则注册. | ||
* @param <T> 需要存放的数据类型 | ||
* @param cacheName 缓存名 | ||
* @param timeout 超时时间 | ||
* @param maximumSize 最大缓存大小 | ||
* @return 返回缓存节点 | ||
*/ | ||
public static <T> CacheNode<T> registerCacheNode( | ||
String cacheName, | ||
Long timeout, | ||
Integer maximumSize) { | ||
|
||
CacheNode<Object> cacheNode = context.get(cacheName); | ||
if (cacheNode != null) { | ||
throw new WeIdBaseException("the cacheName is registed, cacheName= " + cacheName); | ||
} | ||
cacheNode = initCache(cacheName, timeout, maximumSize); | ||
CacheNode<T> node = (CacheNode<T>)cacheNode; | ||
return node; | ||
} | ||
|
||
/** | ||
* 根据缓存名和超时时间初始化缓存模块. | ||
* @param cacheName 缓存名 | ||
* @param timeout 超时时间 | ||
* @param maximumSize 缓存项大小 | ||
* @return 返回缓存节点对象 | ||
*/ | ||
private static synchronized CacheNode<Object> initCache( | ||
String cacheName, | ||
Long timeout, | ||
Integer maximumSize) { | ||
|
||
Cache<String, Object> cache = Caffeine.newBuilder() | ||
.expireAfterWrite(timeout, TimeUnit.MILLISECONDS) | ||
.maximumSize(maximumSize) | ||
.build(); | ||
CacheNode<Object> node = new CacheNode<>(cacheName, cache); | ||
context.put(cacheName, node); | ||
return node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright© (2018-2020) WeBank Co., Ltd. | ||
* | ||
* This file is part of weid-java-sdk. | ||
* | ||
* weid-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weid-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.suite.cache; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
|
||
/** | ||
* 缓存节点. | ||
* @author v_wbgyang | ||
* | ||
* @param <T> 节点存放的对象泛型 | ||
*/ | ||
public class CacheNode<T> { | ||
|
||
private Cache<String, T> cache; | ||
|
||
private String cacheName; | ||
|
||
CacheNode(String cacheName, Cache<String, T> cache) { | ||
this.cacheName = cacheName; | ||
this.cache = cache; | ||
} | ||
|
||
public void put(String key, T t) { | ||
cache.put(key, t); | ||
} | ||
|
||
public T get(String key) { | ||
return cache.getIfPresent(key); | ||
} | ||
|
||
public void remove(String key) { | ||
cache.invalidate(key); | ||
} | ||
|
||
public String getCacheName() { | ||
return cacheName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/com/webank/weid/full/cache/TestCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright© (2018-2020) WeBank Co., Ltd. | ||
* | ||
* This file is part of weid-java-sdk. | ||
* | ||
* weid-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weid-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.full.cache; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.webank.weid.exception.WeIdBaseException; | ||
import com.webank.weid.protocol.cpt.Cpt103; | ||
import com.webank.weid.suite.cache.CacheManager; | ||
import com.webank.weid.suite.cache.CacheNode; | ||
|
||
public class TestCacheManager { | ||
|
||
@Test | ||
public void testPutAndGetString() { | ||
CacheNode<String> cacheNode = CacheManager.registerCacheNode("Test", 10000L, 100); | ||
String value = "abc"; | ||
cacheNode.put("string", value); | ||
String getValue = cacheNode.get("string"); | ||
Assert.assertEquals(value, getValue); | ||
|
||
String newValue = "newAbc"; | ||
cacheNode.put("string", newValue); | ||
|
||
String newGetValue = cacheNode.get("string"); | ||
Assert.assertEquals(newValue, newGetValue); | ||
} | ||
|
||
@Test | ||
public void testRegisterAgain() { | ||
boolean result = false; | ||
try { | ||
CacheManager.registerCacheNode("Test1", 10000L, 100); | ||
CacheManager.registerCacheNode("Test1", 10000L, 100); | ||
result = true; | ||
} catch (WeIdBaseException e) { | ||
result = false; | ||
} | ||
|
||
Assert.assertFalse(result); | ||
} | ||
|
||
@Test | ||
public void testTimeout() throws InterruptedException { | ||
CacheNode<String> cacheNode = CacheManager.registerCacheNode("TestTimeout", 1000L, 100); | ||
String value = "test"; | ||
cacheNode.put("timeout", value); | ||
String getValue = cacheNode.get("timeout"); | ||
Assert.assertEquals(value, getValue); | ||
Thread.sleep(1500); | ||
getValue = cacheNode.get("timeout"); | ||
Assert.assertNull(getValue); | ||
} | ||
|
||
@Test | ||
public void testPutAndGetObject() { | ||
CacheNode<Cpt103> cacheNode = CacheManager.registerCacheNode("obj", 10000L, 100); | ||
Cpt103 cpt = new Cpt103(); | ||
cpt.setId("123456789"); | ||
cacheNode.put("cpt103", cpt); | ||
Cpt103 getObj = cacheNode.get("cpt103"); | ||
Assert.assertTrue(cpt.equals(getObj)); | ||
Assert.assertEquals(cpt.getId(), getObj.getId()); | ||
} | ||
|
||
@Test | ||
public void testMaxNum() throws InterruptedException { | ||
CacheNode<String> cacheNode = CacheManager.registerCacheNode("TestMaxSize", 10000L, 10); | ||
//初始化10个值 | ||
for (int i = 0; i < 10; i++) { | ||
cacheNode.put("key" + i, "value" + i); | ||
} | ||
String value = cacheNode.get("key0"); | ||
Assert.assertEquals("value0", value); | ||
//再次加入一个值 | ||
cacheNode.put("key" + 10, "value" + 10); | ||
//给清除机制缓冲时间 | ||
Thread.sleep(1000); | ||
int count = 0; | ||
for (int i = 0; i <= 10; i++) { | ||
if (cacheNode.get("key" + i) != null) { | ||
count++; | ||
} | ||
} | ||
Assert.assertEquals(10, count); | ||
} | ||
} |