• redis 秒杀


    使用Lua脚本来实现,因为Redis是单线程的,又是C语言编写的,可以使用Lua调用Redis的命令,Lua会具有排他性,所以能够保证安全。

    使用Lua脚本来实现,因为Redis是单线程的,又是C语言编写的,可以使用Lua调用Redis的命令,Lua会具有排他性,所以能够保证安全。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    package com.redis.secondskill;
      
    import java.util.HashSet;
    import java.util.Set;
      
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
      
    public class SS2 {
      
     static String luaScript ="local userid=KEYS[1]; " +
     "local prodid=KEYS[2]; " +
     "local qtkey='sec:'..prodid..":count"; " +
     "local usersKey='sec:'..prodid..":user"; " +
     "local userExists=redis.call("sismember",usersKey,userid); " +
     "if tonumber(userExists)==1 then " +
     " return 2; " +
     "end " +
     "local num = redis.call("get" ,qtkey); " +
     "if tonumber(num)<=0 then " +
     " return 0; " +
     "else " +
     " redis.call("decr",qtkey); " +
     " redis.call("sadd",usersKey,userid); " +
     "end " +
     "return 1" ;
      
     public static boolean doSecKill(String uid,String prodid) {
     JedisPool jedisPool = JedisPollTool.getInstance();
     Jedis jedis = jedisPool.getResource();
     String sha1 = jedis.scriptLoad(luaScript);
      
     Object result= jedis.evalsha(sha1, 2, uid,prodid);
      
     String reString=String.valueOf(result);
     if ("0".equals( reString ) ) {
     System.err.println("已抢空!!");
     }else if("1".equals( reString ) ) {
     System.out.println(uid + "抢购成功!!!!");
     }else if("2".equals( reString ) ) {
     System.err.println("该用户已抢过!!");
     }else{
     System.err.println("抢购异常!!");
     }
     JedisPollTool.distroy(jedisPool, jedis);
     return true;
      
     }
    }

    使用Lua脚本来实现,因为Redis是单线程的,又是C语言编写的,可以使用Lua调用Redis的命令,Lua会具有排他性,所以能够保证安全。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    package com.redis.secondskill;
      
    import java.util.HashSet;
    import java.util.Set;
      
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
      
    public class SS2 {
      
     static String luaScript ="local userid=KEYS[1]; " +
     "local prodid=KEYS[2]; " +
     "local qtkey='sec:'..prodid..":count"; " +
     "local usersKey='sec:'..prodid..":user"; " +
     "local userExists=redis.call("sismember",usersKey,userid); " +
     "if tonumber(userExists)==1 then " +
     " return 2; " +
     "end " +
     "local num = redis.call("get" ,qtkey); " +
     "if tonumber(num)<=0 then " +
     " return 0; " +
     "else " +
     " redis.call("decr",qtkey); " +
     " redis.call("sadd",usersKey,userid); " +
     "end " +
     "return 1" ;
      
     public static boolean doSecKill(String uid,String prodid) {
     JedisPool jedisPool = JedisPollTool.getInstance();
     Jedis jedis = jedisPool.getResource();
     String sha1 = jedis.scriptLoad(luaScript);
      
     Object result= jedis.evalsha(sha1, 2, uid,prodid);
      
     String reString=String.valueOf(result);
     if ("0".equals( reString ) ) {
     System.err.println("已抢空!!");
     }else if("1".equals( reString ) ) {
     System.out.println(uid + "抢购成功!!!!");
     }else if("2".equals( reString ) ) {
     System.err.println("该用户已抢过!!");
     }else{
     System.err.println("抢购异常!!");
     }
     JedisPollTool.distroy(jedisPool, jedis);
     return true;
      
     }
    }
    如果有来生,要做一片树叶。 春天恋上枝,炎夏恋上水。 深秋恋上土,东来化作泥。 润物细无声,生生世世恋红尘。
  • 相关阅读:
    [POI2014]KUR-Couriers
    [题解向] Luogu4092 [HEOI2016/TJOI2016]树
    [探究] OI中各种初级数论算法相关
    [SCOI2005]骑士精神
    [intoj#7]最短距离
    数列分块入门
    动态规划问题基础
    Luogu P1967 货车运输
    Luogu P3379 【模板】最近公共祖先(LCA)
    Luogu P3378 【模板】堆
  • 原文地址:https://www.cnblogs.com/shujiying/p/14671674.html
Copyright © 2020-2023  润新知