Example #1
0
 /**
  * Description: <br>
  *
  * @author 王伟<br>
  * @taskId <br>
  * @param nodeName
  * @param key
  * @throws CacheException <br>
  */
 @Override
 public void removeValue(String nodeName, String key) throws CacheException {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = shardedPool.getResource();
     shardedJedis.hdel(nodeName.getBytes(), key.getBytes());
   } catch (Exception e) {
     throw new CacheException(ErrorCodeDef.CACHE_ERROR_10002, "serial map failed!", e);
   } finally {
     shardedPool.returnResourceObject(shardedJedis);
   }
 }
 /**
  * 删除指定的key,也可以传入一个包含key的数组
  *
  * @param keys 一个key 也可以使 string 数组
  * @return 返回删除成功的个数
  */
 public Long del(String key) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.del(key);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return 0L;
   } finally {
     returnResource(pool, jedis);
   }
 }
 /**
  * 通过key 和offset 从指定的位置开始将原先value替换
  *
  * <p>下标从0开始,offset表示从offset下标开始替换
  *
  * <p>如果替换的字符串长度过小则会这样
  *
  * <p>example:
  *
  * <p>value : [email protected]
  *
  * <p>str : abc
  *
  * <p>从下标7开始替换 则结果为
  *
  * <p>RES : bigsea.abc.cn
  *
  * @param key
  * @param str
  * @param offset 下标位置
  * @return 返回替换后 value 的长度
  */
 public Long setrange(String key, String str, int offset) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.setrange(key, offset, str);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return 0L;
   } finally {
     returnResource(pool, jedis);
   }
 }
 /**
  * 判断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);
   }
 }
 /**
  * 向redis存入key和value,并释放连接资源
  *
  * <p>如果key已经存在 则覆盖
  *
  * @param key
  * @param value
  * @return 成功 返回OK 失败返回 0
  */
 public String set(String key, String value) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.set(key, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return "0";
   } finally {
     returnResource(pool, jedis);
   }
 }
 /**
  * 通过key向zset中添加value,score,其中score就是用来排序的
  *
  * <p>如果该value已经存在则根据score更新元素
  *
  * @param key
  * @param scoreMembers
  * @return
  */
 public Long zadd(String key, Map<String, Double> scoreMembers) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zadd(key, scoreMembers);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key给指定的field的value加上给定的值
  *
  * @param key
  * @param field
  * @param value
  * @return
  */
 public Long hincrby(String key, String field, Long value) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.hincrBy(key, field, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key 和 fields 获取指定的value 如果没有对应的value则返回null
  *
  * @param key
  * @param fields 可以使 一个String 也可以是 String数组
  * @return
  */
 public List<String> hmget(String key, String... fields) {
   ShardedJedis jedis = null;
   List<String> res = null;
   try {
     jedis = pool.getResource();
     res = jedis.hmget(key, fields);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key判断value是否是set中的元素
  *
  * @param key
  * @param member
  * @return
  */
 public Boolean sismember(String key, String member) {
   ShardedJedis jedis = null;
   Boolean res = null;
   try {
     jedis = pool.getResource();
     res = jedis.sismember(key, member);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key获取list指定下标位置的value
  *
  * <p>如果start 为 0 end 为 -1 则返回全部的list中的value
  *
  * @param key
  * @param start
  * @param end
  * @return
  */
 public List<String> lrange(String key, long start, long end) {
   ShardedJedis jedis = null;
   List<String> res = null;
   try {
     jedis = pool.getResource();
     res = jedis.lrange(key, start, end);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key在list指定的位置之前或者之后 添加字符串元素
  *
  * @param key
  * @param where LIST_POSITION枚举类型
  * @param pivot list里面的value
  * @param value 添加的value
  * @return
  */
 public Long linsert(String key, LIST_POSITION where, String pivot, String value) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.linsert(key, where, pivot, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 设置key value并制定这个键值的有效期
  *
  * @param key
  * @param value
  * @param seconds 单位:秒
  * @return 成功返回OK 失败和异常返回null
  */
 public String setex(String key, String value, int seconds) {
   ShardedJedis jedis = null;
   String res = null;
   try {
     jedis = pool.getResource();
     res = jedis.setex(key, seconds, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key删除指定score内的元素
  *
  * @param key
  * @param start
  * @param end
  * @return
  */
 public Long zremrangeByScore(String key, double start, double end) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zremrangeByScore(key, start, end);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 返回指定区间内zset中value的数量
  *
  * @param key
  * @param min
  * @param max
  * @return
  */
 public Long zcount(String key, String min, String max) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zcount(key, min, max);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key返回指定score内zset中的value
  *
  * @param key
  * @param max
  * @param min
  * @return
  */
 public Set<String> zrangeByScore(String key, double max, double min) {
   ShardedJedis jedis = null;
   Set<String> res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zrevrangeByScore(key, max, min);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key增加该zset中value的score的值
  *
  * @param key
  * @param score
  * @param member
  * @return
  */
 public Double zincrby(String key, double score, String member) {
   ShardedJedis jedis = null;
   Double res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zincrby(key, score, member);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key获取所有的field和value
  *
  * @param key
  * @return
  */
 public Map<String, String> hgetall(String key) {
   ShardedJedis jedis = null;
   Map<String, String> res = null;
   try {
     jedis = pool.getResource();
     res = jedis.hgetAll(key);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过下标 和key 获取指定下标位置的 value
  *
  * @param key
  * @param startOffset 开始位置 从0 开始 负数表示从右边开始截取
  * @param endOffset
  * @return 如果没有返回null
  */
 public String getRange(String key, int startOffset, int endOffset) {
   ShardedJedis jedis = null;
   String res = null;
   try {
     jedis = pool.getResource();
     res = jedis.getrange(key, startOffset, endOffset);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key从对应的list中删除指定的count个 和 value相同的元素
  *
  * @param key
  * @param count 当count为0时删除全部
  * @param value
  * @return 返回被删除的个数
  */
 public Long lrem(String key, long count, String value) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.lrem(key, count, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key获取储存在redis中的value
  *
  * <p>并释放连接
  *
  * @param key
  * @return 成功返回value 失败返回null
  */
 public String get(String key) {
   ShardedJedis jedis = null;
   String value = null;
   try {
     jedis = pool.getResource();
     value = jedis.get(key);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return value;
 }
 /**
  * 通过key获取list中指定下标位置的value
  *
  * @param key
  * @param index
  * @return 如果没有返回null
  */
 public String lindex(String key, long index) {
   ShardedJedis jedis = null;
   String res = null;
   try {
     jedis = pool.getResource();
     res = jedis.lindex(key, index);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 减去指定的值
  *
  * @param key
  * @param integer
  * @return
  */
 public Long decrBy(String key, Long integer) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.decrBy(key, integer);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key删除set中对应的value值
  *
  * @param key
  * @param members 可以是一个String 也可以是一个String数组
  * @return 删除的个数
  */
 public Long srem(String key, String... members) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.srem(key, members);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key 和 field 获取指定的 value
  *
  * @param key
  * @param field
  * @return 没有返回null
  */
 public String hget(String key, String field) {
   ShardedJedis jedis = null;
   String res = null;
   try {
     jedis = pool.getResource();
     res = jedis.hget(key, field);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key和field判断是否有指定的value存在
  *
  * @param key
  * @param field
  * @return
  */
 public Boolean hexists(String key, String field) {
   ShardedJedis jedis = null;
   Boolean res = false;
   try {
     jedis = pool.getResource();
     res = jedis.hexists(key, field);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 /**
  * 通过key获取set中随机的value,不删除元素
  *
  * @param key
  * @return
  */
 public String srandmember(String key) {
   ShardedJedis jedis = null;
   String res = null;
   try {
     jedis = pool.getResource();
     res = jedis.srandmember(key);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
  public String get(String key) {
    String value = null;
    ShardedJedis jedis = null;
    try {
      pool = getPool();
      jedis = pool.getResource();
      value = jedis.get(key);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      pool.close();
    }

    return value;
  }
 /**
  * 通过key向指定的value值追加值
  *
  * @param key
  * @param str
  * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L
  */
 public Long append(String key, String str) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.append(key, str);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return 0L;
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
Example #29
0
 /**
  * Description: <br>
  *
  * @author 王伟<br>
  * @taskId <br>
  * @param nodeName
  * @param key
  * @param t
  * @throws CacheException <br>
  */
 @Override
 public <T> void putValue(String nodeName, String key, T t) throws CacheException {
   if (t != null) {
     ShardedJedis shardedJedis = null;
     try {
       shardedJedis = shardedPool.getResource();
       shardedJedis.hset(nodeName.getBytes(), key.getBytes(), SerializationUtil.serial(t));
     } catch (UtilException e) {
       throw new CacheException(e);
     } catch (Exception e) {
       throw new CacheException(ErrorCodeDef.CACHE_ERROR_10002, "serial map failed!", e);
     } finally {
       shardedPool.returnResourceObject(shardedJedis);
     }
   }
 }
 @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();
   }
 }