-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7db7bd1
commit fca2e1e
Showing
9 changed files
with
295 additions
and
118 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/daydreamdev/secondskill/common/StockWithRedis/UpdateStockWithRedis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.daydreamdev.secondskill.common.StockWithRedis; | ||
|
||
import com.daydreamdev.secondskill.common.RedisKeysConstant; | ||
import com.daydreamdev.secondskill.common.utils.RedisPool; | ||
import com.daydreamdev.secondskill.common.utils.RedisPoolUtil; | ||
import com.daydreamdev.secondskill.pojo.Stock; | ||
import lombok.extern.slf4j.Slf4j; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.Transaction; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @auther G.Fukang | ||
* @date 6/8 21:47 | ||
*/ | ||
@Slf4j | ||
public class UpdateStockWithRedis { | ||
|
||
/** | ||
* Redis 事务保证库存更新 | ||
* 捕获异常后应该删除缓存 | ||
*/ | ||
public static void updateStockWithRedis(Stock stock) { | ||
Jedis jedis = null; | ||
try { | ||
jedis = RedisPool.getJedis(); | ||
// 开始事务 | ||
Transaction transaction = jedis.multi(); | ||
// 事务操作 | ||
RedisPoolUtil.decr(RedisKeysConstant.STOCK_COUNT + stock.getId()); | ||
RedisPoolUtil.incr(RedisKeysConstant.STOCK_SALE + stock.getId()); | ||
RedisPoolUtil.incr(RedisKeysConstant.STOCK_VERSION + stock.getId()); | ||
// 结束事务 | ||
List<Object> list = transaction.exec(); | ||
} catch (Exception e) { | ||
log.error("updateStock 获取 Jedis 实例失败:", e); | ||
} finally { | ||
RedisPool.jedisPoolClose(jedis); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters