Class RedisClient

java.lang.Object
fr.hytale.loader.datastorage.RedisClient

public class RedisClient extends Object
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 Details

    • RedisClient

      public RedisClient(String host, int port)
      Creates a new Redis client.
      Parameters:
      host - The Redis server hostname or IP address
      port - The Redis server port (default: 6379)
    • RedisClient

      public RedisClient(String host, int port, String password)
      Creates a new Redis client with authentication.
      Parameters:
      host - The Redis server hostname or IP address
      port - The Redis server port
      password - The Redis password (null if no auth)
    • RedisClient

      public RedisClient(String host, int port, String password, int database)
      Creates a new Redis client with full configuration.
      Parameters:
      host - The Redis server hostname or IP address
      port - The Redis server port
      password - The Redis password (null if no auth)
      database - The database index (0-15)
  • 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

      public boolean set(String key, String value)
      Sets a string value for the given key.
      Parameters:
      key - The key
      value - The value
      Returns:
      true if successful, false otherwise
    • setex

      public boolean setex(String key, String value, long seconds)
      Sets a string value with expiration time.
      Parameters:
      key - The key
      value - The value
      seconds - Expiration time in seconds
      Returns:
      true if successful, false otherwise
    • get

      public String get(String key)
      Gets a string value for the given key.
      Parameters:
      key - The key
      Returns:
      The value, or null if not found
    • delete

      public long delete(String... keys)
      Deletes one or more keys.
      Parameters:
      keys - The keys to delete
      Returns:
      The number of keys deleted
    • exists

      public boolean exists(String key)
      Checks if a key exists.
      Parameters:
      key - The key
      Returns:
      true if exists, false otherwise
    • expire

      public boolean expire(String key, long seconds)
      Sets an expiration time on a key.
      Parameters:
      key - The key
      seconds - Time to live in seconds
      Returns:
      true if successful, false otherwise
    • ttl

      public long ttl(String key)
      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

      public boolean hset(String key, String field, String value)
      Sets a field value in a hash.
      Parameters:
      key - The hash key
      field - The field name
      value - The field value
      Returns:
      true if successful, false otherwise
    • hget

      public String hget(String key, String field)
      Gets a field value from a hash.
      Parameters:
      key - The hash key
      field - The field name
      Returns:
      The field value, or null if not found
    • hgetAll

      public Map<String,String> hgetAll(String key)
      Gets all fields and values from a hash.
      Parameters:
      key - The hash key
      Returns:
      A map of field-value pairs
    • hdel

      public long hdel(String key, String... fields)
      Deletes one or more fields from a hash.
      Parameters:
      key - The hash key
      fields - The fields to delete
      Returns:
      The number of fields deleted
    • hexists

      public boolean hexists(String key, String field)
      Checks if a field exists in a hash.
      Parameters:
      key - The hash key
      field - The field name
      Returns:
      true if exists, false otherwise
    • rpush

      public long rpush(String key, String... values)
      Pushes a value to the right (end) of a list.
      Parameters:
      key - The list key
      values - The values to push
      Returns:
      The length of the list after push
    • lpush

      public long lpush(String key, String... values)
      Pushes a value to the left (start) of a list.
      Parameters:
      key - The list key
      values - The values to push
      Returns:
      The length of the list after push
    • lrange

      public List<String> lrange(String key, long start, long end)
      Gets a range of elements from a list.
      Parameters:
      key - The list key
      start - Start index (0-based)
      end - End index (-1 for all)
      Returns:
      List of values
    • llen

      public long llen(String key)
      Gets the length of a list.
      Parameters:
      key - The list key
      Returns:
      The length of the list
    • sadd

      public long sadd(String key, String... members)
      Adds members to a set.
      Parameters:
      key - The set key
      members - The members to add
      Returns:
      The number of members added
    • smembers

      public Set<String> smembers(String key)
      Gets all members of a set.
      Parameters:
      key - The set key
      Returns:
      Set of members
    • sismember

      public boolean sismember(String key, String member)
      Checks if a member exists in a set.
      Parameters:
      key - The set key
      member - The member to check
      Returns:
      true if member exists, false otherwise
    • srem

      public long srem(String key, String... members)
      Removes members from a set.
      Parameters:
      key - The set key
      members - The members to remove
      Returns:
      The number of members removed
    • incr

      public long incr(String key)
      Increments a numeric value.
      Parameters:
      key - The key
      Returns:
      The new value after increment
    • incrBy

      public long incrBy(String key, long increment)
      Increments a numeric value by a specific amount.
      Parameters:
      key - The key
      increment - The amount to increment
      Returns:
      The new value after increment
    • decr

      public long decr(String key)
      Decrements a numeric value.
      Parameters:
      key - The key
      Returns:
      The new value after decrement
    • decrBy

      public long decrBy(String key, long decrement)
      Decrements a numeric value by a specific amount.
      Parameters:
      key - The key
      decrement - The amount to decrement
      Returns:
      The new value after decrement
    • keys

      public Set<String> keys(String pattern)
      Gets all keys matching a pattern.
      Parameters:
      pattern - The pattern (e.g., "player:*", "user:*:coins")
      Returns:
      Set of matching 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