Redis使用:使用Redisson进行库存管理
最近有这么一个需求,预创建商品信息,放入缓存,用的时候取出,直到库存消耗完。
Redis配置:
#数据源配置
spring:
redis:
##redis 单机环境配置
host: 127.0.0.1
port: 6379
password: test
database: 8
ssl: false
Redisson配置类:
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @auther HuWenQing
* @date 2024/8/2
*/
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private Integer database;
@Bean
public RedissonClient redissonClient(){
Config config = new Config();
config.useSingleServer()
.setAddress("redis://" + host + ":" + port)
.setPassword(password)
.setDatabase(database);
return Redisson.create(config);
}
}
模拟的业务逻辑代码如下:
import org.redisson.api.RedissonClient;
import com.alibaba.fastjson.JSONArray;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import java.util.concurrent.TimeUnit;
@Autowired
private RedissonClient redissonClient;
public void testRedisson(){
//生成产品数量
JSONArray array = new JSONArray();
for(long i=0L; i<10L; i++){
EqtProduct product = new EqtProduct();
product.setId(i);
array.add(product);
}
RBucket<String> bucket = redissonClient.getBucket("testRedisson");
bucket.set(array.toString());
//模拟调用
for(int i=0; i<=10; i++){
new Thread(() -> System.out.println(getProduct())).start();
}
}
private EqtProduct getProduct(){
long t1 = System.currentTimeMillis();
String threadName = Thread.currentThread().getName();
EqtProduct product = null;
RLock lock = redissonClient.getLock("testRedissonLock");
try {
if(!lock.tryLock(5, 5, TimeUnit.SECONDS)){
log.info(threadName + "超时获取锁失败");
throw new RuntimeException(threadName + "超时获取锁失败");
}
RBucket<String> bucket = redissonClient.getBucket("testRedisson");
String productStr = bucket.get();
if(StringUtils.isNotEmpty(productStr)) {
JSONArray array = JSONArray.parseArray(productStr);
if(array.size() > 0){
JSONObject productJson = (JSONObject) array.remove(array.size()-1);
product = JSONObject.parseObject(productJson.toString(), EqtProduct.class);
bucket.set(array.toString());
log.info(threadName + "获取商品成功");
}
}
}catch (Exception e){
e.printStackTrace();
log.info("获取商品失败");
}finally {
if(lock.isHeldByCurrentThread()){
lock.unlock();
}
long t2 = System.currentTimeMillis();
log.info(threadName + "释放锁;处理耗时" + (t2-t1));
}
return product;
}
打印结果如下:
2024-08-05 19:10:06.346 INFO 18184 --- [ Thread-105] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-105获取商品成功
2024-08-05 19:10:06.412 INFO 18184 --- [ Thread-105] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-105释放锁;处理耗时658
EqtProduct(id=9, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:06.514 INFO 18184 --- [ Thread-104] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-104获取商品成功
2024-08-05 19:10:06.578 INFO 18184 --- [ Thread-104] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-104释放锁;处理耗时825
EqtProduct(id=8, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:06.680 INFO 18184 --- [ Thread-96] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-96获取商品成功
2024-08-05 19:10:06.733 INFO 18184 --- [ Thread-96] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-96释放锁;处理耗时981
EqtProduct(id=7, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:06.804 INFO 18184 --- [ Thread-94] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-94获取商品成功
2024-08-05 19:10:06.857 INFO 18184 --- [ Thread-94] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-94释放锁;处理耗时1105
EqtProduct(id=6, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:06.933 INFO 18184 --- [ Thread-93] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-93获取商品成功
2024-08-05 19:10:06.992 INFO 18184 --- [ Thread-93] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-93释放锁;处理耗时1240
EqtProduct(id=5, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.073 INFO 18184 --- [ Thread-98] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-98获取商品成功
2024-08-05 19:10:07.138 INFO 18184 --- [ Thread-98] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-98释放锁;处理耗时1386
EqtProduct(id=4, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.247 INFO 18184 --- [ Thread-103] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-103获取商品成功
2024-08-05 19:10:07.305 INFO 18184 --- [ Thread-103] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-103释放锁;处理耗时1552
EqtProduct(id=3, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.408 INFO 18184 --- [ Thread-91] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-91获取商品成功
2024-08-05 19:10:07.466 INFO 18184 --- [ Thread-91] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-91释放锁;处理耗时1715
EqtProduct(id=2, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.562 INFO 18184 --- [ Thread-100] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-100获取商品成功
2024-08-05 19:10:07.623 INFO 18184 --- [ Thread-100] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-100释放锁;处理耗时1871
EqtProduct(id=1, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.705 INFO 18184 --- [ Thread-106] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-106获取商品成功
2024-08-05 19:10:07.772 INFO 18184 --- [ Thread-106] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-106释放锁;处理耗时2019
EqtProduct(id=0, productCode=null, createTime=null, updateTime=null, productName=null, status=null, type=null, redeemDays=null, customJson=null, appIds=null, operatorName=null, deptId=null, userId=null, isDeleted=null, isEnableSms=null, sendType=null, sendContent=null)
2024-08-05 19:10:07.881 INFO 18184 --- [ Thread-101] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-101释放锁;处理耗时2129
null
2024-08-05 19:10:07.998 INFO 18184 --- [ Thread-95] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-95释放锁;处理耗时2246
null
2024-08-05 19:10:08.093 INFO 18184 --- [ Thread-92] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-92释放锁;处理耗时2342
null
2024-08-05 19:10:08.185 INFO 18184 --- [ Thread-99] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-99释放锁;处理耗时2433
null
2024-08-05 19:10:08.289 INFO 18184 --- [ Thread-102] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-102释放锁;处理耗时2537
null
2024-08-05 19:10:08.394 INFO 18184 --- [ Thread-97] o.s.m.e.s.i.EqtEquityOrderServiceImpl : Thread-97释放锁;处理耗时2643
null
测试100条,大概需要120到150毫秒获取一次商品,性能方面应该还有待加强
主要参考文章:
https://blog.csdn.net/qq_40592590/article/details/140602212
https://blog.csdn.net/Xmi2599/article/details/130844102
在此感谢作者