示例#1
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));
  }
示例#2
0
 /**
  * 设置排序集合
  *
  * @param key
  * @param score
  * @param value
  * @return
  */
 public boolean setSortedSet(String key, long score, String value) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     shardedJedis.zadd(key, score, value);
     return true;
   } catch (Exception ex) {
     logger.error("setSortedSet error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return false;
 }
 /**
  * 通过key向zset中添加value,score,其中score就是用来排序的
  *
  * <p>如果该value已经存在则根据score更新元素
  *
  * @param key
  * @param score
  * @param member
  * @return
  */
 public Long zadd(String key, double score, String member) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.zadd(key, score, member);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
 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);
     }
   }
 }
  @Test
  public void pipelineResponse() {
    jedis.set("string", "foo");
    jedis.lpush("list", "foo");
    jedis.hset("hash", "foo", "bar");
    jedis.zadd("zset", 1, "foo");
    jedis.sadd("set", "foo");

    ShardedJedisPipeline p = jedis.pipelined();
    Response<String> string = p.get("string");
    Response<String> list = p.lpop("list");
    Response<String> hash = p.hget("hash", "foo");
    Response<Set<String>> zset = p.zrange("zset", 0, -1);
    Response<String> set = p.spop("set");
    Response<Boolean> blist = p.exists("list");
    Response<Double> zincrby = p.zincrby("zset", 1, "foo");
    Response<Long> zcard = p.zcard("zset");
    p.lpush("list", "bar");
    Response<List<String>> lrange = p.lrange("list", 0, -1);
    Response<Map<String, String>> hgetAll = p.hgetAll("hash");
    p.sadd("set", "foo");
    Response<Set<String>> smembers = p.smembers("set");
    Response<Set<Tuple>> zrangeWithScores = p.zrangeWithScores("zset", 0, -1);
    p.sync();

    assertEquals("foo", string.get());
    assertEquals("foo", list.get());
    assertEquals("bar", hash.get());
    assertEquals("foo", zset.get().iterator().next());
    assertEquals("foo", set.get());
    assertFalse(blist.get());
    assertEquals(new Double(2), zincrby.get());
    assertEquals(new Long(1), zcard.get());
    assertEquals(1, lrange.get().size());
    assertNotNull(hgetAll.get().get("foo"));
    assertEquals(1, smembers.get().size());
    assertEquals(1, zrangeWithScores.get().size());
  }
 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);
     }
   }
 }