master
DongZeLiang 2024-04-02 10:58:54 +08:00
parent 3e26e9bcd6
commit 6800b917bb
7 changed files with 177 additions and 4 deletions

View File

@ -9,6 +9,13 @@ import com.muyu.common.cache.decoration.DecorationKey;
*/ */
public interface AtomicSequenceCache<K> extends DecorationKey<K> { public interface AtomicSequenceCache<K> extends DecorationKey<K> {
/**
*
* @param key
* @return
*/
public Long get(K key);
/** /**
* *
*/ */

View File

@ -14,6 +14,16 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
/**
*
* @param key
* @return
*/
@Override
public Long get (K key) {
return this.redisService.getCacheObject(encode(key));
}
/** /**
* *
* @param key * @param key
@ -41,7 +51,13 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
*/ */
@Override @Override
public Long increment (K key, Long number) { 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 @Override
public Long decrement (K key, Long number) { 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 @Override
public String encode (K key) { public String encode (K key) {
return null; return keyPre() + key;
} }
public abstract Long getData(K key);
} }

View File

@ -255,4 +255,23 @@ public class RedisService {
public Collection<String> keys (final String pattern) { public Collection<String> keys (final String pattern) {
return redisTemplate.keys(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);
}
} }

View File

@ -1,9 +1,61 @@
package com.muyu.product.cache; 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 * @author DongZl
* @description: SKU * @description: SKU
* @Date 2024-3-29 03:06 * @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();
}
} }

View File

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

View File

@ -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: SKUKey
* @Date 2024-4-2 10:41
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SkuStockKey {
/**
* ID
*/
private Long projectId;
/**
* sku
*/
private String sku;
}

View File

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