Skip to content

Commit

Permalink
Merge pull request #115 from yg3630536/feature/add-cache
Browse files Browse the repository at this point in the history
* Add cache code
  • Loading branch information
chenhaozx committed Jan 21, 2020
2 parents 45cc7ea + cdc6e0f commit 534b785
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 10 deletions.
12 changes: 8 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ List protobuf = [
"com.google.protobuf:protobuf-java:3.9.1"
]

List caffeine = [
"com.github.ben-manes.caffeine:caffeine:2.8.0"
]

configurations {
localDeps
}
Expand All @@ -123,12 +127,12 @@ dependencies {
localDeps 'org.projectlombok:lombok:1.18.10'
if (gradleVer.startsWith("4")) {
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile("com.webank:weid-contract-java:1.2.12") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
} else {
compile fileTree(dir: 'dist/lib', include: '*.jar')
}
Expand All @@ -139,12 +143,12 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.10'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile("com.webank:weid-contract-java:1.2.12") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
} else {
compileOnly files('dist/lib/lombok-1.18.10.jar')
annotationProcessor files('dist/lib/lombok-1.18.10.jar')
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/webank/weid/service/impl/CptServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.webank.weid.service.BaseService;
import com.webank.weid.service.impl.engine.CptServiceEngine;
import com.webank.weid.service.impl.engine.EngineFactory;
import com.webank.weid.suite.cache.CacheManager;
import com.webank.weid.suite.cache.CacheNode;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.WeIdUtils;

Expand All @@ -57,6 +59,10 @@ public class CptServiceImpl extends BaseService implements CptService {

private CptServiceEngine cptServiceEngine = EngineFactory.createCptServiceEngine();

//获取CPT缓存节点
private static CacheNode<ResponseData<Cpt>> cptCahceNode =
CacheManager.registerCacheNode("SYS_CPT", 1000 * 3600 * 24L);

/**
* Register a new CPT with a pre-set CPT ID, to the blockchain.
*
Expand Down Expand Up @@ -200,8 +206,15 @@ public ResponseData<Cpt> queryCpt(Integer cptId) {
if (cptId == null || cptId < 0) {
return new ResponseData<>(null, ErrorCode.CPT_ID_ILLEGAL);
}

return cptServiceEngine.queryCpt(cptId);
String cptIdStr = String.valueOf(cptId);
ResponseData<Cpt> result = cptCahceNode.get(cptIdStr);
if (result == null) {
result = cptServiceEngine.queryCpt(cptId);
if (result.getErrorCode().intValue() == ErrorCode.SUCCESS.getCode()) {
cptCahceNode.put(cptIdStr, result);
}
}
return result;
} catch (Exception e) {
logger.error("[updateCpt] query cpt failed due to unknown error. ", e);
return new ResponseData<>(null, ErrorCode.UNKNOW_ERROR);
Expand Down Expand Up @@ -268,8 +281,12 @@ public ResponseData<CptBaseInfo> updateCpt(CptMapArgs args, Integer cptId) {
cptJsonSchemaNew,
weIdPrivateKey);
String address = WeIdUtils.convertWeIdToAddress(weId);
return cptServiceEngine.updateCpt(cptId, address, cptJsonSchemaNew, rsvSignature,
weIdPrivateKey.getPrivateKey());
ResponseData<CptBaseInfo> result = cptServiceEngine.updateCpt(cptId, address,
cptJsonSchemaNew, rsvSignature, weIdPrivateKey.getPrivateKey());
if (result.getErrorCode().intValue() == ErrorCode.SUCCESS.getCode()) {
cptCahceNode.remove(String.valueOf(cptId));
}
return result;
} catch (Exception e) {
logger.error("[updateCpt] update cpt failed due to unkown error. ", e);
return new ResponseData<>(null, ErrorCode.UNKNOW_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public class CredentialPojoServiceImpl extends BaseService implements Credential
private static CptService cptService = new CptServiceImpl();
private static Persistence dataDriver = new MysqlDriver();


/**
* Salt generator. Automatically fillin the map structure in a recursive manner.
*
Expand Down Expand Up @@ -622,7 +621,7 @@ private static ErrorCode verifyCptFormat(Integer cptId, Map<String, Object> clai
if (cpt == null) {
logger.error(ErrorCode.CREDENTIAL_CPT_NOT_EXISTS.getCodeDesc());
return ErrorCode.CREDENTIAL_CPT_NOT_EXISTS;
}
}
//String cptJsonSchema = JsonUtil.objToJsonStr(cpt.getCptJsonSchema());
String cptJsonSchema = DataToolUtils.serialize(cpt.getCptJsonSchema());

Expand Down
114 changes: 114 additions & 0 deletions src/main/java/com/webank/weid/suite/cache/CacheManager.java
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;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/webank/weid/suite/cache/CacheNode.java
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;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/weidentity.properties.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ domain.masterKey=datasource1:master_secret

domain.credentialSignature=datasource1:credential_signature

# You can configure the maximumSize of the default cache module through caffeineCache.maximumSize.xxx.
caffeineCache.maximumSize.SYS_CPT=100

# Salt length for Proof creation.
salt.length=5

Expand Down
105 changes: 105 additions & 0 deletions src/test/java/com/webank/weid/full/cache/TestCacheManager.java
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);
}
}

0 comments on commit 534b785

Please sign in to comment.