Class RedisHelper<T extends RedisHelper<T>>
- Type Parameters:
T- concrete Redis helper type used for fluent method chaining
- All Implemented Interfaces:
RedisHelperInt
- Direct Known Subclasses:
RedisHashHelper,RedisListHelper,RedisSetHelper,RedisStreamHelper,RedisStringHelper,RedisZSetHelper
Provides common Redis connection management and shared configuration for different Redis data type helpers such as List, Hash, and String.
This class uses self-referencing generics to support fluent API chaining for setters while preserving the concrete helper type.
Example:
RedisListHelper redis = new RedisListHelper(...)
.setAwaitMs(500);
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intdefault ms await for asserts and grabprotected redis.clients.jedis.JedisPoolprotected static final intdefault ms await for asserts and grab -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRedisHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis LIST data types -
Method Summary
Modifier and TypeMethodDescriptionprotected voidattachAndLogList(List<String> actualList, String key) protected voidcheckKeyExists(String key) protected final voidcheckKeyTypeIs(String key, String type) final voidcleanAll()Removes ALL data from current Redis database.final voiddeleteKeysByPattern(String keyPattern) Deletes Redis keys matching the specified pattern.protected voidprotected <R> RexecuteReturn(Function<redis.clients.jedis.Jedis, R> action) booleangetKeyExistsStatus(String key) Checks whether the specified Redis key exists.final longgetKeysCountByPattern(String keyPattern) Returns the number of Redis keys matching the specified pattern.final StringgetKeyType(String key) Returns the Redis data type of the specified key.protected static StringlonggetTtlByKey(String key) Returns the remaining TTL (time-to-live) of the specified Redis key.final voidseeKeyExists(String key) Asserts that the specified Redis key exists.final voidseeKeysCountByPatternIsExactly(String keyPattern, long expectedCount) Asserts that the number of Redis keys matching the specified pattern is exactly the expected count.final voidseeKeysCountByPatternIsGreaterThan(String keyPattern, long minCount) Asserts that the number of Redis keys matching the specified pattern is greater than the given value.final voidseeKeysCountByPatternIsLessThan(String keyPattern, long maxCount) Asserts that the number of Redis keys matching the specified pattern is less than the given value.final voidseeKeyTypeIs(String key, String type) Asserts that the specified Redis key has the expected data type.voidseeRecordsCountByKeyIsExactly(String key, long expectedCount) Asserts that the number of records in the Redis key is exactly the expected value.voidseeTtlIsGreaterThan(String key, int minTtl) Asserts that the key TTL is greater than the specified value.voidseeTtlIsLessThan(String key, int maxTtl) Asserts that the key TTL is less than the specified value.setAwaitMs(int awaitMs) Methods 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
addRecord, addRecord, getRecordsCountByKey, grabRecordsByKey
-
Field Details
-
jedisPool
protected redis.clients.jedis.JedisPool jedisPool -
awaitMs
protected volatile int awaitMsdefault ms await for asserts and grab -
SCAN_BATCH_SIZE
protected static final int SCAN_BATCH_SIZEdefault ms await for asserts and grab- See Also:
-
-
Constructor Details
-
RedisHelper
This constructor initializes client for interaction with Redis LIST data types- Parameters:
host- host of Redis"localhost"port- port of Redisusername- admin usernamepassword- admin password
-
-
Method Details
-
setAwaitMs
-
getConfigSummary
-
cleanAll
@Step("(Redis) Clean all data from Redis") public final void cleanAll()Description copied from interface:RedisHelperIntRemoves ALL data from current Redis database.WARNING: This operation deletes every key from selected Redis DB.
- Specified by:
cleanAllin interfaceRedisHelperInt
-
deleteKeysByPattern
@Step("(Redis) Delete keys by key-pattern <{keyPattern}>") public final void deleteKeysByPattern(String keyPattern) Description copied from interface:RedisHelperIntDeletes Redis keys matching the specified pattern.Uses Redis SCAN to safely find matching keys without blocking the Redis server.
Examples: users:admins (LIST) ├── 0 -> Alex └── 1 -> John users:developers (STRINGS) ├── 0 -> Michael └── 1 -> Max users:data (HASH) ├── 1 -> test1 └── 2 -> test2
deleteByKeyPattern("users:*");Deletes:
users:admins users:developers users:data
Also supports exact key deletion:
deleteByKeyPattern("users:admins");- Specified by:
deleteKeysByPatternin interfaceRedisHelperInt- Parameters:
keyPattern- Redis key pattern or exact key
-
getKeysCountByPattern
Description copied from interface:RedisHelperIntReturns the number of Redis keys matching the specified pattern.This method counts keys, not records inside.
Example:
users:admins -> LIST users:developers -> HASH users:me -> STRING getKeysCountByPattern("users:*") returns 3- Specified by:
getKeysCountByPatternin interfaceRedisHelperInt- Parameters:
keyPattern- Redis key pattern- Returns:
- number of matching Redis keys
-
getKeyType
Description copied from interface:RedisHelperIntReturns the Redis data type of the specified key.Possible return values:
stringlistsetzsethashstreamnone(key does not exist)
- Specified by:
getKeyTypein interfaceRedisHelperInt- Parameters:
key- Redis key- Returns:
- Redis data type or
noneif the key does not exist
-
getKeyExistsStatus
Description copied from interface:RedisHelperIntChecks whether the specified Redis key exists.The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.
- Specified by:
getKeyExistsStatusin interfaceRedisHelperInt- Parameters:
key- Redis key- Returns:
trueif the key exists, otherwisefalse
-
getTtlByKey
Description copied from interface:RedisHelperIntReturns the remaining TTL (time-to-live) of the specified Redis key.- Specified by:
getTtlByKeyin interfaceRedisHelperInt- Parameters:
key- Redis key- Returns:
- remaining TTL in seconds
-
seeKeyExists
Description copied from interface:RedisHelperIntAsserts that the specified Redis key exists.Uses await.
The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.
Example:
seeKeyExists("users:admins"); seeKeyExists("users:1");- Specified by:
seeKeyExistsin interfaceRedisHelperInt- Parameters:
key- Redis key
-
seeKeyTypeIs
@Step("(Redis)[ASSERT] Key <{key}> type is <{type}>") public final void seeKeyTypeIs(String key, String type) Description copied from interface:RedisHelperIntAsserts that the specified Redis key has the expected data type.Supported Redis data types:
stringlistsetzsethashstream
Examples:
seeKeyTypeIs("users:1", "string"); seeKeyTypeIs("users:admins", "list"); seeKeyTypeIs("users", "hash");- Specified by:
seeKeyTypeIsin interfaceRedisHelperInt- Parameters:
key- Redis keytype- expected Redis data type
-
seeTtlIsGreaterThan
@Step("(Redis)[ASSERT] Key <{key}> ttl greater {minTtl} seconds") public void seeTtlIsGreaterThan(String key, int minTtl) Description copied from interface:RedisHelperIntAsserts that the key TTL is greater than the specified value.- Specified by:
seeTtlIsGreaterThanin interfaceRedisHelperInt- Parameters:
key- Redis keyminTtl- minimum expected TTL in seconds
-
seeTtlIsLessThan
@Step("(Redis)[ASSERT] Key <{key}> ttl less {maxTtl} seconds") public void seeTtlIsLessThan(String key, int maxTtl) Description copied from interface:RedisHelperIntAsserts that the key TTL is less than the specified value.- Specified by:
seeTtlIsLessThanin interfaceRedisHelperInt- Parameters:
key- Redis keymaxTtl- maximum expected TTL in seconds
-
seeRecordsCountByKeyIsExactly
@Step("(Redis)[ASSERT] Key <{key}> has exactly {expectedCount} records") public void seeRecordsCountByKeyIsExactly(String key, long expectedCount) Description copied from interface:RedisHelperIntAsserts that the number of records in the Redis key is exactly the expected value.Uses await.
The returned records depend on the configured Redis data type:
- LIST - number of records stored in the specified Redis LIST
- HASH - number of records stored in the specified Redis HASH
- STRING - contains only one value per key if exist
- SET - number of records stored in the specified Redis SET
- ZSET - number of records stored in the specified Redis ZSET
- STREAM - number of records stored in the specified Redis STREAM
- Specified by:
seeRecordsCountByKeyIsExactlyin interfaceRedisHelperInt- Parameters:
key- Redis keyexpectedCount- expected number of records
-
seeKeysCountByPatternIsExactly
@Step("(Redis)[ASSERT] Keys count by key-pattern <{keyPattern}> is exactly {expectedCount}") public final void seeKeysCountByPatternIsExactly(String keyPattern, long expectedCount) Description copied from interface:RedisHelperIntAsserts that the number of Redis keys matching the specified pattern is exactly the expected count.Uses await.
- Specified by:
seeKeysCountByPatternIsExactlyin interfaceRedisHelperInt- Parameters:
keyPattern- Redis key patternexpectedCount- expected number of keys
-
seeKeysCountByPatternIsGreaterThan
@Step("(Redis)[ASSERT] Keys count by key-pattern <{keyPattern}> is greater {minCount}") public final void seeKeysCountByPatternIsGreaterThan(String keyPattern, long minCount) Description copied from interface:RedisHelperIntAsserts that the number of Redis keys matching the specified pattern is greater than the given value.Uses await.
- Specified by:
seeKeysCountByPatternIsGreaterThanin interfaceRedisHelperInt- Parameters:
keyPattern- Redis key patternminCount- minimum expected number of keys
-
seeKeysCountByPatternIsLessThan
@Step("(Redis)[ASSERT] Keys count by key-pattern <{keyPattern}> is less {maxCount}") public final void seeKeysCountByPatternIsLessThan(String keyPattern, long maxCount) Description copied from interface:RedisHelperIntAsserts that the number of Redis keys matching the specified pattern is less than the given value.Uses await.
- Specified by:
seeKeysCountByPatternIsLessThanin interfaceRedisHelperInt- Parameters:
keyPattern- Redis key patternmaxCount- maximum expected number of keys
-
checkKeyExists
-
checkKeyTypeIs
-
attachAndLogList
-
execute
-
executeReturn
-
getPrefix
-