Class RedisSetHelper

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

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

This class provides convenient methods for common Redis SET operations, including adding records, reading records, counting records, checking TTL, and removing records.

Redis SET is an unordered collection of unique string values.

Example of stored records:

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

In this example:

  • users:admins is the Redis SET key
  • Each JSON object is stored as a unique SET member
  • Duplicate records are ignored automatically
  • The order of records is not guaranteed

Notes:

  • TTL is applied to the whole SET key, not individual records.
  • SET members are stored as Redis strings. JSON values can be stored as plain strings.
  • Duplicate values are not allowed. Attempting to add an existing value has no effect.
  • This helper uses JedisPool internally to manage Redis connections.
See Also:
  • Jedis
  • JedisPool
  • Constructor Details

    • RedisSetHelper

      public RedisSetHelper(String host, int port, String username, String password)
      This constructor initializes client for interaction with Redis SET 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 SET.

      If the record already exists in the SET, it will not be added again.

      Example:

       users:admins
       ├── 0 -> Alex
       └── 1 -> John
      
       addRecord("users:admins", "Alex");
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis SET 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 record to the specified Redis SET and sets its time-to-live (TTL).

      If the SET does not exist, it will be created automatically. If the record already exists, it will not be duplicated.

      Example:

       addRecord(
           "users:admins",
           "Alex",
           300
       );
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis SET 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 SET.

      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 SET key
      Returns:
      AssertableStringList list of stored records
      Throws:
      AssertionError - if the key does not exist or not a Redis SET
    • getRecordsCountByKey

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

      This method uses Redis SCARD command.

      Example:

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