|
|
@@ -0,0 +1,105 @@
|
|
|
+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 com.jzg.commons.core.page.HttpResult;
|
|
|
+import com.jzg.commons.entity.po.PtlAgreement;
|
|
|
+import com.jzg.quotation.commons.client.OrgClient;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 协议信息本地缓存服务
|
|
|
+ * <p>
|
|
|
+ * 用于降低批量报价时对 organization 服务的并发压力。
|
|
|
+ * 缓存 key:
|
|
|
+ * - agreementById: agreementId
|
|
|
+ * - agreementsByCompany: companyId + "#" + systemCode
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AgreementCacheService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrgClient orgClient;
|
|
|
+
|
|
|
+ private LoadingCache<String, PtlAgreement> agreementByIdCache;
|
|
|
+ private LoadingCache<String, List<PtlAgreement>> agreementsByCompanyCache;
|
|
|
+
|
|
|
+ @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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据协议ID获取协议(带本地缓存)
|
|
|
+ */
|
|
|
+ public PtlAgreement getByAgreementId(String agreementId) {
|
|
|
+ if (agreementId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ PtlAgreement agreement = agreementByIdCache.get(agreementId);
|
|
|
+ logStats(agreementByIdCache, "agreementByIdCache");
|
|
|
+ return agreement;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("get agreement by id from cache failed, agreementId={}", agreementId, e);
|
|
|
+ return orgClient.getByAgreementId(agreementId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据保险公司ID获取协议列表(带本地缓存)
|
|
|
+ */
|
|
|
+ public List<PtlAgreement> getByCompanyId(String companyId, String systemCode) {
|
|
|
+ if (companyId == null || systemCode == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<PtlAgreement> agreements = agreementsByCompanyCache.get(companyId + "#" + systemCode);
|
|
|
+ logStats(agreementsByCompanyCache, "agreementsByCompanyCache");
|
|
|
+ return agreements;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("get agreements by company from cache failed, companyId={}, systemCode={}", companyId, systemCode, e);
|
|
|
+ HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(companyId, systemCode);
|
|
|
+ return result != null ? result.getData() : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void logStats(LoadingCache<?, ?> cache, String cacheName) {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("{} stats: {}", cacheName, cache.stats());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|