购物车缓存
parent
ba01e230ef
commit
2155eeab10
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-shop-cart-cache</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 商品服务公共依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-common</artifactId>
|
||||
</dependency>
|
||||
<!-- 缓存基准依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-cache</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,100 @@
|
|||
package com.muy.shop.cart.cache;
|
||||
|
||||
import com.muy.shop.cart.cache.key.CartHashKey;
|
||||
import com.muy.shop.cart.cache.sourcedata.CartData;
|
||||
import com.muyu.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.domain.CartInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CartCache
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
@Component
|
||||
public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
||||
@Autowired
|
||||
private CartData cartData;
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
* @return key前缀
|
||||
*/
|
||||
@Override
|
||||
public String keyPre() {
|
||||
return "cart:info:";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public Long decode(String redisKey) {
|
||||
return Convert.toLong(redisKey.replace(keyPre(),""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param hashKey ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encodeHashKey(CartHashKey hashKey){
|
||||
return hashKey.getProjectId()+":"+hashKey.getProjectSku();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisHashKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public CartHashKey decodeHashKey(String redisHashKey) {
|
||||
String[] split = redisHashKey.split(":");
|
||||
return CartHashKey.builder()
|
||||
.projectId(Convert.toLong(split[0]))
|
||||
.projectSku(split[1])
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData(Long key) {
|
||||
return cartData.getData(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CartInfo getData(Long key, CartHashKey hashKey) {
|
||||
return cartData.getData(key,hashKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> defaultValue() {
|
||||
throw new ServiceException("购物车无数据", Result.SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CartInfo defaultHashValue() {
|
||||
throw new ServiceException("购物车无数据",Result.SUCCESS);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.muy.shop.cart.cache.key;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* CartHashKey
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CartHashKey {
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long projectId;
|
||||
|
||||
/**
|
||||
* 商品sku
|
||||
*/
|
||||
private String projectSku;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muy.shop.cart.cache.sourcedata;
|
||||
|
||||
import com.muy.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CartData
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
public interface CartData {
|
||||
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Map<CartHashKey, CartInfo> getData(Long key);
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取1hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash值
|
||||
* @return hash值
|
||||
*/
|
||||
public CartInfo getData(Long key,CartHashKey hashKey);
|
||||
}
|
|
@ -24,6 +24,11 @@
|
|||
<artifactId>muyu-shop-cart-common</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-cache</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.shop.cart.service.sourcedata;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.muy.shop.cart.cache.key.CartHashKey;
|
||||
import com.muy.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.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* CartDataImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
@Service
|
||||
public class CartDataImpl implements CartData{
|
||||
@Autowired
|
||||
private CartInfoService cartInfoService;
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData(Long key) {
|
||||
LambdaQueryWrapper<CartInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CartInfo::getUserId, key);
|
||||
List<CartInfo> cartInfoList = cartInfoService.list(queryWrapper);
|
||||
return cartInfoList.stream()
|
||||
.collect(Collectors.toMap(cartInfo -> CartHashKey.builder().projectId(cartInfo.getProjectId()).projectSku(cartInfo.getProjectSku()).build(), cartInfo -> cartInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash值
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public CartInfo getData(Long key, CartHashKey hashKey) {
|
||||
LambdaQueryWrapper<CartInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CartInfo::getUserId,key);
|
||||
queryWrapper.eq(CartInfo::getProjectId,hashKey.getProjectId());
|
||||
return cartInfoService.getOne(queryWrapper);
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
<module>muyu-shop-cart-common</module>
|
||||
<module>muyu-shop-cart-remote</module>
|
||||
<module>muyu-shop-cart-server</module>
|
||||
<module>muyu-shop-cart-cache</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
8
pom.xml
8
pom.xml
|
@ -243,8 +243,12 @@
|
|||
<artifactId>muyu-product-remote</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 购物车模块 缓存 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
Loading…
Reference in New Issue