Class RedisHashHelper

java.lang.Object
net.bugreaper.modules.redis.helpers.RedisHelper<RedisHashHelper>
net.bugreaper.modules.redis.helpers.RedisHashHelper
All Implemented Interfaces:
RedisHelperInt

public class RedisHashHelper extends RedisHelper<RedisHashHelper> implements RedisHelperInt
Helper class for interacting with Redis HASH data types.

This class provides convenient methods for common Redis HASH operations, including adding records, reading records, counting records, checking TTL, and cleaning hash data.

Redis HASH is a collection of field-value pairs, similar to a Map<String, String>.

Example of stored records:

 users
 ├── 1 -> {"id":1,"name":"Alex"}
 ├── 2 -> {"id":2,"name":"John"}
 └── 3 -> {"id":3,"name":"Michael"}
 

In this example:

  • users is the Redis HASH key
  • 1, 2, 3 are HASH fields (record identifiers)
  • JSON objects are HASH field values stored as strings
  • The order of records is not guaranteed because Redis HASH is unordered

Notes:

  • TTL is applied to the whole HASH key, not individual fields.
  • Records are stored as Redis strings inside the HASH. JSON values can be stored as plain strings.
  • HASH fields cannot have individual expiration times.
  • This helper uses JedisPool internally to manage Redis connections.
See Also:
  • Jedis
  • JedisPool
  • Constructor Details

    • RedisHashHelper

      public RedisHashHelper(String host, int port, String username, String password)
      This constructor initializes client for interaction with Redis HASH data types
      Parameters:
      host - host of Redis "localhost"
      port - port of Redis
      username - admin username
      password - admin password
  • Method Details

    • addRecord

      @Step("(Redis) Insert record into key:id <{keyWithId}>") public void addRecord(String keyWithId, String value)
      Adds a new record to the Redis HASH.

      The record is stored as a HASH field-value pair. The field value contains the record data (for example, JSON string).

      Example:

       users:admins
       ├── 1 -> Alex
       └── 2 -> John
      
       addRecord("users:admins:1", "Alex");
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      keyWithId - Redis HASH key key:id
      value - record value to add
      Throws:
      RedisHelperException - if no id provided in keyWithId
    • addRecord

      @Step("(Redis) Insert record into key:id <{keyWithId}> with ttl: <{ttlSeconds}>") public void addRecord(String keyWithId, String value, long ttlSeconds)
      Adds a new record to the Redis HASH and sets expiration time for the key.

      TTL is applied to the whole HASH, not to an individual field/record.

      Example:

       users:admins
       ├── 1 -> Alex
       └── 2 -> John
      
       addRecord("users:admins:1", "Alex", 300);
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      keyWithId - Redis HASH key:id
      value - record value to add
      ttlSeconds - expiration time in seconds
      Throws:
      RedisHelperException - if no id provided in keyWithId
    • grabRecordsByKey

      @Step("(Redis) Grab records from key: {key}") public net.bugreaper.core.assertable.AssertableStringList grabRecordsByKey(String key)
      Returns all records stored in the specified Redis HASH.

      Uses await until the key exists.

      The returned list contains HASH values only. HASH fields are not returned.

      Example:

       users:admins
       ├── 0 -> "Alex"
       ├── 1 -> "John"
       

      Redis HASH does not guarantee record ordering.

      Specified by:
      grabRecordsByKey in interface RedisHelperInt
      Parameters:
      key - Redis HASH key
      Returns:
      AssertableStringList list of stored records
      Throws:
      AssertionError - if the key does not exist or not a Redis HASH
    • getRecordsCountByKey

      public long getRecordsCountByKey(String key)
      Returns the number of records stored in the specified Redis HASH.

      This method counts HASH fields using Redis HLEN command.

      Example:

       users:admins
       ├── 1 -> record1
       ├── 2 -> record2
      
       getRecordsCountByKey("users:admins") returns 2
       
      Specified by:
      getRecordsCountByKey in interface RedisHelperInt
      Parameters:
      key - Redis HASH key
      Returns:
      number of records stored in the HASH