Exemplo n.º 1
0
 @RequestMapping("/login")
 @ResponseBody
 public Response login(@RequestBody Map<String, Object> map) {
   Response res = new Response();
   List<User> users = userService.login(map);
   if (users == null || users.size() == 0) {
     return res.failure("登陆失败");
   }
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     String skey = "session:" + users.get(0).getId();
     String token = UUID.randomUUID().toString();
     if (shardedJedis.exists(skey)) {
       String oldtoken = shardedJedis.get(skey);
       shardedJedis.del(skey);
       shardedJedis.del("token:" + oldtoken);
     }
     if (shardedJedis.setnx(skey, token) == 1) {
       shardedJedis.expire(skey, 3600 * 24);
       shardedJedis.hset("token:" + token, "name", users.get(0).getName());
       shardedJedis.expire("token:" + token, 3600 * 24);
     } else {
       token = shardedJedis.get(skey);
     }
     Map<String, Object> data = new HashMap<>();
     data.put("token", token);
     return new Response().success(data);
   } finally {
     shardedJedis.close();
   }
 }
Exemplo n.º 2
0
 /**
  * 设置key value,如果key已经存在则返回0,nx==> not exist
  *
  * @param key
  * @param value
  * @return 成功返回1 如果存在 和 发生异常 返回 0
  */
 public Long setnx(String key, String value) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.setnx(key, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return 0L;
   } finally {
     returnResource(pool, jedis);
   }
 }