Redis実戦------javaバージョンコード最適化
4324 ワード
コードリスト4-5 listItem()関数
原版コード:
実際のプロジェクトで実行される改善コード
原版コード:
public boolean listItem(
Jedis conn, String itemId, String sellerId, double price) {
String inventory = "inventory:" + sellerId;
String item = itemId + '.' + sellerId;
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
conn.watch(inventory);
if (!conn.sismember(inventory, itemId)){
conn.unwatch();
return false;
}
Transaction trans = conn.multi();
trans.zadd("market:", price, item);
trans.srem(inventory, itemId);
List
実際のプロジェクトで実行される改善コード
public boolean listItem(Jedis conn, String itemId, String sellerId, double price) {
//1. : key
String inventory=new StringBuffer().append("inventory:").append(sellerId).toString();
String item=new StringBuffer().append(itemId).append(".").append(sellerId).toString();
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
//2.
conn.watch(inventory);
//3.
if (!conn.sismember(inventory, itemId)){
conn.unwatch();
return false;
}
// ,
Transaction trans = conn.multi();
//
trans.zadd("market:", price, item);
//
trans.srem(inventory, itemId);
//
List