redis事務(wù)如何解決超賣問題
redis事務(wù)解決超賣問題
Redis的事務(wù)提供了一種將多個(gè)命令請(qǐng)求打包,然后一次性、按順序性地執(zhí)行多個(gè)命令的機(jī)制。
在事務(wù)執(zhí)行期間,服務(wù)器不會(huì)中斷事務(wù)而去執(zhí)行其它客戶端的命令請(qǐng)求,它會(huì)將事務(wù)中的所有命令執(zhí)行完畢,然后才去處理其它客戶端的命令請(qǐng)求。
事務(wù)以MULTI命令開始,然后將多個(gè)命令放到事務(wù)當(dāng)中,最后由EXEC命令將這個(gè)事務(wù)提交給服務(wù)器執(zhí)行。
1.引入相關(guān)jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.0</version>
</dependency>2.代碼段
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @author lucifer
* @description TODO
* @date 2022-08-10
*/
@RestController
public class Controller {
@Autowired
RedisTemplate redisTemplate;
//寫入緩存中,因?yàn)檫@里的模擬的是一個(gè)商品被多人搶,所以value值隨便吧
@GetMapping("/test1")
public void test1(){
redisTemplate.opsForValue().set("item1",UUID.randomUUID().toString());
}
//模擬多人搶一個(gè)商品,并且只有一件
@GetMapping("/test")
public String test(){
//生成隨機(jī)的userid(模擬多用戶去搶一個(gè)商品)
String userid=UUID.randomUUID().toString();
//redis key 商品id 為了模擬寫成1
String key="item"+1;
//如果redis中不存在搶這個(gè)商品的緩存,就代表搶失敗
//商品獨(dú)一份
if(!redisTemplate.hasKey(key)){
throw new RuntimeException("你沒有搶到");
}
//執(zhí)行redis的事務(wù)
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
//在使用multi()開始的事務(wù)期間觀察給定的修改key
operations.watch(key);
//標(biāo)記事務(wù)塊的開始。 命令將被排隊(duì)
operations.multi();
//設(shè)置key-value
operations.opsForValue().set(key,userid);
//如果任何被監(jiān)視的key已被修改,則操作將失敗
return operations.exec();
}
});
//刪除 避免這個(gè)商品被其他人搶到了
redisTemplate.delete(key);
//todo....數(shù)據(jù)庫(kù)操作
return "你搶到了";
}
}3.測(cè)試
用50個(gè)線程并發(fā)去調(diào)用接口,模擬多人并發(fā)搶商品的功能;


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用
這篇文章主要介紹了Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用,講解了Redis在進(jìn)程和線程的調(diào)度上以及消息隊(duì)列中的作用,需要的朋友可以參考下2016-03-03
redis cluster集群模式下實(shí)現(xiàn)批量可重入鎖
本文主要介紹了使用redis cluster集群版所遇到的問題解決方案及redis可重入鎖是否會(huì)有死鎖的問題等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Redis在項(xiàng)目中的使用(JedisPool方式)
項(xiàng)目操作redis是使用的RedisTemplate方式,另外還可以完全使用JedisPool和Jedis來操作redis,本文給大家介紹Redis在項(xiàng)目中的使用,JedisPool方式,感興趣的朋友跟隨小編一起看看吧2021-12-12
Spring Boot整合Redis實(shí)現(xiàn)訂單超時(shí)處理問題
這篇文章主要介紹了Spring Boot整合Redis實(shí)現(xiàn)訂單超時(shí)處理,通過這個(gè)基本的示例,你可以了解如何使用Spring Boot和Redis來處理訂單超時(shí)問題,并根據(jù)需要進(jìn)行擴(kuò)展和定制,需要的朋友可以參考下2023-11-11
基于Redis的分布式鎖的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了基于Redis的分布式鎖的簡(jiǎn)單實(shí)現(xiàn)方法,Redis官方給出兩種思路,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10

