Java 类redis.clients.jedis.ShardedJedis 实例源码

项目:RedisDirectory    文件:ShardedJedisPoolStream.java   
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = shardedJedis.pipelined();
        }
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
}
项目:JRediClients    文件:ShardedJedisTest.java   
@Test
public void checkCloseable() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
  shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
  shards.get(0).setPassword("foobared");
  shards.get(1).setPassword("foobared");

  ShardedJedis jedisShard = new ShardedJedis(shards);
  try {
    jedisShard.set("shard_closeable", "true");
  } finally {
    jedisShard.close();
  }

  for (Jedis jedis : jedisShard.getAllShards()) {
    assertTrue(!jedis.isConnected());
  }
}
项目:jigsaw-payment    文件:RedisTemplate.java   
/**
 * 全局扫描hset
 *
 * @param match field匹配模式
 */
public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
    try (ShardedJedis shardedJedis = shardedJedisPool.getResource()) {
        int cursor = 0;

        ScanParams scanParams = new ScanParams();
        scanParams.match(match);
        Jedis jedis = shardedJedis.getShard(domain);
        ScanResult<Map.Entry<String, String>> scanResult;
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
        do {
            scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);
            list.addAll(scanResult.getResult());
            cursor = Integer.parseInt(scanResult.getStringCursor());
        } while (cursor > 0);
        return list;
    }
}
项目:iBase4J    文件:JedisTemplate.java   
public static <K> K run(String key, Executor<K> executor, Integer... expire) {
    ShardedJedis jedis = getJedis();
    if (jedis == null) {
        return null;
    }
    try {
        K result = executor.execute(jedis);
        if (jedis.exists(key)) {
            if (expire == null || expire.length == 0) {
                jedis.expire(key, EXPIRE);
            } else if (expire.length == 1) {
                jedis.expire(key, expire[0]);
            }
        }
        return result;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return null;
}
项目:automat    文件:JedisHelper.java   
public final Long incr(final String key) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.incr(key);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final Set<Tuple> zrangeWithScores(final String key, final int start, final int end) {
    return JedisTemplate.run(key, new Executor<Set<Tuple>>() {
        public Set<Tuple> execute(ShardedJedis jedis) {
            return jedis.zrangeWithScores(key, start, end);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Long rpush(final byte[] key, final byte[] string) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.rpush(key, string);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public Long hdel(final byte[] key, final byte[] field) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.hdel(key, field);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final List<byte[]> sort(final byte[] key, final SortingParams sortingParameters) {
    return JedisTemplate.run(key, new Executor<List<byte[]>>() {
        public List<byte[]> execute(ShardedJedis jedis) {
            return jedis.sort(key, sortingParameters);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public Long hdel(final byte[] key, final byte[] field) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.hdel(key, field);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final String srandmember(final String key) {
    return jedisTemplate.run(key, new Executor<String>() {
        public String execute(ShardedJedis jedis) {
            return jedis.srandmember(key);
        }
    });
}
项目:jspider    文件:ShardedRedisScheduler.java   
@Override
public void resetRequestRepeatCheck(Task task, Request request) {
    ShardedJedis jedis = jedisPool.getResource();
    try {
        jedis.srem(RedisKeys.getSetKey(task), request.key());
    } finally {
        jedis.close();
    }
}
项目:iBase4J    文件:JedisHelper.java   
public static final Long zcard(final byte[] key) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.zcard(key);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final String hmset(final String key, final Map<String, String> hash) {
    return JedisTemplate.run(key, new Executor<String>() {
        public String execute(ShardedJedis jedis) {
            return jedis.hmset(key, hash);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Long ttl(final byte[] key) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.ttl(key);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
    return JedisTemplate.run(key, new Executor<Set<Tuple>>() {
        public Set<Tuple> execute(ShardedJedis jedis) {
            return jedis.zrangeByScoreWithScores(key, min, max);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Double zscore(final byte[] key, final byte[] member) {
    return jedisTemplate.run(key, new Executor<Double>() {
        public Double execute(ShardedJedis jedis) {
            return jedis.zscore(key, member);
        }
    });
}
项目:WiFiProbeAnalysis    文件:UserDaoImpl.java   
@Override
public void add(List<Object> objectList) {
    ShardedJedis jedis = JedisPoolManager.getResource();
    Map<String, String> userTableMap = new HashMap<>();
    for (Object o : objectList) {
        UserBean userBean = (UserBean) o;
        String Key = String.valueOf(userBean.getShopId()) + "||" + userBean.getMac();
        String value = JSON.toJSONString(userBean);
        userTableMap.put(Key, value);
    }
    jedis.hmset(TableConstants.TABLE_USER, userTableMap);
    jedis.close();
}
项目:iBase4J    文件:JedisHelper.java   
public static final Long rpush(final byte[] key, final byte[] string) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.rpush(key, string);
        }
    });
}
项目:WiFiProbeAnalysis    文件:UserDaoImpl.java   
@Override
public List<Object> get(List<String> keys) {
    ShardedJedis jedis = JedisPoolManager.getResource();
    List<String> userList = jedis.hmget(TableConstants.TABLE_USER, keys.toArray(new String[0]));
    List<Object> userBeanList = new ArrayList<>();
    for (String user : userList) {
        userBeanList.add(JSON.parseObject(user, UserBean.class));
    }
    jedis.close();
    return userBeanList;
}
项目:WiFiProbeAnalysis    文件:UserDaoImpl.java   
public void updateStayTime() {
    ShardedJedis jedis = JedisPoolManager.getResource();
    Map<String, String> map = jedis.hgetAll(TableConstants.TABLE_USER);
    if(map.size() > 0) {
        List<Object> userBeanList = new ArrayList<>();
        for (Map.Entry<String, String> user : map.entrySet()) {
            UserBean userBean = JSON.parseObject(user.getValue(), UserBean.class);
            String firstVisitTime = jedis.lindex(user.getKey(), 0);
            userBean.setFirst_time(DateUtil.stampToDate(firstVisitTime));
            Long len = jedis.llen(user.getKey());
            String LastVisitTime = "";
            Long stayTime = 0L;
            if (len > 1) {
                LastVisitTime = jedis.lindex(user.getKey(), len - 1);
                stayTime = Long.valueOf(LastVisitTime) - Long.valueOf(firstVisitTime) + 3;
                userBean.setRecent_time(DateUtil.stampToDate(LastVisitTime));
            } else {
                stayTime = 3000L;
                userBean.setRecent_time("-");
            }
            Long visitCycle = 0L;
            userBean.setStayTime(stayTime);
            userBean.setVisitCycle(visitCycle);
            userBeanList.add(userBean);
        }
        System.out.println("insert ....");
        this.add(userBeanList);
    }
    System.out.println("nothing insert ....");
    jedis.close();
}
项目:automat    文件:JedisHelper.java   
public List<byte[]> hmget(final byte[] key, final byte[]... fields) {
    return jedisTemplate.run(key, new Executor<List<byte[]>>() {
        public List<byte[]> execute(ShardedJedis jedis) {
            return jedis.hmget(key, fields);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final Long lpush(final byte[] key, final byte[] string) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.lpush(key, string);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final Long expire(final byte[] key, final int seconds) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.expire(key, seconds);
        }
    }, seconds, seconds);
}
项目:automat    文件:JedisHelper.java   
public final String spop(final String key) {
    return jedisTemplate.run(key, new Executor<String>() {
        public String execute(ShardedJedis jedis) {
            return jedis.spop(key);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Set<String> zrange(final String key, final int start, final int end) {
    return jedisTemplate.run(key, new Executor<Set<String>>() {
        public Set<String> execute(ShardedJedis jedis) {
            return jedis.zrange(key, start, end);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Boolean exists(final byte[] key) {
    return jedisTemplate.run(key, new Executor<Boolean>() {
        public Boolean execute(ShardedJedis jedis) {
            return jedis.exists(key);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max,
        final int offset, final int count) {
    return JedisTemplate.run(key, new Executor<Set<Tuple>>() {
        public Set<Tuple> execute(ShardedJedis jedis) {
            return jedis.zrangeByScoreWithScores(key, min, max, offset, count);
        }
    });
}
项目:JRediClients    文件:ShardedJedisTest.java   
@Test
public void checkKeyTags() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
  shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
  ShardedJedis jedis = new ShardedJedis(shards, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);

  assertEquals(jedis.getKeyTag("foo"), "foo");
  assertEquals(jedis.getKeyTag("foo{bar}"), "bar");
  assertEquals(jedis.getKeyTag("foo{bar}}"), "bar"); // default pattern is
  // non greedy
  assertEquals(jedis.getKeyTag("{bar}foo"), "bar"); // Key tag may appear
  // anywhere
  assertEquals(jedis.getKeyTag("f{bar}oo"), "bar"); // Key tag may appear
  // anywhere

  JedisShardInfo s1 = jedis.getShardInfo("abc{bar}");
  JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
  assertSame(s1, s2);

  List<String> keys = getKeysDifferentShard(jedis);
  JedisShardInfo s3 = jedis.getShardInfo(keys.get(0));
  JedisShardInfo s4 = jedis.getShardInfo(keys.get(1));
  assertNotSame(s3, s4);

  ShardedJedis jedis2 = new ShardedJedis(shards);

  assertEquals(jedis2.getKeyTag("foo"), "foo");
  assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");

  JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0) + "{bar}");
  JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1) + "{bar}");
  assertNotSame(s5, s6);
}
项目:JRediClients    文件:ShardedJedisTest.java   
@Test
public void shardedPipeline() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
  shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
  shards.get(0).setPassword("foobared");
  shards.get(1).setPassword("foobared");
  ShardedJedis jedis = new ShardedJedis(shards);

  final List<String> keys = getKeysDifferentShard(jedis);
  jedis.set(keys.get(0), "a");
  jedis.set(keys.get(1), "b");

  assertNotSame(jedis.getShard(keys.get(0)), jedis.getShard(keys.get(1)));

  List<Object> results = jedis.pipelined(new ShardedJedisPipeline() {
    public void execute() {
      get(keys.get(0));
      get(keys.get(1));
    }
  });

  List<Object> expected = new ArrayList<Object>(2);
  expected.add(SafeEncoder.encode("a"));
  expected.add(SafeEncoder.encode("b"));

  assertEquals(2, results.size());
  assertArrayEquals(SafeEncoder.encode("a"), (byte[]) results.get(0));
  assertArrayEquals(SafeEncoder.encode("b"), (byte[]) results.get(1));
}
项目:azeroth    文件:JedisShardProvider.java   
public void release() {
    ShardedJedis jedis = context.get();
    if (jedis != null) {
        context.remove();
        jedis.close();
        if (logger.isTraceEnabled()) {
            logger.trace("<<release a redis conn[{}]", jedis.toString());
        }
    }
}
项目:asura    文件:RedisServiceImpl.java   
@Override
public <T> T hgetAll(final String key, final Callback<T> callback) {
    checkParameters(key, callback);

    final ShardedJedis jedis = getJedis();
    final Map<String, String> result = jedis.hgetAll(key);
    try {
        return callback.callback(result);
    } catch (final Exception e) {
        throw new GetValueRedisException("method: hgetAll, key: " + key + ", result: " + result, e);
    } finally {
        returnResource(jedis);
    }
}
项目:JRediClients    文件:ShardedJedisPoolTest.java   
@Test
public void checkConnectionWithDefaultPort() {
  ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
  ShardedJedis jedis = pool.getResource();
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));
  pool.returnResource(jedis);
  pool.destroy();
}
项目:automat    文件:JedisHelper.java   
public final Long setnx(final String key, final String value) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.setnx(key, value);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public static final byte[] spop(final byte[] key) {
    return JedisTemplate.run(key, new Executor<byte[]>() {
        public byte[] execute(ShardedJedis jedis) {
            return jedis.spop(key);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public Long hlen(final byte[] key) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.hlen(key);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Long zadd(final String key, final double score, final String member) {
    return jedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.zadd(key, score, member);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final void hset(final String key, final String field, final String value) {
    jedisTemplate.run(key, new Executor<Object>() {
        public Object execute(ShardedJedis jedis) {
            return jedis.hset(key, field, value);
        }
    });
}
项目:iBase4J    文件:JedisHelper.java   
public Long setnx(final byte[] key, final byte[] value) {
    return JedisTemplate.run(key, new Executor<Long>() {
        public Long execute(ShardedJedis jedis) {
            return jedis.setnx(key, value);
        }
    });
}
项目:automat    文件:JedisHelper.java   
public final Set<String> zrangeByScore(final String key, final double min, final double max) {
    return jedisTemplate.run(key, new Executor<Set<String>>() {
        public Set<String> execute(ShardedJedis jedis) {
            return jedis.zrangeByScore(key, min, max);
        }
    });
}