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)); }
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; }
@Test public void connectToRedisUsingSentinelWithReconnect() throws Exception { RedisConnection<String, String> connect = sentinelClient.connect().sync(); connect.ping(); connect.quit(); connect.ping(); connect.close(); }
@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); } }
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(); }
/** * 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()); }
/** * 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[] {})); }
@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)); }
@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); }
@Override public void rollbackTransaction() { connection.discard(); inTransaction = false; }
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())); }
/** * 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); } }
@Override public void remove(Object key) { connection.del(key.toString()); }
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); }
@Override public void connect() throws IOException { client = new RedisClient(host, port); connection = client.connect(); connection.select(dbIndex); }
@Override public void beginTransaction() { connection.multi(); inTransaction = true; }
@Override public void commitTransaction() { connection.exec(); inTransaction = false; }