替换Jedis-使用RedissonClient实现JedisService
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,56 +0,0 @@
|
||||
package com.accompany.core.vo;
|
||||
|
||||
import redis.clients.jedis.Response;
|
||||
|
||||
/**
|
||||
* @author qiudonglin
|
||||
* @description redis hash数据结构
|
||||
* @date 2020/3/4/0004
|
||||
*/
|
||||
public class RedisHashVo {
|
||||
|
||||
private String key;
|
||||
private String field;
|
||||
private String value;
|
||||
private Response<String> response;
|
||||
|
||||
public RedisHashVo() {
|
||||
}
|
||||
|
||||
public RedisHashVo(String key, String field) {
|
||||
this.key = key;
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Response<String> getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public void setResponse(Response<String> response) {
|
||||
this.response = response;
|
||||
}
|
||||
}
|
@@ -270,13 +270,9 @@ public class AccountBlockService extends ServiceImpl<AccountBlockMapper, Account
|
||||
* @param blockList
|
||||
* @throws Exception
|
||||
*/
|
||||
public void saveBlockedAccountCache(List<AccountBlock> blockList) throws Exception {
|
||||
jedisService.doInPipeline((pipeline) -> {
|
||||
blockList.stream().forEach(accountBlock -> {
|
||||
pipeline.hset(RedisKey.block_account.getKey(accountBlock.getBlockType().toString()),
|
||||
accountBlock.getBlockValue(), gson.toJson(accountBlock));
|
||||
});
|
||||
});
|
||||
public void saveBlockedAccountCache(List<AccountBlock> blockList) {
|
||||
blockList.forEach(accountBlock -> jedisService.hset(RedisKey.block_account.getKey(accountBlock.getBlockType().toString()),
|
||||
accountBlock.getBlockValue(), gson.toJson(accountBlock)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,13 +280,9 @@ public class AccountBlockService extends ServiceImpl<AccountBlockMapper, Account
|
||||
*
|
||||
* @param blockList
|
||||
*/
|
||||
public void deleteBlockedAccountCache(List<AccountBlock> blockList) throws Exception {
|
||||
jedisService.doInPipeline((pipeline) -> {
|
||||
blockList.stream().forEach(accountBlock -> {
|
||||
pipeline.hdel(RedisKey.block_account.getKey(accountBlock.getBlockType().toString()),
|
||||
accountBlock.getBlockValue());
|
||||
});
|
||||
});
|
||||
public void deleteBlockedAccountCache(List<AccountBlock> blockList) {
|
||||
blockList.forEach(accountBlock -> jedisService.hdel(RedisKey.block_account.getKey(accountBlock.getBlockType().toString()),
|
||||
accountBlock.getBlockValue()));
|
||||
}
|
||||
|
||||
public List<AccountBlock> batchQueryAccountBlock(List<String> erbanNos) {
|
||||
|
@@ -52,7 +52,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
public class ActivityPackService {
|
||||
|
||||
private static final String HASH_TAG = "{pack_stock}";
|
||||
|
||||
@@ -73,14 +73,9 @@ public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
private UsersService usersService;
|
||||
@Autowired
|
||||
private ActivityAwardMapper activityAwardMapper;
|
||||
@Autowired
|
||||
private BillRecordService billRecordService;
|
||||
|
||||
private ActivityPackService self;
|
||||
|
||||
private static String buyPackScriptSha;
|
||||
private static String reduceSaleScriptSha;
|
||||
|
||||
private Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
@@ -228,9 +223,6 @@ public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
if (totalPriceD.compareTo(purse.getDiamonds()) > 0) {
|
||||
throw new ServiceException(BusiStatus.PURSEMONEYNOTENOUGH);
|
||||
}
|
||||
List<String> keys = Lists.newArrayList(getSalesKey(date), getUserBuyNumKey(date, uid));
|
||||
List<String> args = Lists.newArrayList(String.valueOf(packId), String.valueOf(packNum), String.valueOf(stock), String.valueOf(allowBuyNum));
|
||||
deductStock(keys, args);
|
||||
try {
|
||||
//记录账单
|
||||
BillObjTypeEnum objTypeEnum = getBillObjTypeByPackType(packType);
|
||||
@@ -242,8 +234,6 @@ public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
sendPackMessage2MQ(uid, totalPrice, packId, packName, packNum, sellingPrice, ticketNum, packType);
|
||||
} catch (ServiceException e) {
|
||||
log.error("购买礼包异常", e);
|
||||
//失败则减少当天的销售量
|
||||
self.backStock(keys, args);
|
||||
throw e;
|
||||
}
|
||||
return ticketNum * packNum;
|
||||
@@ -290,29 +280,6 @@ public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
return RedisKey.pack_user_buy_num.getKey(uid + "_" + DateTimeUtil.convertDate(date, DateTimeUtil.DEFAULT_DATE_PATTERN_) + HASH_TAG);
|
||||
}
|
||||
|
||||
private void deductStock(List<String> keys, List<String> args) {
|
||||
Object res = jedisService.evalsha(buyPackScriptSha, keys, args);
|
||||
if (res == null) {
|
||||
log.error("exec lua fail. keys:{}, args:{}", keys, args);
|
||||
throw new ServiceException(BusiStatus.SERVERERROR);
|
||||
}
|
||||
int resCode = Integer.parseInt(res.toString());
|
||||
switch (resCode) {
|
||||
case 0:
|
||||
//成功
|
||||
break;
|
||||
case 1:
|
||||
throw new ServiceException(BusiStatus.PACK_UNDER_STOCK);
|
||||
case 2:
|
||||
throw new ServiceException(BusiStatus.PACK_REACH_BUY_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
@Async
|
||||
public void backStock(List<String> keys, List<String> args) {
|
||||
jedisService.evalsha(reduceSaleScriptSha, keys, args);
|
||||
}
|
||||
|
||||
private void sendPackMessage2MQ(long uid, long totalPrice, int packId, String packName, int packNum, long packPrice,
|
||||
Integer ticketNum, Byte packType) {
|
||||
ActivityPackMessage message = new ActivityPackMessage();
|
||||
@@ -379,28 +346,6 @@ public class ActivityPackService implements InitializingBean, BeanSelfAware {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
//TODO 待优化
|
||||
ClassLoader classLoader = this.getClass().getClassLoader();
|
||||
try (Reader buyPackReader = new InputStreamReader(classLoader.getResourceAsStream("lua/activity/deductStock.lua"));
|
||||
Reader reduceSalesReader = new InputStreamReader(classLoader.getResourceAsStream("lua/activity/backStock.lua"))) {
|
||||
|
||||
String buyPackScript = CharStreams.toString(buyPackReader);
|
||||
buyPackScriptSha = jedisService.scriptLoad(buyPackScript);
|
||||
Assert.notNull(buyPackScriptSha, "buyPackScriptSha is null");
|
||||
|
||||
String reduceSaleScript = CharStreams.toString(reduceSalesReader);
|
||||
reduceSaleScriptSha = jedisService.scriptLoad(reduceSaleScript);
|
||||
Assert.notNull(reduceSaleScriptSha, "reduceSaleScriptSha is null");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanSelf(Object self) {
|
||||
this.self = (ActivityPackService) self;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据礼包类型查询礼包
|
||||
*
|
||||
|
@@ -728,17 +728,14 @@ public class DynamicSquareService extends CommunityBaseService {
|
||||
|
||||
if (CollectionUtils.isEmpty(rsIdList)) return rsIdList;
|
||||
|
||||
int size = rsIdList.size();
|
||||
jedisService.doInPipeline(pipeline -> {
|
||||
int num = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
num++;
|
||||
pipeline.zadd(RedisKey.dynamic_square_recently.getKey(), Double.valueOf(num), String.valueOf(rsIdList.get(i)));
|
||||
}
|
||||
//每5分钟刷新一次
|
||||
pipeline.expire(RedisKey.dynamic_square_recently.getKey(), 5 * 60);
|
||||
});
|
||||
int num = 1;
|
||||
for (Long id : rsIdList){
|
||||
jedisService.zadd(RedisKey.dynamic_square_recently.getKey(), Double.valueOf(num++), String.valueOf(id));
|
||||
}
|
||||
jedisService.expire(RedisKey.dynamic_square_recently.getKey(), 5 * 60);
|
||||
logger.info("广场动态写入redis完成{}", rsIdList.stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA)));
|
||||
|
||||
|
||||
}catch (Exception e){
|
||||
log.error("广场动态缓存处理异常", e);
|
||||
throw new ServiceException(BusiStatus.SQUARE_DYNAMIC_CACHE_PROCESSING_EXCEPTION);
|
||||
|
@@ -42,6 +42,7 @@ import com.accompany.core.service.message.MessageRobotPushService;
|
||||
import com.accompany.core.util.DateUtil;
|
||||
import com.accompany.core.vo.UserLevelVo;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@@ -233,12 +234,7 @@ public class LinearlyPrizePoolService {
|
||||
initCurrentLineIdIfNecessary(lineId);
|
||||
// 按奖品线id分组保存到缓存中
|
||||
String cachKey = RedisKey.linearly_pool_draw_line.getKey(lineId.toString());
|
||||
jedisService.doInPipeline((pipeline) ->
|
||||
poolLine.forEach(item -> {
|
||||
pipeline.rpush(cachKey, JSONObject.toJSONString(item));
|
||||
})
|
||||
|
||||
);
|
||||
jedisService.rpushList(cachKey, poolLine.stream().map(JSON::toJSONString).toList());
|
||||
jedisService.hset(RedisKey.linearly_pool_stat.getKey(), CACHE_FIELD_MAX_LINE_ID, lineId.toString());
|
||||
jedisService.zadd(RedisKey.linearly_pool_line_id_list.getKey(), lineId.doubleValue(), lineId.toString());
|
||||
saveDataStopWatch.stop();
|
||||
|
@@ -67,6 +67,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -243,30 +244,13 @@ public class UserNameplateService extends BaseService {
|
||||
return vo;
|
||||
}
|
||||
|
||||
public Map<Long, UserNameplateVo> getBatchUserUsingNameplateVo(List<String> uids) throws Exception {
|
||||
Map<Long, UserNameplateVo> userNameplateMap = Maps.newHashMap();
|
||||
Map<Long, Response<String>> userNameplateMapStr = Maps.newHashMap();
|
||||
jedisService.doInPipeline((pipeline) -> {
|
||||
uids.forEach(uid -> {
|
||||
Response<String> response = pipeline.hget(RedisKey.user_using_nameplate.getKey(), uid);
|
||||
userNameplateMapStr.put(Long.valueOf(uid), response);
|
||||
});
|
||||
});
|
||||
userNameplateMapStr.entrySet().forEach(entry -> {
|
||||
Long key = entry.getKey();
|
||||
Response<String> response = entry.getValue();
|
||||
if (ApplicationConstant.NULL_JSON_OBJECT.equalsIgnoreCase(response.get()) || StringUtils.isEmpty(response.get())) {
|
||||
return;
|
||||
}
|
||||
UserNameplateVo vo = this.gson.fromJson(response.get(), UserNameplateVo.class);
|
||||
vo = checkNameplateVo(vo);
|
||||
if (vo.getExpireTime().getTime() <= System.currentTimeMillis()) {
|
||||
this.jedisService.hdel(RedisKey.user_using_nameplate.getKey(), key.toString());
|
||||
return;
|
||||
}
|
||||
userNameplateMap.put(key, vo);
|
||||
});
|
||||
return userNameplateMap;
|
||||
public Map<Long, UserNameplateVo> getBatchUserUsingNameplateVo(List<String> uids) {
|
||||
return uids.stream().map(uid -> this.jedisService.hget(RedisKey.user_using_nameplate.getKey(), uid))
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.map(str -> gson.fromJson(str, UserNameplateVo.class))
|
||||
.map(this::checkNameplateVo)
|
||||
.filter(vo->vo.getExpireTime().getTime() > System.currentTimeMillis())
|
||||
.collect(Collectors.toMap(UserNameplateVo::getUid, v -> v));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -142,17 +142,15 @@ public class RoomGiftValueService extends BaseService {
|
||||
} else {
|
||||
partitionId = null;
|
||||
}
|
||||
Set<Long> newUids = new HashSet<>(targetUids);
|
||||
try {
|
||||
jedisService.doInPipeline((pipeline) ->
|
||||
newUids.forEach(uid -> {
|
||||
// 更新用户礼物值
|
||||
jedisService.hincrbyfloat(roomKey(roomUid), uid.toString(), value);
|
||||
// 更新用户收到哪些用户的礼物
|
||||
jedisService.zincrby(rankKey(roomUid, uid.toString(), partitionId), value, sendUid.toString());
|
||||
})
|
||||
);
|
||||
|
||||
try {
|
||||
Set<Long> newUids = new HashSet<>(targetUids);
|
||||
newUids.parallelStream().forEach(uid -> {
|
||||
// 更新用户礼物值
|
||||
jedisService.hincrbyfloat(roomKey(roomUid), uid.toString(), value);
|
||||
// 更新用户收到哪些用户的礼物
|
||||
jedisService.zincrby(rankKey(roomUid, uid.toString(), partitionId), value, sendUid.toString());
|
||||
});
|
||||
|
||||
scrambleVipMic(roomUid, targetUids, value, sendUid);
|
||||
executeTrigger(roomUid);
|
||||
@@ -196,7 +194,7 @@ public class RoomGiftValueService extends BaseService {
|
||||
*
|
||||
* @param roomUid
|
||||
*/
|
||||
public void cleanGiftValueCache(Long roomUid) throws Exception {
|
||||
public void cleanGiftValueCache(Long roomUid) {
|
||||
Integer partitionId;
|
||||
Room room = roomService.getRoomByUid(roomUid);
|
||||
if (room != null) {
|
||||
@@ -205,10 +203,7 @@ public class RoomGiftValueService extends BaseService {
|
||||
partitionId = null;
|
||||
}
|
||||
Set<String> uidSet = jedisService.hgetAllKeysByKey(roomKey(roomUid));
|
||||
// pipeline清除房间内所有用户的记录
|
||||
jedisService.doInPipeline((pipeline) ->
|
||||
uidSet.forEach(uid -> pipeline.del(rankKey(roomUid, uid, partitionId)))
|
||||
);
|
||||
uidSet.forEach(uid -> jedisService.del(rankKey(roomUid, uid, partitionId)));
|
||||
// 清除房间用户set
|
||||
jedisService.del(roomKey(roomUid));
|
||||
}
|
||||
|
@@ -392,13 +392,11 @@ public class RoomSearchService extends BaseService {
|
||||
// 注意!!!!:这个列表返回的
|
||||
List<Room> roomList = roomMapper.queryRoomByPermitType(permitType);
|
||||
if (cacheKey != null) {
|
||||
jedisService.doInPipeline((pipeline -> {
|
||||
roomList.forEach(room -> {
|
||||
int onlineNum = room.getOnlineNum() == null ? 0 : room.getOnlineNum();
|
||||
pipeline.zadd(cacheKey, onlineNum, String.valueOf(room.getUid()));
|
||||
});
|
||||
pipeline.expire(cacheKey, 5);
|
||||
}));
|
||||
roomList.forEach(room -> {
|
||||
int onlineNum = room.getOnlineNum() == null ? 0 : room.getOnlineNum();
|
||||
jedisService.zadd(cacheKey, Integer.valueOf(onlineNum).doubleValue(), String.valueOf(room.getUid()));
|
||||
});
|
||||
jedisService.expire(cacheKey, 5);
|
||||
}
|
||||
}
|
||||
if (cacheKey != null) {
|
||||
|
@@ -365,7 +365,9 @@ public class CrossRoomPkRoundServiceImpl extends ServiceImpl<CrossRoomPkRoundMap
|
||||
jedisService.set(RedisKey.cross_room_pk_auto_end.getKey(String.valueOf(round.getId())), String.valueOf(1), round.getDuration() * 60);
|
||||
|
||||
List<String> pkUidList = Arrays.asList(String.valueOf(inviteeRoom.getUid()), String.valueOf(inviteRoom.getUid()));
|
||||
jedisService.doInPipeline((pipeline -> pkUidList.forEach(uidStr -> pipeline.hset(RedisKey.corss_room_round_pking.getKey(), uidStr, String.valueOf(round.getId())))));
|
||||
for (String uidStr : pkUidList){
|
||||
jedisService.hset(RedisKey.corss_room_round_pking.getKey(), uidStr, String.valueOf(round.getId()));
|
||||
}
|
||||
|
||||
String limitKey = RedisKey.cross_room_pk_initiate_limit.getKey(inviteRoom.getUid() + StrUtil.UNDERLINE + inviteeRoom.getUid());
|
||||
jedisService.del(limitKey);
|
||||
@@ -576,7 +578,9 @@ public class CrossRoomPkRoundServiceImpl extends ServiceImpl<CrossRoomPkRoundMap
|
||||
}
|
||||
// 清除正在pk的缓存key
|
||||
List<String> pkUidList = Arrays.asList(String.valueOf(invitePkRoom.getRoomUid()), String.valueOf(acceptPkRoom.getRoomUid()));
|
||||
jedisService.doInPipeline((pipeline -> pkUidList.forEach(uidStr -> pipeline.hdel(RedisKey.corss_room_round_pking.getKey(), uidStr))));
|
||||
for (String uidStr : pkUidList){
|
||||
jedisService.hdel(RedisKey.corss_room_round_pking.getKey(), uidStr);
|
||||
}
|
||||
}
|
||||
|
||||
//更新参与双方相关数据,如果为非强制结束,设置实际结束时间为计划结束时间
|
||||
@@ -1234,7 +1238,9 @@ public class CrossRoomPkRoundServiceImpl extends ServiceImpl<CrossRoomPkRoundMap
|
||||
}
|
||||
sendSingleRoomPkEndMsg(anotherRoom.getRoomId(), currentRoom.getUid());
|
||||
List<String> pkUidList = Arrays.asList(String.valueOf(invitePkRoom.getRoomUid()), String.valueOf(acceptPkRoom.getRoomUid()));
|
||||
jedisService.doInPipeline((pipeline -> pkUidList.forEach(uidStr -> pipeline.hdel(RedisKey.corss_room_round_pking.getKey(), uidStr))));
|
||||
for (String uidStr : pkUidList){
|
||||
jedisService.hdel(RedisKey.corss_room_round_pking.getKey(), uidStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("结束pk出现异常", e);
|
||||
throw new ServiceException(BusiStatus.SERVERBUSY);
|
||||
|
@@ -31,7 +31,6 @@ import com.accompany.core.model.Users;
|
||||
import com.accompany.core.service.SysConfService;
|
||||
import com.accompany.core.service.common.JedisService;
|
||||
import com.accompany.core.service.user.UsersBaseService;
|
||||
import com.accompany.core.vo.RedisHashVo;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
@@ -287,19 +286,12 @@ public class SingleBroadcastPopularityServiceImpl extends ServiceImpl<SingleBroa
|
||||
|
||||
|
||||
private List<SingleBroadcastMainVO> getSingleBroadcastMainVOList(List<Long> uids) {
|
||||
List<RedisHashVo> voList = new ArrayList<>();
|
||||
for (Long uid : uids) {
|
||||
RedisHashVo redisHashVo = new RedisHashVo();
|
||||
redisHashVo.setKey(RedisKey.single_broadcast_room_popularity.getKey());
|
||||
redisHashVo.setField(uid.toString());
|
||||
voList.add(redisHashVo);
|
||||
}
|
||||
List<RedisHashVo> redisHashVoList = jedisService.hgetBatch(voList);
|
||||
List<String> redisHashVoList = jedisService.hmread(RedisKey.single_broadcast_room_popularity.getKey(), uids.toArray(String[]::new));
|
||||
|
||||
List<SingleBroadcastCacheDto> dtoList = new LinkedList<>();
|
||||
for (RedisHashVo hashVo : redisHashVoList) {
|
||||
if (StringUtils.isNotEmpty(hashVo.getValue())) {
|
||||
SingleBroadcastCacheDto singleBroad = gson.fromJson(hashVo.getValue(), SingleBroadcastCacheDto.class);
|
||||
for (String hashVo : redisHashVoList) {
|
||||
if (StringUtils.isNotEmpty(hashVo)) {
|
||||
SingleBroadcastCacheDto singleBroad = gson.fromJson(hashVo, SingleBroadcastCacheDto.class);
|
||||
dtoList.add(singleBroad);
|
||||
}
|
||||
}
|
||||
@@ -316,7 +308,7 @@ public class SingleBroadcastPopularityServiceImpl extends ServiceImpl<SingleBroa
|
||||
}
|
||||
}
|
||||
|
||||
singleBroadcastList.stream().forEach(
|
||||
singleBroadcastList.forEach(
|
||||
result -> {
|
||||
String roundIdStr = jedisService.hget(RedisKey.corss_room_round_pking.getKey(), String.valueOf(result.getUid()));
|
||||
result.setCrossPking(org.apache.commons.lang3.StringUtils.isNotBlank(roundIdStr));
|
||||
@@ -328,7 +320,7 @@ public class SingleBroadcastPopularityServiceImpl extends ServiceImpl<SingleBroa
|
||||
|
||||
|
||||
@Override
|
||||
public boolean initSingleBroadcastCache() throws IOException {
|
||||
public boolean initSingleBroadcastCache() {
|
||||
QueryWrapper<SingleBroadcastPopularity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda().eq(SingleBroadcastPopularity::getIsDel, Constant.SingleBroadcastDel.NO_DEL).orderByDesc(SingleBroadcastPopularity::getIsTop)
|
||||
.orderByAsc(SingleBroadcastPopularity::getTopOrder);
|
||||
@@ -336,12 +328,10 @@ public class SingleBroadcastPopularityServiceImpl extends ServiceImpl<SingleBroa
|
||||
List<SingleBroadcastPopularity> recordList = this.list(queryWrapper);
|
||||
if (CollectionUtils.isEmpty(recordList)) return Boolean.TRUE;
|
||||
|
||||
jedisService.doInPipeline((pipeline) -> {
|
||||
recordList.stream().forEach(broadcastPopularity -> {
|
||||
SingleBroadcastCacheDto singleBroadcastCacheDto = buildSingleBroadcastCacheDtoF(broadcastPopularity, x -> buildSingleBroadcastCacheDto(x));
|
||||
pipeline.hset(RedisKey.single_broadcast_room_popularity.getKey(),
|
||||
String.valueOf(broadcastPopularity.getUid()), gson.toJson(singleBroadcastCacheDto));
|
||||
});
|
||||
recordList.stream().forEach(broadcastPopularity -> {
|
||||
SingleBroadcastCacheDto singleBroadcastCacheDto = buildSingleBroadcastCacheDtoF(broadcastPopularity, x -> buildSingleBroadcastCacheDto(x));
|
||||
jedisService.hset(RedisKey.single_broadcast_room_popularity.getKey(),
|
||||
String.valueOf(broadcastPopularity.getUid()), gson.toJson(singleBroadcastCacheDto));
|
||||
});
|
||||
|
||||
return Boolean.FALSE;
|
||||
|
@@ -88,6 +88,7 @@ import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 夺宝精灵活动
|
||||
@@ -283,20 +284,11 @@ public class SeizeTreasureService {
|
||||
SpringContextHolder.getBean(SeizeTreasureService.class).initForestPool(poolLevel);
|
||||
throw new ServiceException(BusiStatus.SEIZE_TREASURE_DRAW_TOO_BUSY);
|
||||
}
|
||||
List<SeizeTreasurePoolRewardCache> rewardCacheList = new ArrayList<>(drawNum);
|
||||
List<Response<String>> rewardRespList = new ArrayList<>();
|
||||
jedisService.doInPipeline(pipeline -> {
|
||||
for (int i = 0; i < drawNum; i++) {
|
||||
rewardRespList.add(pipeline.lpop(poolKey));
|
||||
}
|
||||
pipeline.sync();
|
||||
});
|
||||
rewardRespList.forEach(resp -> {
|
||||
String rewardCacheStr = resp.get();
|
||||
if (StringUtils.hasText(rewardCacheStr)) {
|
||||
rewardCacheList.add(gson.fromJson(rewardCacheStr, SeizeTreasurePoolRewardCache.class));
|
||||
}
|
||||
});
|
||||
List<SeizeTreasurePoolRewardCache> rewardCacheList = IntStream.range(0, drawNum)
|
||||
.mapToObj(n->jedisService.lpop(poolKey))
|
||||
.filter(StringUtils::hasText)
|
||||
.map(rewardCacheStr -> gson.fromJson(rewardCacheStr, SeizeTreasurePoolRewardCache.class))
|
||||
.toList();
|
||||
// 取出奖励数量小于抽奖次数
|
||||
if (rewardCacheList.size() < drawNum) {
|
||||
log.info("SeizeTreasureService getForestPoolReward rewardCacheList.size:{} < drawNum:{}", rewardCacheList.size(), drawNum);
|
||||
@@ -387,20 +379,12 @@ public class SeizeTreasureService {
|
||||
//累计抽奖次数
|
||||
Long currentDrawNum = jedisService.hincrBy(userInfoKey, DrawUserInfoField.CURRENT_DRAW_NUM_FIELD, drawNum.longValue());
|
||||
if (CollectionUtil.isNotEmpty(poolRewardList)) {
|
||||
try {
|
||||
jedisService.doInPipeline(pipeline -> {
|
||||
for (int i = 0; i < drawNum && i < poolRewardList.size(); i++) {
|
||||
SeizeTreasurePoolRewardCache rewardCache = poolRewardList.get(i);
|
||||
//清空上次差值
|
||||
if (ActPropsId.highElfIdList.contains(rewardCache.getReward().getRewardRefId())) {
|
||||
pipeline.hdel(RedisKey.getActRedisKey(RedisKey.USER_INFO_KEY), DrawUserInfoField.LESS_DRAW_NUM_FIELD);
|
||||
}
|
||||
//更新差值数
|
||||
pipeline.hincrBy(userInfoKey, DrawUserInfoField.LESS_DRAW_NUM_FIELD, 1L);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
for (SeizeTreasurePoolRewardCache rewardCache : poolRewardList){
|
||||
if (ActPropsId.highElfIdList.contains(rewardCache.getReward().getRewardRefId())) {
|
||||
//更新差值数
|
||||
jedisService.hdel(RedisKey.getActRedisKey(RedisKey.USER_INFO_KEY), DrawUserInfoField.LESS_DRAW_NUM_FIELD);
|
||||
}
|
||||
jedisService.hincrBy(userInfoKey, DrawUserInfoField.LESS_DRAW_NUM_FIELD, 1L);
|
||||
}
|
||||
}
|
||||
updateLuckyNum();
|
||||
@@ -682,7 +666,7 @@ public class SeizeTreasureService {
|
||||
return seizeTreasurePoolRewardVos.stream().sorted(Comparator.comparingInt(SeizeTreasurePoolRewardVo::getRewardOrder).reversed()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<SeizeTreasurePoolRewardCache> getTreasurePoolReward(Integer drawNum) throws IOException {
|
||||
public List<SeizeTreasurePoolRewardCache> getTreasurePoolReward(Integer drawNum) {
|
||||
String poolRedisKey = RedisKey.getActRedisKey(RedisKey.POOL_KEY);
|
||||
Long poolSize = jedisService.llen(poolRedisKey);
|
||||
log.info("SeizeTreasureService getTreasurePoolReward poolRedisKey:{} drawNum:{} poolSize:{}", poolRedisKey, drawNum, poolSize);
|
||||
@@ -693,20 +677,12 @@ public class SeizeTreasureService {
|
||||
SpringContextHolder.getBean(SeizeTreasureService.class).initTreasurePool();
|
||||
throw new ServiceException(BusiStatus.SEIZE_TREASURE_DRAW_TOO_BUSY);
|
||||
}
|
||||
List<SeizeTreasurePoolRewardCache> rewardCacheList = new ArrayList<>(drawNum);
|
||||
List<Response<String>> rewardRespList = new ArrayList<>();
|
||||
jedisService.doInPipeline(pipeline -> {
|
||||
for (int i = 0; i < drawNum; i++) {
|
||||
rewardRespList.add(pipeline.lpop(poolRedisKey));
|
||||
}
|
||||
pipeline.sync();
|
||||
});
|
||||
rewardRespList.forEach(resp -> {
|
||||
String rewardCacheStr = resp.get();
|
||||
if (StringUtils.hasText(rewardCacheStr)) {
|
||||
rewardCacheList.add(gson.fromJson(rewardCacheStr, SeizeTreasurePoolRewardCache.class));
|
||||
}
|
||||
});
|
||||
List<SeizeTreasurePoolRewardCache> rewardCacheList = IntStream.range(0, drawNum)
|
||||
.mapToObj(n->jedisService.lpop(poolRedisKey))
|
||||
.filter(StringUtils::hasText)
|
||||
.map(rewardCacheStr -> gson.fromJson(rewardCacheStr, SeizeTreasurePoolRewardCache.class))
|
||||
.toList();
|
||||
|
||||
// 取出奖励数量小于抽奖次数
|
||||
if (rewardCacheList.size() < drawNum) {
|
||||
log.info("SeizeTreasureService getTreasurePoolReward rewardCacheList.size():{} < drawNum:{}", rewardCacheList.size(), drawNum);
|
||||
|
@@ -36,6 +36,7 @@ import org.springframework.stereotype.Service;
|
||||
import redis.clients.jedis.Response;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Created by liuguofu on 2017/10/17.
|
||||
@@ -139,21 +140,14 @@ public class UserInRoomService {
|
||||
return getUserInRoomMapBatchCache(uids);
|
||||
}
|
||||
|
||||
public Map<Long, RoomVo> getUserInRoomMapBatchCache(List<String> uids) throws Exception {
|
||||
Map<Long, RoomVo> roomVoMap = Maps.newHashMap();
|
||||
Map<Long, Response<String>> roomVoMapStr = Maps.newHashMap();
|
||||
jedisService.doInPipeline((pipeline) -> {
|
||||
uids.forEach(uid -> {
|
||||
Response<String> response = pipeline.hget(RedisKey.user_in_room.getKey(), uid);
|
||||
roomVoMapStr.put(Long.valueOf(uid), response);
|
||||
});
|
||||
});
|
||||
roomVoMapStr.forEach((key, response) -> {
|
||||
if (StringUtils.isNotBlank(response.get())) {
|
||||
roomVoMap.put(key, GsonUtil.getDefGson().fromJson(response.get(), RoomVo.class));
|
||||
public Map<Long, RoomVo> getUserInRoomMapBatchCache(List<String> uids) {
|
||||
return uids.stream().map(uid -> {
|
||||
String roomVoStr = jedisService.hget(RedisKey.user_in_room.getKey(), uid);
|
||||
if (StringUtils.isBlank(roomVoStr)){
|
||||
return null;
|
||||
}
|
||||
});
|
||||
return roomVoMap;
|
||||
return GsonUtil.getDefGson().fromJson(roomVoStr, RoomVo.class);
|
||||
}).filter(Objects::nonNull).collect(Collectors.toMap(RoomVo::getUid, roomVo -> roomVo));
|
||||
}
|
||||
|
||||
public void updateUserInRoomRecord(Long uid, Long roomUid, Byte behaveType) {
|
||||
|
@@ -1,8 +0,0 @@
|
||||
|
||||
local packId = ARGV[1]
|
||||
local num = tonumber(ARGV[2])
|
||||
|
||||
redis.call('HINCRBY',KEYS[1],packId,-num)
|
||||
redis.call('HINCRBY',KEYS[2],packId,-num)
|
||||
|
||||
return 0
|
@@ -1,33 +0,0 @@
|
||||
|
||||
local packId = ARGV[1]
|
||||
local num = tonumber(ARGV[2])
|
||||
local stock = tonumber(ARGV[3])
|
||||
local allowBuyNum = tonumber(ARGV[4])
|
||||
local expireSeconds = 24 * 60 * 60
|
||||
|
||||
local salesStr = redis.call('HGET',KEYS[1],packId)
|
||||
local sales = (salesStr ~= false and tonumber(salesStr)) or 0
|
||||
if stock > 0 and sales + num > stock then
|
||||
return 1
|
||||
end
|
||||
|
||||
local userBuyNumStr = redis.call('HGET',KEYS[2],packId)
|
||||
local userBuyNum = (userBuyNumStr ~= false and tonumber(userBuyNumStr)) or 0
|
||||
if allowBuyNum > 0 and userBuyNum + num > allowBuyNum then
|
||||
return 2
|
||||
end
|
||||
|
||||
redis.call('HINCRBY',KEYS[1],packId,num)
|
||||
redis.call('HINCRBY',KEYS[2],packId,num)
|
||||
|
||||
local salesTtl = redis.call('ttl', KEYS[1])
|
||||
if salesTtl == -1 then
|
||||
redis.call('EXPIRE',KEYS[1],expireSeconds)
|
||||
end
|
||||
|
||||
local userBuyNumTtl = redis.call('ttl', KEYS[2])
|
||||
if userBuyNumTtl == -1 then
|
||||
redis.call('EXPIRE',KEYS[2],expireSeconds)
|
||||
end
|
||||
|
||||
return 0
|
Reference in New Issue
Block a user