Class RedisStringHelper
- All Implemented Interfaces:
RedisHelperInt
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:3are 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
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
ConstructorsConstructorDescriptionRedisStringHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis STRING data types -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new record to the Redis STRING key.voidAdds a new record to the Redis STRING key and sets expiration time.longReturns the number of records stored for the specified Redis STRING key.net.bugreaper.core.assertable.AssertableStringListgrabRecordsByKey(String key) Returns the record stored in the specified Redis STRING key.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
-
RedisStringHelper
This constructor initializes client for interaction with Redis STRING 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 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:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis STRING keyvalue- 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:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis STRING keyvalue- record value to storettlSeconds- 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
AssertableStringListkeeps the API consistent with other Redis helpers (LIST, HASH, etc.).- Specified by:
grabRecordsByKeyin interfaceRedisHelperInt- Parameters:
key- Redis STRING key- Returns:
AssertableStringListlist containing one record- Throws:
AssertionError- if the key does not exist or not a Redis LIST
-
getRecordsCountByKey
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:
getRecordsCountByKeyin interfaceRedisHelperInt- Parameters:
key- Redis STRING key- Returns:
1if key exists, otherwise0
- key exists -> returns
-