-- 函数:尝试获得红包,如果成功,则返回json字符串,如果不成功,则返回空
-- 参数:红包队列名, 已消费的队列名,去重的Map名,用户ID
-- 返回值:nil 或者 json字符串,包含用户ID:userId,红包ID:id,红包金额:money
-- 如果用户已抢过红包,则返回nil
if rediscall('hexists', KEYS[3], KEYS[4]) ~= 0 then
return nil
else
-- 先取出一个小红包
local hongBao = rediscall('rpop', KEYS[1]);
if hongBao then
local x = cjsondecode(hongBao);
-- 加入用户ID信息
x['userId'] = KEYS[4];
local re = cjsonencode(x);
-- 把用户ID放到去重的set里
rediscall('hset', KEYS[3], KEYS[4], KEYS[4]);
-- 把红包放到已消费队列里
rediscall('lpush', KEYS[2], re);
return re;
end
end
return nil
public class TestEval {
static String host = "localhost";
static int honBaoCount = 1_0_0000;
static int threadCount = 20;
static String hongBaoList = "hongBaoList";
static String hongBaoConsumedList = "hongBaoConsumedList";
static String hongBaoConsumedMap = "hongBaoConsumedMap";
static Random random = new Random();
// -- 函数:尝试获得红包,如果成功,则返回json字符串,如果不成功,则返回空
// -- 参数:红包队列名, 已消费的队列名,去重的Map名,用户ID
// -- 返回值:nil 或者 json字符串,包含用户ID:userId,红包ID:id,红包金额:money
static String tryGetHongBaoScript =
// "local bConsumed = rediscall('hexists', KEYS[3], KEYS[4]);n"
// + "print('bConsumed:' ,bConsumed);n"
"if rediscall('hexists', KEYS[3], KEYS[4]) ~= 0 thenn"
+ "return niln"
+ "elsen"
+ "local hongBao = rediscall('rpop', KEYS[1]);n"
// + "print('hongBao:', hongBao);n"
+ "if hongBao thenn"
+ "local x = cjsondecode(hongBao);n"
+ "x['userId'] = KEYS[4];n"
+ "local re = cjsonencode(x);n"
+ "rediscall('hset', KEYS[3], KEYS[4], KEYS[4]);n"
+ "rediscall('lpush', KEYS[2], re);n"
+ "return re;n"
+ "endn"
+ "endn"
+ "return nil";
static StopWatch watch = new StopWatch();
public static void main(String[] args) throws InterruptedException {
// testEval();
generateTestData();
testTryGetHongBao();
}
static public void generateTestData() throws InterruptedException {
Jedis jedis = new Jedis(host);
jedisflushAll();
final CountDownLatch latch = new CountDownLatch(threadCount);
for(int i = 0; i < threadCount; ++i) {
final int temp = i;
Thread thread = new Thread() {
public void run() {
Jedis jedis = new Jedis(host);
int per = honBaoCount/threadCount;
JSONObject object = new JSONObject();
for(int j = temp * per; j < (temp+1) * per; j++) {
objectput("id", j);
objectput("money", j);
jedislpush(hongBaoList, objecttoJSONString());
}
latchcountDown();
}
};
threadstart();
}
latchawait();
}
static public void testTryGetHongBao() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(threadCount);
Systemerrprintln("start:" + SystemcurrentTimeMillis()/1000);
watchstart();
for(int i = 0; i < threadCount; ++i) {
final int temp = i;
Thread thread = new Thread() {
public void run() {
Jedis jedis = new Jedis(host);
String sha = jedisscriptLoad(tryGetHongBaoScript);
int j = honBaoCount/threadCount * temp;
while(true) {
Object object = jediseval(tryGetHongBaoScript, 4, hongBaoList, hongBaoConsumedList, hongBaoConsumedMap, "" + j);
j++;
if (object != null) {
// Systemoutprintln("get hongBao:" + object);
}else {
//已经取完了
if(jedisllen(hongBaoList) == 0)
break;
}
}
latchcountDown();
}
};
threadstart();
}
latchawait();
watchstop();
Systemerrprintln("time:" + watchgetTotalTimeSeconds());
Systemerrprintln("speed:" + honBaoCount/watchgetTotalTimeSeconds());
Systemerrprintln("end:" + SystemcurrentTimeMillis()/1000);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有