Class RedisHashHelper
- All Implemented Interfaces:
RedisHelperInt
This class provides convenient methods for common Redis HASH operations, including adding records, reading records, counting records, checking TTL, and cleaning hash data.
Redis HASH is a collection of field-value pairs, similar to a
Map<String, String>.
Example of stored records:
users
├── 1 -> {"id":1,"name":"Alex"}
├── 2 -> {"id":2,"name":"John"}
└── 3 -> {"id":3,"name":"Michael"}
In this example:
usersis the Redis HASH key1,2,3are HASH fields (record identifiers)- JSON objects are HASH field values stored as strings
- The order of records is not guaranteed because Redis HASH is unordered
Notes:
- TTL is applied to the whole HASH key, not individual fields.
- Records are stored as Redis strings inside the HASH. JSON values can be stored as plain strings.
- HASH fields cannot have individual expiration times.
- 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
ConstructorsConstructorDescriptionRedisHashHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis HASH data types -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new record to the Redis HASH.voidAdds a new record to the Redis HASH and sets expiration time for the key.longReturns the number of records stored in the specified Redis HASH.net.bugreaper.core.assertable.AssertableStringListgrabRecordsByKey(String key) Returns all records stored in the specified Redis HASH.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
-
RedisHashHelper
This constructor initializes client for interaction with Redis HASH 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:id <{keyWithId}>") public void addRecord(String keyWithId, String value) Adds a new record to the Redis HASH.The record is stored as a HASH field-value pair. The field value contains the record data (for example, JSON string).
Example:
users:admins ├── 1 -> Alex └── 2 -> John addRecord("users:admins:1", "Alex");- Specified by:
addRecordin interfaceRedisHelperInt- Parameters:
keyWithId- Redis HASH key key:idvalue- record value to add- Throws:
RedisHelperException- if no id provided in keyWithId
-
addRecord
@Step("(Redis) Insert record into key:id <{keyWithId}> with ttl: <{ttlSeconds}>") public void addRecord(String keyWithId, String value, long ttlSeconds) Adds a new record to the Redis HASH and sets expiration time for the key.TTL is applied to the whole HASH, not to an individual field/record.
Example:
users:admins ├── 1 -> Alex └── 2 -> John addRecord("users:admins:1", "Alex", 300);- Specified by:
addRecordin interfaceRedisHelperInt- Parameters:
keyWithId- Redis HASH key:idvalue- record value to addttlSeconds- expiration time in seconds- Throws:
RedisHelperException- if no id provided in keyWithId
-
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 HASH.Uses await until the key exists.
The returned list contains HASH values only. HASH fields are not returned.
Example:
users:admins ├── 0 -> "Alex" ├── 1 -> "John"
Redis HASH does not guarantee record ordering.
- Specified by:
grabRecordsByKeyin interfaceRedisHelperInt- Parameters:
key- Redis HASH key- Returns:
AssertableStringListlist of stored records- Throws:
AssertionError- if the key does not exist or not a Redis HASH
-
getRecordsCountByKey
Returns the number of records stored in the specified Redis HASH.This method counts HASH fields using Redis
HLENcommand.Example:
users:admins ├── 1 -> record1 ├── 2 -> record2 getRecordsCountByKey("users:admins") returns 2- Specified by:
getRecordsCountByKeyin interfaceRedisHelperInt- Parameters:
key- Redis HASH key- Returns:
- number of records stored in the HASH
-