|
@@ -8,10 +8,15 @@ import com.fs.company.domain.CompanyConfig;
|
|
import com.fs.company.mapper.CompanyConfigMapper;
|
|
import com.fs.company.mapper.CompanyConfigMapper;
|
|
import com.fs.company.service.ICompanyConfigService;
|
|
import com.fs.company.service.ICompanyConfigService;
|
|
import com.fs.system.domain.SysConfig;
|
|
import com.fs.system.domain.SysConfig;
|
|
|
|
+import org.apache.http.util.Asserts;
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static com.fs.common.utils.DictUtils.getCacheKey;
|
|
import static com.fs.common.utils.DictUtils.getCacheKey;
|
|
|
|
|
|
@@ -28,6 +33,10 @@ public class CompanyConfigServiceImpl implements ICompanyConfigService
|
|
private RedisCache redisCache;
|
|
private RedisCache redisCache;
|
|
@Autowired
|
|
@Autowired
|
|
private CompanyConfigMapper companyConfigMapper;
|
|
private CompanyConfigMapper companyConfigMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate; // 注入RedisTemplate
|
|
|
|
+ private static final String REDIS_KEY_PREFIX = "red_packet_config:";
|
|
|
|
+ private static final long CACHE_TIMEOUT = 24 * 60 * 60;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 查询参数配置
|
|
* 查询参数配置
|
|
@@ -133,4 +142,33 @@ public class CompanyConfigServiceImpl implements ICompanyConfigService
|
|
public CompanyConfig selectCompanyConfigByServerKey(String key) {
|
|
public CompanyConfig selectCompanyConfigByServerKey(String key) {
|
|
return companyConfigMapper.selectCompanyConfigByServerKey(key);
|
|
return companyConfigMapper.selectCompanyConfigByServerKey(key);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String selectRedPacketConfigByKey(Long companyId) {
|
|
|
|
+ Asserts.notNull(companyId,"公司id不能为空!");
|
|
|
|
+ String redisKey = REDIS_KEY_PREFIX + companyId;
|
|
|
|
+ String cachedConfig = redisTemplate.opsForValue().get(redisKey);
|
|
|
|
+ if (cachedConfig != null) {
|
|
|
|
+ return cachedConfig;
|
|
|
|
+ }
|
|
|
|
+ synchronized (getSynchronizationObject(companyId)) {
|
|
|
|
+ cachedConfig = redisTemplate.opsForValue().get(redisKey);
|
|
|
|
+
|
|
|
|
+ if (cachedConfig != null) {
|
|
|
|
+ return cachedConfig;
|
|
|
|
+ }
|
|
|
|
+ String configFromDb = companyConfigMapper.selectRedPacketConfigByKey(companyId);
|
|
|
|
+ if (configFromDb != null) {
|
|
|
|
+ redisTemplate.opsForValue().set(redisKey, configFromDb, CACHE_TIMEOUT, TimeUnit.SECONDS);
|
|
|
|
+ } else {
|
|
|
|
+ redisTemplate.opsForValue().set(redisKey, "", 5 * 60, TimeUnit.SECONDS);
|
|
|
|
+ }
|
|
|
|
+ return configFromDb;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static final ConcurrentHashMap<Long, Object> LOCKS = new ConcurrentHashMap<>();
|
|
|
|
+ private static Object getSynchronizationObject(Long companyId) {
|
|
|
|
+ return LOCKS.computeIfAbsent(companyId, k -> new Object());
|
|
|
|
+ }
|
|
}
|
|
}
|