Authored by mali

收藏接口

@@ -31,6 +31,7 @@ public enum CacheKeyEnum { @@ -31,6 +31,7 @@ public enum CacheKeyEnum {
31 31
32 USER_SKU_FAVORITE_ZSET_KEY("ufo:user:sku:favorite:","用户sku商品收藏集合", 180, TimeUnit.DAYS), 32 USER_SKU_FAVORITE_ZSET_KEY("ufo:user:sku:favorite:","用户sku商品收藏集合", 180, TimeUnit.DAYS),
33 33
  34 + USER_SKU_FAVORITE_FLAG_KEY("ufo:user:sku:favorite:flag:","用户sku收藏sku的标识", 3, TimeUnit.SECONDS),
34 35
35 ; 36 ;
36 37
@@ -8,6 +8,7 @@ import com.yoho.core.redis.cluster.operations.nosync.YHZSetOperations; @@ -8,6 +8,7 @@ import com.yoho.core.redis.cluster.operations.nosync.YHZSetOperations;
8 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 8 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
9 import com.yohoufo.common.cache.CacheHelper; 9 import com.yohoufo.common.cache.CacheHelper;
10 import com.yohoufo.common.cache.CacheKeyEnum; 10 import com.yohoufo.common.cache.CacheKeyEnum;
  11 +import com.yohoufo.common.utils.DateUtil;
11 import com.yohoufo.dal.user.model.UserAuthorizeInfo; 12 import com.yohoufo.dal.user.model.UserAuthorizeInfo;
12 import com.yohoufo.dal.user.model.UserFavorite; 13 import com.yohoufo.dal.user.model.UserFavorite;
13 import com.yohoufo.dal.user.model.UserFavoriteSku; 14 import com.yohoufo.dal.user.model.UserFavoriteSku;
@@ -61,6 +62,11 @@ public class CacheService { @@ -61,6 +62,11 @@ public class CacheService {
61 return RedisKeyBuilder.newInstance().appendFixed(CacheKeyEnum.USER_SKU_FAVORITE_ZSET_KEY.getCacheKey()).appendVar(uid); 62 return RedisKeyBuilder.newInstance().appendFixed(CacheKeyEnum.USER_SKU_FAVORITE_ZSET_KEY.getCacheKey()).appendVar(uid);
62 } 63 }
63 64
  65 + /////// 相关的key构造
  66 + public static RedisKeyBuilder getSkuFavoriteFlagRedisKeyBuilder(Integer uid, Integer storageId){
  67 + return RedisKeyBuilder.newInstance().appendFixed(CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getCacheKey()).appendVarWithMH(uid).appendVar(storageId);
  68 + }
  69 +
64 /************************************************************************** 70 /**************************************************************************
65 * 商品收藏相关 71 * 商品收藏相关
66 *************************************************************************/ 72 *************************************************************************/
@@ -428,6 +434,18 @@ public class CacheService { @@ -428,6 +434,18 @@ public class CacheService {
428 return set; 434 return set;
429 } 435 }
430 436
  437 +
  438 + public boolean setNxKey(Integer userId, Integer storageId) {
  439 + Boolean aBoolean = yhValueOperations.setIfAbsent(getSkuFavoriteFlagRedisKeyBuilder(userId, storageId), String.valueOf(DateUtil.getCurrentTimeSecond()));
  440 + if (aBoolean) {
  441 + yhRedisTemplate.longExpire(getSkuFavoriteFlagRedisKeyBuilder(userId, storageId),
  442 + CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getDefaultExpireTime(),
  443 + CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getTimeUnit());
  444 + }
  445 +
  446 + return aBoolean;
  447 + }
  448 +
431 private String getSkuFacoriteValue(Integer storageId, BigDecimal price) { 449 private String getSkuFacoriteValue(Integer storageId, BigDecimal price) {
432 StringBuilder sb = new StringBuilder(); 450 StringBuilder sb = new StringBuilder();
433 sb.append(storageId).append(":").append(price); 451 sb.append(storageId).append(":").append(price);
@@ -45,6 +45,12 @@ public class FavoriteSkuService { @@ -45,6 +45,12 @@ public class FavoriteSkuService {
45 public void addFavorite(FavoriteSkuRequestVO vo){ 45 public void addFavorite(FavoriteSkuRequestVO vo){
46 logger.info("FavoriteSkuService addFavorite vo {}",vo); 46 logger.info("FavoriteSkuService addFavorite vo {}",vo);
47 47
  48 + // 防止并发,这里用setnx来做
  49 + if (!cacheService.setNxKey(vo.getUid(), vo.getStorageId())) {
  50 + logger.warn("FavoriteSkuService addFavorite exist concurrent call vo {}", vo);
  51 + return;
  52 + }
  53 +
48 boolean isHas = cacheService.hasUserFavoriteSkuKey(vo.getUid(), true); //判断redis是否存在该key,如果不存在先获取所有商品并放入缓存 54 boolean isHas = cacheService.hasUserFavoriteSkuKey(vo.getUid(), true); //判断redis是否存在该key,如果不存在先获取所有商品并放入缓存
49 if(!isHas){ 55 if(!isHas){
50 List<UserFavoriteSku> favoriteList = userFavoriteSkuDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid()); 56 List<UserFavoriteSku> favoriteList = userFavoriteSkuDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());