|
Name |
RedisService |
|
Class Path |
|
|
Versions |
9 10 |
Overview
The RedisService class provides integration with Redis for caching and key-value storage. It supports:
-
Connecting to Redis using a single node or a Sentinel-based setup.
-
Basic caching operations (
put,get,remove). -
Handling encryption for Redis credentials.
-
Managing Redis connections and resource lifecycle.
Available Functions
Lifecycle Management
start(): void
Initializes the Redis connection pool based on the configured host or sentinel settings.
-
Throws:
-
ConfigurationExceptionif no Redis host or sentinels are configured.
-
stop(): void
Stops the Redis connection pool and releases resources.
Connection Management
Jedis getResource(): Jedis
Gets a Redis connection from the pool.
-
Returns:
-
Jedis: A Redis client connection.
-
void clearResource(Jedis jedis): void
Releases a Redis connection.
-
Parameters:
-
jedis(Jedis): The Redis connection to release.
-
void clearResource(Jedis jedis, boolean error): void
Releases a Redis connection, optionally marking it as an error.
-
Parameters:
-
jedis(Jedis): The Redis connection. -
error(boolean): Iftrue, logs an error when clearing the resource.
-
Cache Operations
void put(String key, Object o): void
Stores a key-value pair in Redis.
-
Parameters:
-
key(String): The cache key. -
o(Object): The value to store.
-
Object get(String key): Object
Retrieves a value from Redis by key.
-
Parameters:
-
key(String): The cache key.
-
-
Returns:
-
Object: The stored value ornullif not found.
-
Object get(String key, Number timeout): Object
Retrieves a value from Redis with a placeholder timeout (not implemented).
-
Parameters:
-
key(String): The cache key. -
timeout(Number): The timeout (not used).
-
-
Returns:
-
Object: The stored value ornullif not found.
-
Object remove(String key): Object
Deletes a key from Redis.
-
Parameters:
-
key(String): The cache key.
-
-
Returns:
-
Object: The number of keys removed.
-
Configuration Methods
void setHost(String host): void
Sets the Redis host.
-
Parameters:
-
host(String): The Redis server host.
-
void setPassword(String password): void
Sets and decrypts the Redis password if encrypted.
-
Parameters:
-
password(String): The password.
-
void setMaster(String master): void
Sets the master node name when using Redis Sentinel.
-
Parameters:
-
master(String): The master name.
-
void setSentinels(String sentinelsString): void
Sets Redis Sentinel nodes from a semicolon-separated string.
-
Parameters:
-
sentinelsString(String): The Redis Sentinel addresses.
-
-
Throws:
-
RuntimeExceptionif no sentinels are provided.
-
void setTimeoutInSec(int sec): void
Sets the default TTL (time-to-live) for cache keys.
-
Parameters:
-
sec(int): Timeout in seconds.
-
Advanced Methods
Object invoke(Function function): Object
Executes a JavaScript function within a Redis connection.
-
Parameters:
-
function(Function): A JavaScript function to execute.
-
-
Returns:
-
Object: The result of the function.
-
void invalidate(): void
Invalidates the cache (not implemented).
-
Throws:
-
IllegalArgumentExceptionwhen called.
-
Summary
The RedisService class manages Redis connections and provides basic caching functionality. It supports:
-
Redis Sentinel and standalone Redis connections.
-
Secure password handling with decryption.
-
JavaScript execution within Redis.
-
Key-value operations with TTL support.
This service is useful for applications requiring caching, distributed state management, or Redis-based storage.