Class RedisZSetHelper

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

public class RedisZSetHelper extends RedisHelper<RedisZSetHelper> implements RedisHelperInt
Helper class for interacting with Redis Sorted Set (ZSET) data types.

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

Redis ZSET is an ordered collection of unique string values, where each value is associated with a numeric score. Records are automatically sorted by score in ascending order.

Example of stored records:

 users:ranking
 ├── score=100 -> {"id":1,"name":"Alex"}
 ├── score=200 -> {"id":2,"name":"John"}
 └── score=300 -> {"id":3,"name":"Michael"}
 

In this example:

  • users:ranking is the Redis ZSET key
  • Each JSON object is stored as a unique ZSET member
  • Each member is associated with a numeric score
  • Records are automatically ordered by score

Notes:

  • TTL is applied to the whole ZSET key, not individual records.
  • ZSET members are stored as Redis strings. JSON values can be stored as plain strings.
  • Duplicate members are not allowed. Adding an existing member updates its score.
  • This helper uses JedisPool internally to manage Redis connections.
See Also:
  • Jedis
  • JedisPool
  • Constructor Details

    • RedisZSetHelper

      public RedisZSetHelper(String host, int port, String username, String password)
      This constructor initializes client for interaction with Redis ZSET 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:score <{keyWithScore}>") public void addRecord(String keyWithScore, String value)
      Adds a record to the specified Redis Sorted Set (ZSET).

      If the record already exists, its score is updated.

      Example:

       users:admins
       ├── 100 -> Alex
       └── 300 -> John
      
       addRecord("users:admins:200", "Mike");
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      keyWithScore - Redis ZSET key:score
      value - record value to add
    • addRecord

      @Step("(Redis) Insert record into key:score <{keyWithScore}> with ttl: <{ttlSeconds}>") public void addRecord(String keyWithScore, String value, long ttlSeconds)
      Adds a record to the specified Redis Sorted Set (ZSET) and sets its time-to-live (TTL).

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

      Example:

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

      Uses await until the key exists.

      The returned list contains all ZSET members ordered by their score in ascending order.

      Examples:

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

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

      This method uses Redis ZCARD command.

      Example:

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