Class RedisListHelper

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

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

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

Redis LIST is an ordered collection of string values.

Example of stored records:

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

In this example:

  • users:admins is the Redis LIST key
  • 0, 1, 2 are LIST indexes
  • JSON objects are LIST elements stored as strings
  • The order of records is preserved by insertion order

Notes:

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

    • RedisListHelper

      public RedisListHelper(String host, int port, String username, String password)
      This constructor initializes client for interaction with Redis LIST 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 <{key}>") public void addRecord(String key, String value)
      Adds a new record to the Redis LIST.

      Example:

       users:admins
       ├── 0 -> Alex
       └── 1 -> John
      
       addRecord("users:admins", "Alex");
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis LIST key
      value - record value to add
    • addRecord

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

      TTL is applied to the whole LIST, not to an individual record.

      Example:

       addRecord(
           "users:admins",
           "Alex",
           300
       );
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis LIST key
      value - record value to add
      ttlSeconds - expiration time in seconds
    • 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 LIST.

      Uses await until the key exists.

      The records are returned in insertion order (head to tail).

      Examples:

       users:admins
       ├── 0 -> "Alex"
       ├── 1 -> "John"
      
       grabRecordsByKey("users:admins")
        .seeListAnyEquals("John");
       
      Specified by:
      grabRecordsByKey in interface RedisHelperInt
      Parameters:
      key - Redis LIST key
      Returns:
      AssertableStringList list of stored records
      Throws:
      AssertionError - if the key does not exist or not a Redis LIST
    • getRecordsCountByKey

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

      This method uses Redis LLEN command.

      Example:

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