Class Redis

java.lang.Object
net.bugreaper.modules.redis.Redis
All Implemented Interfaces:
RedisConfig, RedisHelperInt

public class Redis extends Object implements RedisHelperInt, RedisConfig
Universal Redis helper facade that provides a common API for different Redis data types.

This class delegates all operations to a concrete Redis helper implementation based on the selected data type.

Supported Redis data types:

The selected Redis data type defines the underlying Redis commands used for storing and retrieving records.

  • Constructor Details

    • Redis

      public Redis(String host, int port, String username, String password, String dataType)
      This constructor initializes client for interaction with Redis
      Parameters:
      host - host of Redis "localhost"
      port - port of Redis
      username - admin username
      password - admin password
      dataType - Redis data type "list", "hash", "string", "set", "zset"
  • Method Details

    • getInstance

      public static Redis getInstance(String dataType)
      Returns a unique Redis helper instance for the specified Redis data type.

      This method implements the Multiton pattern. A separate singleton instance is created for each Redis data type. Subsequent calls with the same data type return the already created instance.

      Supported types:

      • list
      • hash
      • string
      • set
      • zset
      • stream

      Loads configuration values from a YAML file.

      Default file: bugreaper.yml

      Custom file: using -DbugreaperEnv=test loads bugreaper-test.yml

       modules:
         redis:
           host: localhost
           port: 6379
           username: admin
           password: admin_pass
           await: 300 # optional
       
      Parameters:
      dataType - Redis data type used to select the required helper
      Returns:
      unique Redis instance associated with the specified Redis data type
      Throws:
      RedisHelperException - if the specified Redis data type wrong or not supported
    • setAwaitMs

      public Redis setAwaitMs(int awaitMs)
      Description copied from interface: RedisConfig
      Configures the global await timeout for assertions and operations that use await.
      Specified by:
      setAwaitMs in interface RedisConfig
      Parameters:
      awaitMs - await timeout in milliseconds
      Returns:
      this instance for method chaining
    • getConfigSummary

      public String getConfigSummary()
      Description copied from interface: RedisConfig
      Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.

      The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.

      Specified by:
      getConfigSummary in interface RedisConfig
      Returns:
      String with summary
    • addRecord

      public void addRecord(String key, String value)
      Description copied from interface: RedisHelperInt
      Adds a record to Redis using the configured Redis data type helper.

      The storage operation depends on the Redis data type

      • LIST - appends a new element to the list
      • HASH - stores a value in the hash by key:id
      • STRING - stores or replaces the string value
      • SET - appends a new element to the set (duplicates ignored)
      • ZSET - appends a new element to the zset by key:score (duplicates ignored)
      • STREAM - appends a new JSON element to the stream
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis key
      value - value to store in Redis
    • addRecord

      public void addRecord(String key, String value, long ttlSeconds)
      Description copied from interface: RedisHelperInt
      Adds a record to Redis with an expiration time.

      The storage operation depends on the Redis data type

      • LIST - appends a new element to the list, TTL for all list
      • HASH - stores a value in the hash by key:id, TTL for all hash
      • STRING - stores or replaces the string value, TTL for key
      • SET - stores a value in the hash by key, TTL for all set
      • ZSET - appends a new element to the zset by key:score, TTL for all zset
      • STREAM - appends a new JSON element to the stream, TTL for all stream
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis key
      value - value to store in Redis
      ttlSeconds - time to live in seconds
    • cleanAll

      public void cleanAll()
      Description copied from interface: RedisHelperInt
      Removes ALL data from current Redis database.

      WARNING: This operation deletes every key from selected Redis DB.

      Specified by:
      cleanAll in interface RedisHelperInt
    • deleteKeysByPattern

      public void deleteKeysByPattern(String keyPattern)
      Description copied from interface: RedisHelperInt
      Deletes Redis keys matching the specified pattern.

      Uses Redis SCAN to safely find matching keys without blocking the Redis server.

      Examples: users:admins (LIST) ├── 0 -> Alex └── 1 -> John users:developers (STRINGS) ├── 0 -> Michael └── 1 -> Max users:data (HASH) ├── 1 -> test1 └── 2 -> test2

       deleteByKeyPattern("users:*");
       

      Deletes:

       users:admins
       users:developers
       users:data
       

      Also supports exact key deletion:

       deleteByKeyPattern("users:admins");
       
      Specified by:
      deleteKeysByPattern in interface RedisHelperInt
      Parameters:
      keyPattern - Redis key pattern or exact key
    • grabRecordsByKey

      public net.bugreaper.core.assertable.AssertableStringList grabRecordsByKey(String key)
      Description copied from interface: RedisHelperInt
      Retrieves all records stored under the specified Redis key.

      Uses await until the key exists.

      The returned records depend on the configured Redis data type:

      • LIST - returns list elements
      • HASH - returns hash values
      • STRING - returns a single value as a list
      • SET - returns set elements
      • ZSET - returns zset elements
      • STREAM - returns stream elements - records are represented as JSON strings

      Examples:

       grabRecordsByKey("users:admins")
        .seeListAnyEquals("John");
       
      Specified by:
      grabRecordsByKey in interface RedisHelperInt
      Parameters:
      key - Redis key
      Returns:
      AssertableStringList list of stored records
    • getRecordsCountByKey

      public long getRecordsCountByKey(String key)
      Description copied from interface: RedisHelperInt
      Returns the number of records stored under the specified Redis key.

      The returned count depend on the configured Redis data type:

      • LIST - returns elements count in list
      • HASH - returns values count in hash
      • STRING - stores only a single value per key
      • SET - returns values count in set
      • ZSET - returns values count in zset
      • STREAM - returns values count in stream
      Specified by:
      getRecordsCountByKey in interface RedisHelperInt
      Parameters:
      key - Redis key
      Returns:
      number of records stored under the key
    • getKeysCountByPattern

      public long getKeysCountByPattern(String keyPattern)
      Description copied from interface: RedisHelperInt
      Returns the number of Redis keys matching the specified pattern.

      This method counts keys, not records inside.

      Example:

       users:admins      -> LIST
       users:developers  -> HASH
       users:me          -> STRING
      
       getKeysCountByPattern("users:*") returns 3
       
      Specified by:
      getKeysCountByPattern in interface RedisHelperInt
      Parameters:
      keyPattern - Redis key pattern
      Returns:
      number of matching Redis keys
    • getKeyExistsStatus

      public boolean getKeyExistsStatus(String key)
      Description copied from interface: RedisHelperInt
      Checks whether the specified Redis key exists.

      The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.

      Specified by:
      getKeyExistsStatus in interface RedisHelperInt
      Parameters:
      key - Redis key
      Returns:
      true if the key exists, otherwise false
    • getTtlByKey

      public long getTtlByKey(String key)
      Description copied from interface: RedisHelperInt
      Returns the remaining TTL (time-to-live) of the specified Redis key.
      Specified by:
      getTtlByKey in interface RedisHelperInt
      Parameters:
      key - Redis key
      Returns:
      remaining TTL in seconds
    • getKeyType

      public String getKeyType(String key)
      Description copied from interface: RedisHelperInt
      Returns the Redis data type of the specified key.

      Possible return values:

      • string
      • list
      • set
      • zset
      • hash
      • stream
      • none (key does not exist)
      Specified by:
      getKeyType in interface RedisHelperInt
      Parameters:
      key - Redis key
      Returns:
      Redis data type or none if the key does not exist
    • seeKeyExists

      public void seeKeyExists(String key)
      Description copied from interface: RedisHelperInt
      Asserts that the specified Redis key exists.

      Uses await.

      The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.

      Example:

       seeKeyExists("users:admins");
       seeKeyExists("users:1");
       
      Specified by:
      seeKeyExists in interface RedisHelperInt
      Parameters:
      key - Redis key
    • seeKeyTypeIs

      public void seeKeyTypeIs(String key, String type)
      Description copied from interface: RedisHelperInt
      Asserts that the specified Redis key has the expected data type.

      Supported Redis data types:

      • string
      • list
      • set
      • zset
      • hash
      • stream

      Examples:

       seeKeyTypeIs("users:1", "string");
       seeKeyTypeIs("users:admins", "list");
       seeKeyTypeIs("users", "hash");
       
      Specified by:
      seeKeyTypeIs in interface RedisHelperInt
      Parameters:
      key - Redis key
      type - expected Redis data type
    • seeRecordsCountByKeyIsExactly

      public void seeRecordsCountByKeyIsExactly(String key, long expectedCount)
      Description copied from interface: RedisHelperInt
      Asserts that the number of records in the Redis key is exactly the expected value.

      Uses await.

      The returned records depend on the configured Redis data type:

      • LIST - number of records stored in the specified Redis LIST
      • HASH - number of records stored in the specified Redis HASH
      • STRING - contains only one value per key if exist
      • SET - number of records stored in the specified Redis SET
      • ZSET - number of records stored in the specified Redis ZSET
      • STREAM - number of records stored in the specified Redis STREAM
      Specified by:
      seeRecordsCountByKeyIsExactly in interface RedisHelperInt
      Parameters:
      key - Redis key
      expectedCount - expected number of records
    • seeKeysCountByPatternIsExactly

      public void seeKeysCountByPatternIsExactly(String keyPattern, long expectedCount)
      Description copied from interface: RedisHelperInt
      Asserts that the number of Redis keys matching the specified pattern is exactly the expected count.

      Uses await.

      Specified by:
      seeKeysCountByPatternIsExactly in interface RedisHelperInt
      Parameters:
      keyPattern - Redis key pattern
      expectedCount - expected number of keys
    • seeTtlIsGreaterThan

      public void seeTtlIsGreaterThan(String key, int minTtl)
      Description copied from interface: RedisHelperInt
      Asserts that the key TTL is greater than the specified value.
      Specified by:
      seeTtlIsGreaterThan in interface RedisHelperInt
      Parameters:
      key - Redis key
      minTtl - minimum expected TTL in seconds
    • seeTtlIsLessThan

      public void seeTtlIsLessThan(String key, int maxTtl)
      Description copied from interface: RedisHelperInt
      Asserts that the key TTL is less than the specified value.
      Specified by:
      seeTtlIsLessThan in interface RedisHelperInt
      Parameters:
      key - Redis key
      maxTtl - maximum expected TTL in seconds
    • seeKeysCountByPatternIsGreaterThan

      public void seeKeysCountByPatternIsGreaterThan(String keyPattern, long minCount)
      Description copied from interface: RedisHelperInt
      Asserts that the number of Redis keys matching the specified pattern is greater than the given value.

      Uses await.

      Specified by:
      seeKeysCountByPatternIsGreaterThan in interface RedisHelperInt
      Parameters:
      keyPattern - Redis key pattern
      minCount - minimum expected number of keys
    • seeKeysCountByPatternIsLessThan

      public void seeKeysCountByPatternIsLessThan(String keyPattern, long maxCount)
      Description copied from interface: RedisHelperInt
      Asserts that the number of Redis keys matching the specified pattern is less than the given value.

      Uses await.

      Specified by:
      seeKeysCountByPatternIsLessThan in interface RedisHelperInt
      Parameters:
      keyPattern - Redis key pattern
      maxCount - maximum expected number of keys