Ejemplo n.º 1
0
 private boolean set(final String key, final Object value) {
   RedisConnection<String, String> redis = ChunkControl.getPlugin().redis();
   return redis.hset(
       String.format("chunk:%s:%d", key, chunk.getX()),
       String.format("%d", chunk.getZ()),
       String.valueOf(value));
 }
Ejemplo n.º 2
0
  public RedisConnection<String, String> connect() {
    RedisConnection<String, String> connection = redisClient.connect();

    String password = getConfig().getString("redis.password");
    if (password != null) connection.auth(password);

    return connection;
  }
Ejemplo n.º 3
0
 @Test
 public void connectToRedisUsingSentinelWithReconnect() throws Exception {
   RedisConnection<String, String> connect = sentinelClient.connect().sync();
   connect.ping();
   connect.quit();
   connect.ping();
   connect.close();
 }
Ejemplo n.º 4
0
 @SuppressWarnings({"unchecked", "rawtypes"})
 @Override
 public void put(Object key, Object value) {
   if (value instanceof Map) {
     connection.hmset(key.toString(), (Map) value);
   } else {
     connection.set(key.toString(), value.toString());
   }
   if (keyExpiryTime != -1) {
     connection.expire(key.toString(), keyExpiryTime);
   }
 }
Ejemplo n.º 5
0
  public static void main(String[] args) {
    // Syntax: redis://[password@]host[:port][/databaseNumber]
    RedisClient redisClient = new RedisClient(RedisURI.create("redis://password@localhost:6379/0"));
    RedisConnection<String, String> connection = redisClient.connect();
    System.out.println("Connected to Redis");

    connection.set("foo", "bar");
    String value = connection.get("foo");
    System.out.println(value);

    connection.close();
    redisClient.shutdown();
  }
Ejemplo n.º 6
0
 /**
  * Gets the value given the key. Note that it does NOT work with hash values or list values
  *
  * @param key
  * @return value of the key.
  */
 @Override
 public Object get(Object key) {
   if (isInTransaction()) {
     throw new RuntimeException("Cannot call get when in redis transaction");
   }
   return connection.get(key.toString());
 }
Ejemplo n.º 7
0
 /**
  * Gets all the values given the keys. Note that it does NOT work with hash values or list values
  *
  * @param keys
  * @return values of the keys.
  */
 @SuppressWarnings("unchecked")
 @Override
 public List<Object> getAll(List<Object> keys) {
   if (isInTransaction()) {
     throw new RuntimeException("Cannot call get when in redis transaction");
   }
   return (List<Object>) (List<?>) connection.mget(keys.toArray(new String[] {}));
 }
Ejemplo n.º 8
0
  @Override
  public void putAll(Map<Object, Object> m) {

    Map<String, String> params = Maps.newHashMap();
    for (Map.Entry<Object, Object> entry : m.entrySet()) {
      params.put(entry.getKey().toString(), entry.getValue().toString());
    }
    connection.mset(params);
  }
    @Override
    public void execute(Tuple tuple) {
      // access the first column 'word'
      String word = tuple.getStringByField("word");

      // access the second column 'count'
      Integer count = tuple.getIntegerByField("count");

      // publish the word count to redis using word as the key
      redis.publish("WordCountTopology", word + "|" + Long.toString(count));
    }
Ejemplo n.º 10
0
  @Test
  public void sentinelConnectWith() throws Exception {

    RedisClient client =
        new RedisClient(
            RedisURI.Builder.sentinel(TestSettings.host(), 1234, MASTER_ID)
                .withSentinel(TestSettings.host())
                .build());

    RedisSentinelAsyncCommands<String, String> sentinelConnection = client.connectSentinelAsync();
    assertThat(sentinelConnection.ping().get()).isEqualTo("PONG");

    sentinelConnection.close();

    RedisConnection<String, String> connection2 = client.connect().sync();
    assertThat(connection2.ping()).isEqualTo("PONG");
    connection2.quit();

    Wait.untilTrue(connection2::isOpen).waitOrTimeout();

    assertThat(connection2.ping()).isEqualTo("PONG");
    connection2.close();
    FastShutdown.shutdown(client);
  }
Ejemplo n.º 11
0
 @Override
 public void rollbackTransaction() {
   connection.discard();
   inTransaction = false;
 }
Ejemplo n.º 12
0
 private String get(final String key) {
   RedisConnection<String, String> redis = ChunkControl.getPlugin().redis();
   return redis.hget(
       String.format("chunk:%s:%d", key, chunk.getX()), String.format("%d", chunk.getZ()));
 }
Ejemplo n.º 13
0
 /**
  * Calls incrbyfloat on the redis store.
  *
  * @param key
  * @param doubleValue
  */
 public void incrByFloat(String key, double doubleValue) {
   connection.incrbyfloat(key, doubleValue);
   if (keyExpiryTime != -1) {
     connection.expire(key, keyExpiryTime);
   }
 }
Ejemplo n.º 14
0
 @Override
 public void remove(Object key) {
   connection.del(key.toString());
 }
Ejemplo n.º 15
0
 private double add(final String key, final double value) {
   RedisConnection<String, String> redis = ChunkControl.getPlugin().redis();
   return redis.hincrbyfloat(
       String.format("chunk:%s:%d", key, chunk.getX()), String.format("%d", chunk.getZ()), value);
 }
Ejemplo n.º 16
0
 @Override
 public void connect() throws IOException {
   client = new RedisClient(host, port);
   connection = client.connect();
   connection.select(dbIndex);
 }
Ejemplo n.º 17
0
 @Override
 public void beginTransaction() {
   connection.multi();
   inTransaction = true;
 }
Ejemplo n.º 18
0
 @Override
 public void commitTransaction() {
   connection.exec();
   inTransaction = false;
 }