@Test(expected = JedisDataException.class)
  public void pipelineResponseWithinPipeline() {
    jedis.set("string", "foo");

    ShardedJedisPipeline p = jedis.pipelined();
    Response<String> string = p.get("string");
    string.get();
    p.sync();
  }
Beispiel #2
0
 public void delAuthCode(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.del(key);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Beispiel #3
0
  private void SortedSetOperate() {
    System.out.println("======================zset==========================");
    // 清空数据
    System.out.println(jedis.flushDB());

    System.out.println("=============增=============");
    System.out.println("zset中添加元素element001:" + shardedJedis.zadd("zset", 7.0, "element001"));
    System.out.println("zset中添加元素element002:" + shardedJedis.zadd("zset", 8.0, "element002"));
    System.out.println("zset中添加元素element003:" + shardedJedis.zadd("zset", 2.0, "element003"));
    System.out.println("zset中添加元素element004:" + shardedJedis.zadd("zset", 3.0, "element004"));
    System.out.println("zset集合中的所有元素:" + shardedJedis.zrange("zset", 0, -1)); // 按照权重值排序
    System.out.println();

    System.out.println("=============删=============");
    System.out.println("zset中删除元素element002:" + shardedJedis.zrem("zset", "element002"));
    System.out.println("zset集合中的所有元素:" + shardedJedis.zrange("zset", 0, -1));
    System.out.println();

    System.out.println("=============改=============");
    System.out.println();

    System.out.println("=============查=============");
    System.out.println("统计zset集合中的元素中个数:" + shardedJedis.zcard("zset"));
    System.out.println("统计zset集合中权重某个范围内(1.0——5.0),元素的个数:" + shardedJedis.zcount("zset", 1.0, 5.0));
    System.out.println("查看zset集合中element004的权重:" + shardedJedis.zscore("zset", "element004"));
    System.out.println("查看下标1到2范围内的元素值:" + shardedJedis.zrange("zset", 1, 2));
  }
Beispiel #4
0
 public void addAuthCode(String key, String authCode) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, authCode);
     shardedJedis.expire(key, Constants.AUTH_CODE_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Beispiel #5
0
 public void addAccessToken(String key, String accessToken) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, accessToken);
     shardedJedis.expire(key, Constants.ACCESS_TOKEN_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Beispiel #6
0
 public String get(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.get(key);
   } catch (Exception ex) {
     logger.error("get error.", ex);
     return null;
   } finally {
     returnResource(shardedJedis);
   }
 }
Beispiel #7
0
 /**
  * 获得排序打分
  *
  * @param key
  * @return
  */
 public Double getScore(String key, String member) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.zscore(key, member);
   } catch (Exception ex) {
     logger.error("getSoredSet error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }
Beispiel #8
0
 public long decr(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     return shardedJedis.decr(key);
   } catch (Exception ex) {
     logger.error("incr error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return 0;
 }
 @Override
 public List<String> hmget(String key, String... fields) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hmget(key, fields);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return new ArrayList<String>(0);
 }
Beispiel #10
0
 public String getAccessTokenByKey(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   String accessToken = "";
   try {
     accessToken = shardedJedis.get(key);
     return accessToken;
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Beispiel #11
0
 public String getAuthCodeByKey(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   String authCode = "";
   try {
     authCode = shardedJedis.get(key);
     return authCode;
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
 @Override
 public Long expire(String key, int seconds) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.expire(key, seconds);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return null;
 }
Beispiel #13
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);
   }
 }
 @Override
 public Boolean hexists(String key, String field) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hexists(key, field);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return Boolean.FALSE;
 }
Beispiel #15
0
 /**
  * 获取List
  *
  * @param key
  * @return
  */
 public List<String> getList(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.lrange(key, 0, -1);
   } catch (Exception ex) {
     logger.error("getList error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }
 @Override
 public Long hset(String key, String field, String value) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hset(key, field, value);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return null;
 }
Beispiel #17
0
 /**
  * 返回 domain 指定的哈希key值总数
  *
  * @param domain
  * @return
  */
 public long lenHset(String domain) {
   ShardedJedis shardedJedis = null;
   long retList = 0;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     retList = shardedJedis.hlen(domain);
   } catch (Exception ex) {
     logger.error("hkeys error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return retList;
 }
Beispiel #18
0
 /**
  * 计算排序长度
  *
  * @param key
  * @param startScore
  * @param endScore
  * @return
  */
 public long countSoredSet(String key, long startScore, long endScore) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     Long count = shardedJedis.zcount(key, startScore, endScore);
     return count == null ? 0L : count;
   } catch (Exception ex) {
     logger.error("countSoredSet error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return 0L;
 }
 /**
  * 判断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);
   }
 }
 public void zDelByRank(String cacheKey, long startIndex, long endIndex) throws Exception {
   ShardedJedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     jedis.zremrangeByRank(cacheKey, startIndex, endIndex);
   } catch (Exception e) {
     throw e;
   } finally {
     if (jedis != null) {
       jedisPool.returnResource(jedis);
     }
   }
 }
 /**
  * 删除指定的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);
   }
 }
 /**
  * 向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 和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);
   }
 }
  @Test
  public void testGetMasterResource() {

    ShardedJedis shardedJedis = matrixRedisClient.getRedisPool().getMasterResource();
    Collection<JedisShardInfo> shardInfoList = shardedJedis.getAllShardInfo();

    for (JedisShardInfo shardInfo : shardInfoList) {
      Assert.assertTrue(
          Constants.DEFAULT_MASTER1_MASTER.equals(shardInfo.getHost() + ":" + shardInfo.getPort())
              || Constants.DEFAULT_MASTER2_MASTER.equals(
                  shardInfo.getHost() + ":" + shardInfo.getPort()));
    }
  }
  @Test
  public void testGetSlaveResource() {
    ShardedJedis shardedJedis = matrixRedisClient.getRedisPool().getSlaveResource();
    Collection<JedisShardInfo> shardInfoList = shardedJedis.getAllShardInfo();

    for (JedisShardInfo shardInfo : shardInfoList) {
      Assert.assertTrue(
          Arrays.asList(Constants.DEFAULT_MASTER1_SLAVES)
                  .contains(shardInfo.getHost() + ":" + shardInfo.getPort())
              || Arrays.asList(Constants.DEFAULT_MASTER2_SLAVES)
                  .contains(shardInfo.getHost() + ":" + shardInfo.getPort()));
    }
  }
Beispiel #26
0
 public boolean set(String key, String value) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     shardedJedis.set(key, value);
     return true;
   } catch (Exception ex) {
     logger.error("set error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return false;
 }
 public void zDel(String cacheKey, String... items) throws Exception {
   ShardedJedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     jedis.zrem(cacheKey, items);
   } catch (Exception e) {
     throw e;
   } finally {
     if (jedis != null) {
       jedisPool.returnResource(jedis);
     }
   }
 }
Beispiel #28
0
 /**
  * 删除排序集合
  *
  * @param key
  * @param value
  * @return
  */
 public boolean delSortedSet(String key, String value) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     long count = shardedJedis.zrem(key, value);
     return count > 0;
   } catch (Exception ex) {
     logger.error("delSortedSet error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return false;
 }
 public void zDelByScore(String cacheKey, double minScore, double maxScore) throws Exception {
   ShardedJedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     jedis.zremrangeByScore(cacheKey, minScore, maxScore);
   } catch (Exception e) {
     throw e;
   } finally {
     if (jedis != null) {
       jedisPool.returnResource(jedis);
     }
   }
 }
Beispiel #30
0
  public boolean hasLiked(int uid, int bid) {

    String hashcode = encodeUtil.encode(bid, uid);

    if (shardedJedis.hexists(hashcode, "isLiked")
        && shardedJedis.hget(hashcode, "isLiked").equals(BookLikes.LIKED + "")) {

      return true;

    } else {
      return false;
    }
  }