|
@@ -1,26 +1,23 @@
|
|
|
package com.jzg.quotation.summary.service;
|
|
package com.jzg.quotation.summary.service;
|
|
|
|
|
|
|
|
-import com.google.common.cache.CacheBuilder;
|
|
|
|
|
-import com.google.common.cache.CacheLoader;
|
|
|
|
|
-import com.google.common.cache.LoadingCache;
|
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.jzg.commons.core.page.HttpResult;
|
|
import com.jzg.commons.core.page.HttpResult;
|
|
|
import com.jzg.commons.entity.po.PtlAgreement;
|
|
import com.jzg.commons.entity.po.PtlAgreement;
|
|
|
import com.jzg.quotation.commons.client.OrgClient;
|
|
import com.jzg.quotation.commons.client.OrgClient;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.redisson.api.RBucket;
|
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 协议信息本地缓存服务
|
|
|
|
|
|
|
+ * 协议信息 Redis 缓存服务
|
|
|
* <p>
|
|
* <p>
|
|
|
* 用于降低批量报价时对 organization 服务的并发压力。
|
|
* 用于降低批量报价时对 organization 服务的并发压力。
|
|
|
- * 缓存 key:
|
|
|
|
|
- * - agreementById: agreementId
|
|
|
|
|
- * - agreementsByCompany: companyId + "#" + systemCode
|
|
|
|
|
|
|
+ * 多实例部署时缓存共享。
|
|
|
* </p>
|
|
* </p>
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -28,78 +25,69 @@ import java.util.concurrent.TimeUnit;
|
|
|
public class AgreementCacheService {
|
|
public class AgreementCacheService {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private OrgClient orgClient;
|
|
|
|
|
-
|
|
|
|
|
- private LoadingCache<String, PtlAgreement> agreementByIdCache;
|
|
|
|
|
- private LoadingCache<String, List<PtlAgreement>> agreementsByCompanyCache;
|
|
|
|
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
|
|
|
|
- @PostConstruct
|
|
|
|
|
- public void init() {
|
|
|
|
|
- agreementByIdCache = CacheBuilder.newBuilder()
|
|
|
|
|
- .maximumSize(5000)
|
|
|
|
|
- .expireAfterWrite(10, TimeUnit.MINUTES)
|
|
|
|
|
- .recordStats()
|
|
|
|
|
- .build(new CacheLoader<String, PtlAgreement>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public PtlAgreement load(String agreementId) {
|
|
|
|
|
- log.debug("agreementByIdCache miss, agreementId={}", agreementId);
|
|
|
|
|
- return orgClient.getByAgreementId(agreementId);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private OrgClient orgClient;
|
|
|
|
|
|
|
|
- agreementsByCompanyCache = CacheBuilder.newBuilder()
|
|
|
|
|
- .maximumSize(2000)
|
|
|
|
|
- .expireAfterWrite(10, TimeUnit.MINUTES)
|
|
|
|
|
- .recordStats()
|
|
|
|
|
- .build(new CacheLoader<String, List<PtlAgreement>>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public List<PtlAgreement> load(String key) {
|
|
|
|
|
- String[] parts = key.split("#", 2);
|
|
|
|
|
- log.debug("agreementsByCompanyCache miss, companyId={}, systemCode={}", parts[0], parts[1]);
|
|
|
|
|
- HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(parts[0], parts[1]);
|
|
|
|
|
- return result != null ? result.getData() : null;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private static final String AGREEMENT_ID_KEY_PREFIX = "quotation:agreement:id:";
|
|
|
|
|
+ private static final String AGREEMENT_COMPANY_KEY_PREFIX = "quotation:agreement:company:";
|
|
|
|
|
+ private static final long CACHE_TTL = 10;
|
|
|
|
|
+ private static final TimeUnit CACHE_TTL_UNIT = TimeUnit.MINUTES;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据协议ID获取协议(带本地缓存)
|
|
|
|
|
|
|
+ * 根据协议ID获取协议(带Redis缓存)
|
|
|
*/
|
|
*/
|
|
|
public PtlAgreement getByAgreementId(String agreementId) {
|
|
public PtlAgreement getByAgreementId(String agreementId) {
|
|
|
if (agreementId == null) {
|
|
if (agreementId == null) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
+ String key = AGREEMENT_ID_KEY_PREFIX + agreementId;
|
|
|
try {
|
|
try {
|
|
|
- PtlAgreement agreement = agreementByIdCache.get(agreementId);
|
|
|
|
|
- logStats(agreementByIdCache, "agreementByIdCache");
|
|
|
|
|
|
|
+ RBucket<String> bucket = redissonClient.getBucket(key);
|
|
|
|
|
+ String cached = bucket.get();
|
|
|
|
|
+ if (cached != null) {
|
|
|
|
|
+ log.debug("getByAgreementId cache hit, agreementId={}", agreementId);
|
|
|
|
|
+ return JSONUtil.toBean(cached, PtlAgreement.class);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.debug("getByAgreementId cache miss, agreementId={}", agreementId);
|
|
|
|
|
+ PtlAgreement agreement = orgClient.getByAgreementId(agreementId);
|
|
|
|
|
+ if (agreement != null) {
|
|
|
|
|
+ bucket.set(JSONUtil.toJsonStr(agreement), CACHE_TTL, CACHE_TTL_UNIT);
|
|
|
|
|
+ }
|
|
|
return agreement;
|
|
return agreement;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("get agreement by id from cache failed, agreementId={}", agreementId, e);
|
|
|
|
|
|
|
+ log.error("getByAgreementId from redis cache failed, agreementId={}", agreementId, e);
|
|
|
return orgClient.getByAgreementId(agreementId);
|
|
return orgClient.getByAgreementId(agreementId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据保险公司ID获取协议列表(带本地缓存)
|
|
|
|
|
|
|
+ * 根据保险公司ID获取协议列表(带Redis缓存)
|
|
|
*/
|
|
*/
|
|
|
public List<PtlAgreement> getByCompanyId(String companyId, String systemCode) {
|
|
public List<PtlAgreement> getByCompanyId(String companyId, String systemCode) {
|
|
|
if (companyId == null || systemCode == null) {
|
|
if (companyId == null || systemCode == null) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
+ String key = AGREEMENT_COMPANY_KEY_PREFIX + companyId + ":" + systemCode;
|
|
|
try {
|
|
try {
|
|
|
- List<PtlAgreement> agreements = agreementsByCompanyCache.get(companyId + "#" + systemCode);
|
|
|
|
|
- logStats(agreementsByCompanyCache, "agreementsByCompanyCache");
|
|
|
|
|
|
|
+ RBucket<String> bucket = redissonClient.getBucket(key);
|
|
|
|
|
+ String cached = bucket.get();
|
|
|
|
|
+ if (cached != null) {
|
|
|
|
|
+ log.debug("getByCompanyId cache hit, companyId={}, systemCode={}", companyId, systemCode);
|
|
|
|
|
+ return JSONUtil.toList(cached, PtlAgreement.class);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.debug("getByCompanyId cache miss, companyId={}, systemCode={}", companyId, systemCode);
|
|
|
|
|
+ HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(companyId, systemCode);
|
|
|
|
|
+ List<PtlAgreement> agreements = result != null ? result.getData() : null;
|
|
|
|
|
+ if (agreements != null) {
|
|
|
|
|
+ bucket.set(JSONUtil.toJsonStr(agreements), CACHE_TTL, CACHE_TTL_UNIT);
|
|
|
|
|
+ }
|
|
|
return agreements;
|
|
return agreements;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("get agreements by company from cache failed, companyId={}, systemCode={}", companyId, systemCode, e);
|
|
|
|
|
|
|
+ log.error("getByCompanyId from redis cache failed, companyId={}, systemCode={}", companyId, systemCode, e);
|
|
|
HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(companyId, systemCode);
|
|
HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(companyId, systemCode);
|
|
|
return result != null ? result.getData() : null;
|
|
return result != null ? result.getData() : null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private void logStats(LoadingCache<?, ?> cache, String cacheName) {
|
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
|
|
- log.debug("{} stats: {}", cacheName, cache.stats());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|