购物车缓存

master
baize 2024-04-02 19:02:39 +08:00
parent ba01e230ef
commit 2155eeab10
10 changed files with 260 additions and 5 deletions

View File

@ -39,6 +39,10 @@ public class Result<T> implements Serializable {
private T data; 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 () { public static <T> Result<T> success () {
return restResult(null, SUCCESS, null); return restResult(null, SUCCESS, null);
} }
@ -71,8 +75,6 @@ public class Result<T> implements Serializable {
return restResult(null, code, msg); return restResult(null, code, msg);
} }
public static <T> Result<T> warn () { public static <T> Result<T> warn () {
return restResult(null, WARN, null); return restResult(null, WARN, null);
} }

View File

@ -66,7 +66,7 @@ public class GlobalExceptionHandler {
public Result handleServiceException (ServiceException e, HttpServletRequest request) { public Result handleServiceException (ServiceException e, HttpServletRequest request) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
Integer code = e.getCode(); 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

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

View File

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

View File

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

View File

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

View File

@ -24,6 +24,11 @@
<artifactId>muyu-shop-cart-common</artifactId> <artifactId>muyu-shop-cart-common</artifactId>
<version>${muyu.version}</version> <version>${muyu.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- SpringCloud Alibaba Nacos --> <!-- SpringCloud Alibaba Nacos -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>

View File

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

View File

@ -15,6 +15,7 @@
<module>muyu-shop-cart-common</module> <module>muyu-shop-cart-common</module>
<module>muyu-shop-cart-remote</module> <module>muyu-shop-cart-remote</module>
<module>muyu-shop-cart-server</module> <module>muyu-shop-cart-server</module>
<module>muyu-shop-cart-cache</module>
</modules> </modules>
<properties> <properties>

View File

@ -243,8 +243,12 @@
<artifactId>muyu-product-remote</artifactId> <artifactId>muyu-product-remote</artifactId>
<version>${muyu.version}</version> <version>${muyu.version}</version>
</dependency> </dependency>
<!-- 购物车模块 缓存 依赖-->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart</artifactId>
<version>${muyu.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>