Class RedisZSetHelper
- All Implemented Interfaces:
RedisHelperInt
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:rankingis 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
JedisPoolinternally to manage Redis connections.
- See Also:
-
JedisJedisPool
-
Field Summary
Fields inherited from class net.bugreaper.modules.redis.helpers.RedisHelper
awaitMs, jedisPool, SCAN_BATCH_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionRedisZSetHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis ZSET data types -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a record to the specified Redis Sorted Set (ZSET).voidAdds a record to the specified Redis Sorted Set (ZSET) and sets its time-to-live (TTL).longReturns the number of records stored in the specified Redis Sorted Set (ZSET).net.bugreaper.core.assertable.AssertableStringListgrabRecordsByKey(String key) Returns all records stored in the specified Redis Sorted Set (ZSET).Methods inherited from class net.bugreaper.modules.redis.helpers.RedisHelper
attachAndLogList, checkKeyExists, checkKeyTypeIs, cleanAll, deleteKeysByPattern, execute, executeReturn, getConfigSummary, getKeyExistsStatus, getKeysCountByPattern, getKeyType, getPrefix, getTtlByKey, seeKeyExists, seeKeysCountByPatternIsExactly, seeKeysCountByPatternIsGreaterThan, seeKeysCountByPatternIsLessThan, seeKeyTypeIs, seeRecordsCountByKeyIsExactly, seeTtlIsGreaterThan, seeTtlIsLessThan, setAwaitMsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.bugreaper.modules.redis.interfaces.RedisHelperInt
cleanAll, deleteKeysByPattern, getKeyExistsStatus, getKeysCountByPattern, getKeyType, getTtlByKey, seeKeyExists, seeKeysCountByPatternIsExactly, seeKeysCountByPatternIsGreaterThan, seeKeysCountByPatternIsLessThan, seeKeyTypeIs, seeRecordsCountByKeyIsExactly, seeTtlIsGreaterThan, seeTtlIsLessThan
-
Constructor Details
-
RedisZSetHelper
This constructor initializes client for interaction with Redis ZSET data types- Parameters:
host- host of Redis"localhost"port- port of Redisusername- admin usernamepassword- 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:
addRecordin interfaceRedisHelperInt- Parameters:
keyWithScore- Redis ZSET key:scorevalue- 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:
addRecordin interfaceRedisHelperInt- Parameters:
keyWithScore- Redis ZSET key:scorevalue- record value to addttlSeconds- 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:
grabRecordsByKeyin interfaceRedisHelperInt- Parameters:
key- Redis ZSET key- Returns:
AssertableStringListlist of stored records- Throws:
AssertionError- if the key does not exist or not a Redis ZSET
-
getRecordsCountByKey
Returns the number of records stored in the specified Redis Sorted Set (ZSET).This method uses Redis
ZCARDcommand.Example:
users:admins ├── 100 -> record1 ├── 200 -> record2 getRecordsCountByKey("users:admins") returns 2- Specified by:
getRecordsCountByKeyin interfaceRedisHelperInt- Parameters:
key- Redis ZSET key- Returns:
- number of records in the ZSET
-