Class RedisSetHelper
java.lang.Object
net.bugreaper.modules.redis.helpers.RedisHelper<RedisSetHelper>
net.bugreaper.modules.redis.helpers.RedisSetHelper
- All Implemented Interfaces:
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:adminsis 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
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
ConstructorsConstructorDescriptionRedisSetHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis SET data types -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new record to the Redis SET.voidAdds a record to the specified Redis SET and sets its time-to-live (TTL).longReturns the number of records stored in the specified Redis SET.net.bugreaper.core.assertable.AssertableStringListgrabRecordsByKey(String key) Returns all records stored in the specified Redis SET.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
-
RedisSetHelper
This constructor initializes client for interaction with Redis SET data types- Parameters:
host- host of Redis"localhost"port- port of Redisusername- admin usernamepassword- admin password
-
-
Method Details
-
addRecord
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:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis SET keyvalue- 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:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis SET keyvalue- 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 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:
grabRecordsByKeyin interfaceRedisHelperInt- Parameters:
key- Redis SET key- Returns:
AssertableStringListlist of stored records- Throws:
AssertionError- if the key does not exist or not a Redis SET
-
getRecordsCountByKey
Returns the number of records stored in the specified Redis SET.This method uses Redis
SCARDcommand.Example:
users:admins ├── 0 -> record1 ├── 1 -> record2 getRecordsCountByKey("users:admins") returns 2- Specified by:
getRecordsCountByKeyin interfaceRedisHelperInt- Parameters:
key- Redis SET key- Returns:
- number of records in the SET
-