Class RedisStringHelper

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

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

This class provides convenient methods for common Redis STRING operations, including adding records, reading records, checking key existence, counting records, retrieving TTL, and cleaning string data.

Redis STRING stores a single string value per key. The stored value can contain plain text, numbers, serialized objects, or JSON documents.

Example of stored records:

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

In this example:

  • users:1, users:2, users:3 are Redis STRING keys
  • Each key contains a single string value
  • JSON objects are stored as STRING values
  • Each record is independent and can have its own TTL

Notes:

  • Each Redis STRING key stores only one value.
  • TTL is applied to each individual STRING key.
  • Redis STRING does not maintain ordering between keys. To sort records by creation time or another field, use an additional data structure such as Redis ZSET.
  • This helper uses JedisPool internally to manage Redis connections.
See Also:
  • Jedis
  • JedisPool
  • Constructor Details

    • RedisStringHelper

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

      Redis STRING stores a single value per key. The value can contain plain text, numbers, serialized objects, or JSON data.

      Example:

       users:admin1 -> Alex
      
       addRecord("users:admin1", "Alex");
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis STRING key
      value - record value to store
    • 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 STRING key and sets expiration time.

      TTL is applied to this individual STRING key.

      Example:

       addRecord(
           "users:admin1",
           "Alex",
           300
       );
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis STRING key
      value - record value to store
      ttlSeconds - expiration time in seconds
    • grabRecordsByKey

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

      Uses await until the key exists.

      The returned list always contains one element because Redis STRING stores only a single value per key.

      Example:

       users:admin1 -> Alex
      
       grabRecordsByKey("users:admins")
        .seeListAnyEquals("Alex");
       

      Returning a AssertableStringList keeps the API consistent with other Redis helpers (LIST, HASH, etc.).

      Specified by:
      grabRecordsByKey in interface RedisHelperInt
      Parameters:
      key - Redis STRING key
      Returns:
      AssertableStringList list containing one record
      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 for the specified Redis STRING key.

      Because Redis STRING contains only one value per key:

      • key exists -> returns 1
      • key does not exist -> returns 0

      Example:

       users:admin1 -> Alex
      
       getRecordsCountByKey("users:admin1") returns 1
       getRecordsCountByKey("users:unknown") returns 0
       
      Specified by:
      getRecordsCountByKey in interface RedisHelperInt
      Parameters:
      key - Redis STRING key
      Returns:
      1 if key exists, otherwise 0