购物车缓存
parent
f0185f5d17
commit
6f928a717b
|
@ -39,6 +39,10 @@ public class Result<T> implements Serializable {
|
|||
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> buildCode(int code, String msg, T data){
|
||||
return restResult(data, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success () {
|
||||
return restResult(null, SUCCESS, null);
|
||||
}
|
||||
|
@ -71,8 +75,6 @@ public class Result<T> implements Serializable {
|
|||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <T> Result<T> warn () {
|
||||
return restResult(null, WARN, null);
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ public class GlobalExceptionHandler {
|
|||
public Result handleServiceException (ServiceException e, HttpServletRequest request) {
|
||||
log.error(e.getMessage(), e);
|
||||
Integer code = e.getCode();
|
||||
return StringUtils.isNotNull(code) ? Result.error(code, e.getMessage()) : Result.error(e.getMessage());
|
||||
return StringUtils.isNotNull(code) ? Result.buildCode(code, e.getMessage(), null) : Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
package com.muyu.shop.cart.cache;
|
||||
|
||||
import com.muyu.common.cache.abs.HashCacheAbs;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.cache.sourcedata.CartData;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
@ -15,6 +20,8 @@ import java.util.Map;
|
|||
@Component
|
||||
public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
||||
|
||||
@Autowired
|
||||
private CartData cartData;
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
|
@ -22,7 +29,7 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public String keyPre () {
|
||||
return null;
|
||||
return "cart:info:";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -32,7 +39,7 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public Long decode (String redisKey) {
|
||||
return null;
|
||||
return Convert.toLong(redisKey.replace(keyPre(), ""));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +51,7 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public String encodeHashKey (CartHashKey hashKey) {
|
||||
return null;
|
||||
return hashKey.getProjectId()+":"+hashKey.getProjectSku();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,7 +61,11 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public CartHashKey decodeHashKey (String redisHashKey) {
|
||||
return null;
|
||||
String[] split = redisHashKey.split(":");
|
||||
return CartHashKey.builder()
|
||||
.projectId(Convert.toLong(split[0]))
|
||||
.projectSku(split[1])
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,7 +75,7 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData (Long key) {
|
||||
return null;
|
||||
return cartData.getData(key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,7 +88,7 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public CartInfo getData (Long key, CartHashKey hashKey) {
|
||||
return null;
|
||||
return cartData.getData(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,12 +96,12 @@ public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
|||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> defaultValue () {
|
||||
return null;
|
||||
throw new ServiceException("购物车无数据", Result.SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CartInfo defaultHashValue () {
|
||||
return null;
|
||||
throw new ServiceException("购物车无数据", Result.SUCCESS);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.shop.cart.cache.sourcedata;
|
||||
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 购物车源数据获取
|
||||
* @Date 2024-4-2 上午 11:49
|
||||
*/
|
||||
public interface CartData {
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Map<CartHashKey, CartInfo> getData (Long key);
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
*
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash键
|
||||
*
|
||||
* @return hash值
|
||||
*/
|
||||
public CartInfo getData (Long key, CartHashKey hashKey);
|
||||
}
|
|
@ -84,7 +84,12 @@
|
|||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-common</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 购物车模块 缓存 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package com.muyu.shop.cart.service.sourcedata;
|
||||
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.cache.sourcedata.CartData;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import com.muyu.shop.cart.service.CartInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 缓存实现
|
||||
* @Date 2024-4-2 上午 11:51
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class CartDataImpl implements CartData {
|
||||
|
||||
@Autowired
|
||||
private CartInfoService cartInfoService;
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
*
|
||||
* @param key 键
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData (Long key) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
*
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash键
|
||||
*
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public CartInfo getData (Long key, CartHashKey hashKey) {
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue