购物车缓存

master
DongZeLiang 2024-04-02 11:52:17 +08:00
parent f0185f5d17
commit 6f928a717b
6 changed files with 109 additions and 12 deletions

View File

@ -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);
}

View File

@ -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());
}
/**

View File

@ -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);
}
}

View File

@ -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);
/**
* hashhash
*
* @param key
* @param hashKey hash
*
* @return hash
*/
public CartInfo getData (Long key, CartHashKey hashKey);
}

View File

@ -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>

View File

@ -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;
}
/**
* hashhash
*
* @param key
* @param hashKey hash
*
* @return hash
*/
@Override
public CartInfo getData (Long key, CartHashKey hashKey) {
return null;
}
}