|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.jzg.quotation.summary.service;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+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.redisson.api.RBucket;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 协议信息 Redis 缓存服务
|
|
|
+ * <p>
|
|
|
+ * 用于降低批量报价时对 organization 服务的并发压力。
|
|
|
+ * 多实例部署时缓存共享。
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AgreementCacheService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrgClient orgClient;
|
|
|
+
|
|
|
+ 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获取协议(带Redis缓存)
|
|
|
+ */
|
|
|
+ public PtlAgreement getByAgreementId(String agreementId) {
|
|
|
+ if (agreementId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String key = AGREEMENT_ID_KEY_PREFIX + agreementId;
|
|
|
+ try {
|
|
|
+ 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;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("getByAgreementId from redis cache failed, agreementId={}", agreementId, e);
|
|
|
+ return orgClient.getByAgreementId(agreementId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据保险公司ID获取协议列表(带Redis缓存)
|
|
|
+ */
|
|
|
+ public List<PtlAgreement> getByCompanyId(String companyId, String systemCode) {
|
|
|
+ if (companyId == null || systemCode == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String key = AGREEMENT_COMPANY_KEY_PREFIX + companyId + ":" + systemCode;
|
|
|
+ try {
|
|
|
+ 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;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("getByCompanyId from redis cache failed, companyId={}, systemCode={}", companyId, systemCode, e);
|
|
|
+ HttpResult<List<PtlAgreement>> result = orgClient.getByCompanyId(companyId, systemCode);
|
|
|
+ return result != null ? result.getData() : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|