master
parent
3e26e9bcd6
commit
6800b917bb
|
@ -9,6 +9,13 @@ import com.muyu.common.cache.decoration.DecorationKey;
|
|||
*/
|
||||
public interface AtomicSequenceCache<K> extends DecorationKey<K> {
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public Long get(K key);
|
||||
|
||||
/**
|
||||
* 自增
|
||||
*/
|
||||
|
|
|
@ -14,6 +14,16 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public Long get (K key) {
|
||||
return this.redisService.getCacheObject(encode(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增
|
||||
* @param key
|
||||
|
@ -41,7 +51,13 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
*/
|
||||
@Override
|
||||
public Long increment (K key, Long number) {
|
||||
return null;
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.increment(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,7 +68,13 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
*/
|
||||
@Override
|
||||
public Long decrement (K key, Long number) {
|
||||
return null;
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.decrement(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,6 +86,8 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
*/
|
||||
@Override
|
||||
public String encode (K key) {
|
||||
return null;
|
||||
return keyPre() + key;
|
||||
}
|
||||
|
||||
public abstract Long getData(K key);
|
||||
}
|
||||
|
|
|
@ -255,4 +255,23 @@ public class RedisService {
|
|||
public Collection<String> keys (final String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少序列值
|
||||
* @param key key
|
||||
* @param number 值
|
||||
* @return 操作后的值
|
||||
*/
|
||||
public Long decrement (final String key, Long number) {
|
||||
return redisTemplate.opsForValue().decrement(key,number);
|
||||
}
|
||||
/**
|
||||
* 增加序列值
|
||||
* @param key key
|
||||
* @param number 值
|
||||
* @return 操作后的值
|
||||
*/
|
||||
public Long increment (final String key, Long number) {
|
||||
return redisTemplate.opsForValue().increment(key,number);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,61 @@
|
|||
package com.muyu.product.cache;
|
||||
|
||||
import com.muyu.common.cache.abs.AtomicSequenceCacheAbs;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuStockData;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 商品SKU库存缓存
|
||||
* @Date 2024-3-29 下午 03:06
|
||||
*/
|
||||
public class ProjectSkuStockCache {
|
||||
@Service
|
||||
public class ProjectSkuStockCache extends AtomicSequenceCacheAbs<SkuStockKey> {
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuStockData projectSkuStockData;
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
*
|
||||
* @return key前缀
|
||||
*/
|
||||
@Override
|
||||
public String keyPre () {
|
||||
return "project:sku:stock:";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param skuStockKey ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode (SkuStockKey skuStockKey) {
|
||||
return keyPre() + skuStockKey.getProjectId() + ":" + skuStockKey.getSku();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getData (SkuStockKey key) {
|
||||
return projectSkuStockData.getData(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public SkuStockKey decode (String redisKey) {
|
||||
String[] split = redisKey.replace(keyPre(), "").split(":");
|
||||
return SkuStockKey.builder()
|
||||
.projectId(Convert.toLong(split[0]))
|
||||
.sku(split[1])
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.product.cache.datasource;
|
||||
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: SKU库存
|
||||
* @Date 2024-4-2 上午 10:52
|
||||
*/
|
||||
public interface ProjectSkuStockData {
|
||||
|
||||
public Long getData (SkuStockKey key) ;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.muyu.product.cache.key;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 商品SKU库存Key
|
||||
* @Date 2024-4-2 上午 10:41
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SkuStockKey {
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long projectId;
|
||||
|
||||
/**
|
||||
* sku
|
||||
*/
|
||||
private String sku;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.product.cache.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuStockData;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.service.ProjectSkuInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: sku库存实现类
|
||||
* @Date 2024-4-2 上午 10:53
|
||||
*/
|
||||
@Service
|
||||
public class ProjectSkuStockDataImpl implements ProjectSkuStockData {
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuInfoService projectSkuInfoService;
|
||||
|
||||
@Override
|
||||
public Long getData (SkuStockKey key) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId, key.getProjectId());
|
||||
queryWrapper.eq(ProjectSkuInfo::getSku, key.getSku());
|
||||
ProjectSkuInfo projectSkuInfo = projectSkuInfoService.getOne(queryWrapper);
|
||||
return projectSkuInfo.getStock();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue