@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();
   }
 }
 @Override
 public Boolean exists(String key) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.exists(key);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return Boolean.FALSE;
 }
 /**
  * 判断key是否存在
  *
  * @param key
  * @return true OR false
  */
 public Boolean exists(String key) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.exists(key);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return false;
   } finally {
     returnResource(pool, jedis);
   }
 }
Esempio n. 4
0
 private void KeyOperate() {
   System.out.println("======================key==========================");
   // 清空数据
   System.out.println("清空库中所有数据:" + jedis.flushDB());
   // 判断key否存在
   System.out.println("判断key999键是否存在:" + shardedJedis.exists("key999"));
   System.out.println("新增key001,value001键值对:" + shardedJedis.set("key001", "value001"));
   System.out.println("判断key001是否存在:" + shardedJedis.exists("key001"));
   // 输出系统中所有的key
   System.out.println("新增key002,value002键值对:" + shardedJedis.set("key002", "value002"));
   System.out.println("系统中所有键如下:");
   Set<String> keys = jedis.keys("*");
   Iterator<String> it = keys.iterator();
   while (it.hasNext()) {
     String key = it.next();
     System.out.println(key);
   }
   // 删除某个key,若key不存在,则忽略该命令。
   System.out.println("系统中删除key002: " + jedis.del("key002"));
   System.out.println("判断key002是否存在:" + shardedJedis.exists("key002"));
   // 设置 key001的过期时间
   System.out.println("设置 key001的过期时间为5秒:" + jedis.expire("key001", 5));
   try {
     Thread.sleep(2000);
   } catch (InterruptedException e) {
   }
   // 查看某个key的剩余生存时间,单位【秒】.永久生存或者不存在的都返回-1
   System.out.println("查看key001的剩余生存时间:" + jedis.ttl("key001"));
   // 移除某个key的生存时间
   System.out.println("移除key001的生存时间:" + jedis.persist("key001"));
   System.out.println("查看key001的剩余生存时间:" + jedis.ttl("key001"));
   // 查看key所储存的值的类型
   System.out.println("查看key所储存的值的类型:" + jedis.type("key001"));
   /*
    * 一些其他方法:1、修改键名:jedis.rename("key6", "key0");
    * 2、将当前db的key移动到给定的db当中:jedis.move("foo", 1)
    */
 }
 public void zAdd(String cacheKey, int seconds, Map<Double, String> scoreItems) throws Exception {
   ShardedJedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     if (scoreItems != null && scoreItems.size() > 0) {
       boolean isNew = !jedis.exists(cacheKey);
       jedis.zadd(cacheKey, scoreItems);
       if (isNew && seconds > 0) {
         jedis.expire(cacheKey, seconds);
       }
     }
   } catch (Exception e) {
     throw e;
   } finally {
     if (jedis != null) {
       jedisPool.returnResource(jedis);
     }
   }
 }
 public void zAdd(String cacheKey, int seconds, double score, Object item) throws Exception {
   ShardedJedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     boolean isNew = !jedis.exists(cacheKey);
     String cacheValue = null;
     if (CharSequence.class.isAssignableFrom(item.getClass())) {
       cacheValue = item.toString();
     } else {
       cacheValue = JSON.toJSONString(item);
     }
     jedis.zadd(cacheKey, score, cacheValue);
     if (isNew && seconds > 0) {
       jedis.expire(cacheKey, seconds);
     }
   } catch (Exception e) {
     throw e;
   } finally {
     if (jedis != null) {
       jedisPool.returnResource(jedis);
     }
   }
 }