V getFromMap(String key, K field);
+
+ /**
+ * 将指定的值添加到有序集合中
+ *
+ * @param key 键
+ * @param value 值
+ */
+ void addToSortedSet(String key, String value);
+
+ /**
+ * 获取 Redis 锁(可重入锁)
+ *
+ * @param key 键
+ * @return Lock
+ */
+ RLock getLock(String key);
+
+ /**
+ * 获取 Redis 锁(公平锁)
+ *
+ * @param key 键
+ * @return Lock
+ */
+ RLock getFairLock(String key);
+
+ /**
+ * 获取 Redis 锁(读写锁)
+ *
+ * @param key 键
+ * @return RReadWriteLock
+ */
+ RReadWriteLock getReadWriteLock(String key);
+
+ /**
+ * 获取 Redis 信号量
+ *
+ * @param key 键
+ * @return RSemaphore
+ */
+ RSemaphore getSemaphore(String key);
+
+ /**
+ * 获取 Redis 过期信号量
+ *
+ * 基于Redis的Redisson的分布式信号量(Semaphore)Java对象RSemaphore采用了与java.util.concurrent.Semaphore相似的接口和用法。
+ * 同时还提供了异步(Async)、反射式(Reactive)和RxJava2标准的接口。
+ *
+ * @param key 键
+ * @return RPermitExpirableSemaphore
+ */
+ RPermitExpirableSemaphore getPermitExpirableSemaphore(String key);
+
+ /**
+ * 闭锁
+ *
+ * @param key 键
+ * @return RCountDownLatch
+ */
+ RCountDownLatch getCountDownLatch(String key);
+
+ /**
+ * 布隆过滤器
+ *
+ * @param key 键
+ * @param 存放对象
+ * @return 返回结果
+ */
+ RBloomFilter getBloomFilter(String key);
+
+}
diff --git a/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/redis/RedissonService.java b/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/redis/RedissonService.java
new file mode 100644
index 0000000..f1a31ca
--- /dev/null
+++ b/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/redis/RedissonService.java
@@ -0,0 +1,161 @@
+package cn.bugstack.infrastructure.persistent.redis;
+
+import org.redisson.api.*;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.time.Duration;
+
+/**
+ * @ClassName: RedissonService
+ * @Description:
+ * @Author: zhaoyongfeng
+ * @Date: 2024/11/14 17:49
+ */
+@Service("redissonService")
+public class RedissonService implements IRedisService {
+
+ @Resource
+ private RedissonClient redissonClient;
+
+ public void setValue(String key, T value) {
+ redissonClient.getBucket(key).set(value);
+ }
+
+ @Override
+ public void setValue(String key, T value, long expired) {
+ RBucket bucket = redissonClient.getBucket(key);
+ bucket.set(value, Duration.ofMillis(expired));
+ }
+
+ public T getValue(String key) {
+ return redissonClient.getBucket(key).get();
+ }
+
+ @Override
+ public RQueue getQueue(String key) {
+ return redissonClient.getQueue(key);
+ }
+
+ @Override
+ public RBlockingQueue getBlockingQueue(String key) {
+ return redissonClient.getBlockingQueue(key);
+ }
+
+ @Override
+ public RDelayedQueue getDelayedQueue(RBlockingQueue rBlockingQueue) {
+ return redissonClient.getDelayedQueue(rBlockingQueue);
+ }
+
+ @Override
+ public long incr(String key) {
+ return redissonClient.getAtomicLong(key).incrementAndGet();
+ }
+
+ @Override
+ public long incrBy(String key, long delta) {
+ return redissonClient.getAtomicLong(key).addAndGet(delta);
+ }
+
+ @Override
+ public long decr(String key) {
+ return redissonClient.getAtomicLong(key).decrementAndGet();
+ }
+
+ @Override
+ public long decrBy(String key, long delta) {
+ return redissonClient.getAtomicLong(key).addAndGet(-delta);
+ }
+
+ @Override
+ public void remove(String key) {
+ redissonClient.getBucket(key).delete();
+ }
+
+ @Override
+ public boolean isExists(String key) {
+ return redissonClient.getBucket(key).isExists();
+ }
+
+ public void addToSet(String key, String value) {
+ RSet set = redissonClient.getSet(key);
+ set.add(value);
+ }
+
+ public boolean isSetMember(String key, String value) {
+ RSet set = redissonClient.getSet(key);
+ return set.contains(value);
+ }
+
+ public void addToList(String key, String value) {
+ RList list = redissonClient.getList(key);
+ list.add(value);
+ }
+
+ public String getFromList(String key, int index) {
+ RList list = redissonClient.getList(key);
+ return list.get(index);
+ }
+
+ @Override
+ public RMap getMap(String key) {
+ return redissonClient.getMap(key);
+ }
+
+ public void addToMap(String key, String field, String value) {
+ RMap map = redissonClient.getMap(key);
+ map.put(field, value);
+ }
+
+ public String getFromMap(String key, String field) {
+ RMap map = redissonClient.getMap(key);
+ return map.get(field);
+ }
+
+ @Override
+ public V getFromMap(String key, K field) {
+ return redissonClient.getMap(key).get(field);
+ }
+
+ public void addToSortedSet(String key, String value) {
+ RSortedSet sortedSet = redissonClient.getSortedSet(key);
+ sortedSet.add(value);
+ }
+
+ @Override
+ public RLock getLock(String key) {
+ return redissonClient.getLock(key);
+ }
+
+ @Override
+ public RLock getFairLock(String key) {
+ return redissonClient.getFairLock(key);
+ }
+
+ @Override
+ public RReadWriteLock getReadWriteLock(String key) {
+ return redissonClient.getReadWriteLock(key);
+ }
+
+ @Override
+ public RSemaphore getSemaphore(String key) {
+ return redissonClient.getSemaphore(key);
+ }
+
+ @Override
+ public RPermitExpirableSemaphore getPermitExpirableSemaphore(String key) {
+ return redissonClient.getPermitExpirableSemaphore(key);
+ }
+
+ @Override
+ public RCountDownLatch getCountDownLatch(String key) {
+ return redissonClient.getCountDownLatch(key);
+ }
+
+ @Override
+ public RBloomFilter getBloomFilter(String key) {
+ return redissonClient.getBloomFilter(key);
+ }
+
+
+}
diff --git a/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/repository/StrategyRepository.java b/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/repository/StrategyRepository.java
new file mode 100644
index 0000000..5c2355f
--- /dev/null
+++ b/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/repository/StrategyRepository.java
@@ -0,0 +1,76 @@
+package cn.bugstack.infrastructure.persistent.repository;
+
+
+import cn.bugstack.domain.strategy.model.entity.StrategyAwardEntity;
+import cn.bugstack.domain.strategy.repository.IStrategyRepository;
+import cn.bugstack.infrastructure.persistent.dao.IStrategyAwardDao;
+import cn.bugstack.infrastructure.persistent.po.StrategyAward;
+import cn.bugstack.infrastructure.persistent.redis.IRedisService;
+import cn.bugstack.types.common.Constants;
+import org.springframework.stereotype.Repository;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * @ClassName: StrategyRepository
+ * @Description: 策略服务仓储实现
+ * @Author: zhaoyongfeng
+ * @Date: 2024/11/14 19:27
+ */
+@Repository
+public class StrategyRepository implements IStrategyRepository {
+
+ @Resource
+ private IStrategyAwardDao strategyAwardDao;
+ @Resource
+ private IRedisService redisService;
+
+ @Override
+ public List queryStrategyAwardList(Long strategyId) {
+ // 优先从缓存获取
+ String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
+ List strategyAwardEntities = redisService.getValue(cacheKey);
+ if (null != strategyAwardEntities && !strategyAwardEntities.isEmpty()) return strategyAwardEntities;
+ // 从库中获取数据
+ List strategyAwards = strategyAwardDao.queryStrategyAwardListByStrategyId(strategyId);
+ strategyAwardEntities = new ArrayList<>(strategyAwards.size());
+ for (StrategyAward strategyAward : strategyAwards) {
+ StrategyAwardEntity strategyAwardEntity = StrategyAwardEntity.builder()
+ .strategyId(strategyAward.getStrategyId())
+ .awardId(strategyAward.getAwardId())
+ .awardCount(strategyAward.getAwardCount())
+ .awardCountSurplus(strategyAward.getAwardCountSurplus())
+ .awardRate(strategyAward.getAwardRate())
+ .build();
+ strategyAwardEntities.add(strategyAwardEntity);
+ }
+ redisService.setValue(cacheKey, strategyAwardEntities);
+ return strategyAwardEntities;
+ }
+
+ @Override
+ public void storeStrategyAwardSearchRateTable(Long strategyId, Integer rateRange, Map strategyAwardSearchRateTable) {
+ // 1. 存储抽奖策略范围值,如10000,用于生成1000以内的随机数
+ redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId, rateRange);
+ // 2. 存储概率查找表
+ Map cacheRateTable = redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId);
+ cacheRateTable.putAll(strategyAwardSearchRateTable);
+
+ }
+
+ @Override
+ public Integer getStrategyAwardAssemble(Long strategyId, Integer rateKey) {
+ return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId, rateKey);
+ }
+
+ @Override
+ public int getRateRange(Long strategyId) {
+ return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId);
+ }
+
+}
+
diff --git a/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/redis/package-info.java b/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/redis/package-info.java
deleted file mode 100644
index 6c4af59..0000000
--- a/xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/redis/package-info.java
+++ /dev/null
@@ -1,4 +0,0 @@
-/**
- * 提供redis链接配置
- */
-package cn.bugstack.infrastructure.redis;
\ No newline at end of file
diff --git a/xfg-frame-archetype-lite-types/src/main/java/cn/bugstack/types/common/Constants.java b/xfg-frame-archetype-lite-types/src/main/java/cn/bugstack/types/common/Constants.java
index 3e21a0d..008bc32 100644
--- a/xfg-frame-archetype-lite-types/src/main/java/cn/bugstack/types/common/Constants.java
+++ b/xfg-frame-archetype-lite-types/src/main/java/cn/bugstack/types/common/Constants.java
@@ -4,4 +4,11 @@ public class Constants {
public final static String SPLIT = ",";
+ public static class RedisKey {
+ public static String STRATEGY_AWARD_KEY = "big_market_strategy_award_key_";
+ public static String STRATEGY_RATE_TABLE_KEY = "big_market_strategy_rate_table_key_";
+ public static String STRATEGY_RATE_RANGE_KEY = "big_market_strategy_rate_range_key_";
+ }
+
+
}
--
GitLab