Class RedisClient
java.lang.Object
fr.hytale.loader.datastorage.RedisClient
Redis client for managing remote Redis database connections.
This class provides a high-level API for interacting with Redis, including connection pooling, automatic reconnection, and common operations.
Example Usage:
// Connect to Redis
RedisClient redis = new RedisClient("127.0.0.1", 6379);
redis.connect();
// Store and retrieve data
redis.set("player:123:coins", "1000");
String coins = redis.get("player:123:coins");
// Hash operations
redis.hset("player:123", "name", "Steve");
redis.hset("player:123", "level", "42");
Map<String, String> data = redis.hgetAll("player:123");
// Close connection
redis.disconnect();
- Since:
- 1.0.5
- Version:
- 1.0.6
- Author:
- HytaleLoader
-
Constructor Summary
ConstructorsConstructorDescriptionRedisClient(String host, int port) Creates a new Redis client.RedisClient(String host, int port, String password) Creates a new Redis client with authentication.RedisClient(String host, int port, String password, int database) Creates a new Redis client with full configuration. -
Method Summary
Modifier and TypeMethodDescriptionbooleanconnect()Connects to the Redis server with connection pooling.longDecrements a numeric value.longDecrements a numeric value by a specific amount.longDeletes one or more keys.voidDisconnects from the Redis server and closes the connection pool.booleanChecks if a key exists.booleanSets an expiration time on a key.Gets a string value for the given key.redis.clients.jedis.JedisPoolgetPool()Gets direct access to the Jedis connection pool.longDeletes one or more fields from a hash.booleanChecks if a field exists in a hash.Gets a field value from a hash.Gets all fields and values from a hash.booleanSets a field value in a hash.longIncrements a numeric value.longIncrements a numeric value by a specific amount.booleanChecks if the client is connected to Redis.Gets all keys matching a pattern.longGets the length of a list.longPushes a value to the left (start) of a list.Gets a range of elements from a list.booleanping()Executes a Redis PING command to test connectivity.longPushes a value to the right (end) of a list.longAdds members to a set.booleanSets a string value for the given key.booleanSets a string value with expiration time.booleanChecks if a member exists in a set.Gets all members of a set.longRemoves members from a set.longGets the remaining time to live of a key.
-
Constructor Details
-
RedisClient
Creates a new Redis client.- Parameters:
host- The Redis server hostname or IP addressport- The Redis server port (default: 6379)
-
RedisClient
-
RedisClient
-
-
Method Details
-
connect
public boolean connect()Connects to the Redis server with connection pooling.- Returns:
- true if connected successfully, false otherwise
-
disconnect
public void disconnect()Disconnects from the Redis server and closes the connection pool. -
isConnected
public boolean isConnected()Checks if the client is connected to Redis.- Returns:
- true if connected, false otherwise
-
set
-
setex
-
get
-
delete
Deletes one or more keys.- Parameters:
keys- The keys to delete- Returns:
- The number of keys deleted
-
exists
Checks if a key exists.- Parameters:
key- The key- Returns:
- true if exists, false otherwise
-
expire
Sets an expiration time on a key.- Parameters:
key- The keyseconds- Time to live in seconds- Returns:
- true if successful, false otherwise
-
ttl
Gets the remaining time to live of a key.- Parameters:
key- The key- Returns:
- Time to live in seconds, -1 if no expiry, -2 if not found
-
hset
-
hget
-
hgetAll
-
hdel
-
hexists
-
rpush
-
lpush
-
lrange
-
llen
Gets the length of a list.- Parameters:
key- The list key- Returns:
- The length of the list
-
sadd
-
smembers
-
sismember
-
srem
-
incr
Increments a numeric value.- Parameters:
key- The key- Returns:
- The new value after increment
-
incrBy
Increments a numeric value by a specific amount.- Parameters:
key- The keyincrement- The amount to increment- Returns:
- The new value after increment
-
decr
Decrements a numeric value.- Parameters:
key- The key- Returns:
- The new value after decrement
-
decrBy
Decrements a numeric value by a specific amount.- Parameters:
key- The keydecrement- The amount to decrement- Returns:
- The new value after decrement
-
keys
-
ping
public boolean ping()Executes a Redis PING command to test connectivity.- Returns:
- true if server responds with PONG, false otherwise
-
getPool
public redis.clients.jedis.JedisPool getPool()Gets direct access to the Jedis connection pool.Use this for advanced operations not covered by the wrapper methods. Remember to close resources properly using try-with-resources.
- Returns:
- The Jedis connection pool
-