Authored by mali

收藏接口

... ... @@ -31,6 +31,7 @@ public enum CacheKeyEnum {
USER_SKU_FAVORITE_ZSET_KEY("ufo:user:sku:favorite:","用户sku商品收藏集合", 180, TimeUnit.DAYS),
USER_SKU_FAVORITE_FLAG_KEY("ufo:user:sku:favorite:flag:","用户sku收藏sku的标识", 3, TimeUnit.SECONDS),
;
... ...
... ... @@ -8,6 +8,7 @@ import com.yoho.core.redis.cluster.operations.nosync.YHZSetOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.cache.CacheHelper;
import com.yohoufo.common.cache.CacheKeyEnum;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.user.model.UserAuthorizeInfo;
import com.yohoufo.dal.user.model.UserFavorite;
import com.yohoufo.dal.user.model.UserFavoriteSku;
... ... @@ -61,6 +62,11 @@ public class CacheService {
return RedisKeyBuilder.newInstance().appendFixed(CacheKeyEnum.USER_SKU_FAVORITE_ZSET_KEY.getCacheKey()).appendVar(uid);
}
/////// 相关的key构造
public static RedisKeyBuilder getSkuFavoriteFlagRedisKeyBuilder(Integer uid, Integer storageId){
return RedisKeyBuilder.newInstance().appendFixed(CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getCacheKey()).appendVarWithMH(uid).appendVar(storageId);
}
/**************************************************************************
* 商品收藏相关
*************************************************************************/
... ... @@ -428,6 +434,18 @@ public class CacheService {
return set;
}
public boolean setNxKey(Integer userId, Integer storageId) {
Boolean aBoolean = yhValueOperations.setIfAbsent(getSkuFavoriteFlagRedisKeyBuilder(userId, storageId), String.valueOf(DateUtil.getCurrentTimeSecond()));
if (aBoolean) {
yhRedisTemplate.longExpire(getSkuFavoriteFlagRedisKeyBuilder(userId, storageId),
CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getDefaultExpireTime(),
CacheKeyEnum.USER_SKU_FAVORITE_FLAG_KEY.getTimeUnit());
}
return aBoolean;
}
private String getSkuFacoriteValue(Integer storageId, BigDecimal price) {
StringBuilder sb = new StringBuilder();
sb.append(storageId).append(":").append(price);
... ...
... ... @@ -45,6 +45,12 @@ public class FavoriteSkuService {
public void addFavorite(FavoriteSkuRequestVO vo){
logger.info("FavoriteSkuService addFavorite vo {}",vo);
// 防止并发,这里用setnx来做
if (!cacheService.setNxKey(vo.getUid(), vo.getStorageId())) {
logger.warn("FavoriteSkuService addFavorite exist concurrent call vo {}", vo);
return;
}
boolean isHas = cacheService.hasUserFavoriteSkuKey(vo.getUid(), true); //判断redis是否存在该key,如果不存在先获取所有商品并放入缓存
if(!isHas){
List<UserFavoriteSku> favoriteList = userFavoriteSkuDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());
... ...