public class Redis extends Object implements sirius.kernel.Startable, sirius.kernel.Stoppable
The connection parameters are setup using the system configuration. Most probably a value for redis.host is all that is required. Another good practice is to use redis.db=1 for developement system so that they can run in parallel with a test system (which uses the default database 0 - unless configured otherwise).
Modifier and Type | Class and Description |
---|---|
static class |
Redis.LockInfo
Data object for storing information of a redis lock
|
Modifier and Type | Field and Description |
---|---|
protected sirius.kernel.health.Average |
callDuration |
static String |
INFO_USED_MEMORY
Contains the entry name of the info section under which redis reports the amount of consumed ram
|
protected redis.clients.jedis.JedisPool |
jedis |
static sirius.kernel.health.Log |
LOG |
protected sirius.kernel.health.Average |
messageDuration |
protected redis.clients.jedis.JedisSentinelPool |
sentinelPool |
Constructor and Description |
---|
Redis() |
Modifier and Type | Method and Description |
---|---|
protected Optional<Redis.LockInfo> |
computeLockInfo(redis.clients.jedis.Jedis redis,
String key) |
void |
exec(Supplier<String> description,
Consumer<redis.clients.jedis.Jedis> task)
Executes one or more Redis commands without any return value.
|
Map<String,String> |
getInfo()
Returns a map of monitoring info about the redis server.
|
List<Redis.LockInfo> |
getLockList()
Returns a list of all currently held locks.
|
int |
getPriority() |
protected void |
handlePubSubMessage(String channel,
String message,
Subscriber subscriber) |
boolean |
isConfigured()
Determines if access to Redis is configured.
|
boolean |
isLocked(String lock)
Determines if the given lock is currently locked by this or another node.
|
String |
pollQueue(String queue)
Polls an element off a queue in Redis.
|
void |
publish(String topic,
String message)
Broadcasts a message to a pub-sub topic in redis.
|
void |
pushToQueue(String queue,
String data)
Pushes a piece of data to a queue in Redis.
|
<T> T |
query(Supplier<String> description,
Function<redis.clients.jedis.Jedis,T> task)
Executes one or more Redis commands and returns a value of the given type.
|
void |
started() |
void |
stopped() |
boolean |
tryLock(String lock,
Duration acquireTimeout,
Duration lockTimeout)
Tries to acquire the given lock in the given timeslot.
|
void |
tryLocked(String lock,
Duration acquireTimeout,
Duration lockTimeout,
Runnable lockedTask)
Boilerplate method to perform the given task while holding the given lock.
|
void |
unlock(String lock)
Releases the lock.
|
void |
unlock(String lock,
boolean force)
Releases the given lock.
|
public static final sirius.kernel.health.Log LOG
protected sirius.kernel.health.Average callDuration
protected sirius.kernel.health.Average messageDuration
protected redis.clients.jedis.JedisPool jedis
protected redis.clients.jedis.JedisSentinelPool sentinelPool
public static final String INFO_USED_MEMORY
protected void handlePubSubMessage(String channel, String message, Subscriber subscriber)
public int getPriority()
getPriority
in interface sirius.kernel.di.std.Priorized
getPriority
in interface sirius.kernel.Startable
getPriority
in interface sirius.kernel.Stoppable
public void started()
started
in interface sirius.kernel.Startable
public void stopped()
stopped
in interface sirius.kernel.Stoppable
public boolean isConfigured()
public <T> T query(Supplier<String> description, Function<redis.clients.jedis.Jedis,T> task)
T
- the generic type of the resultdescription
- a description of the actions performed used for debugging and tracingtask
- the actual task to perform using redispublic void exec(Supplier<String> description, Consumer<redis.clients.jedis.Jedis> task)
description
- a description of the actions performed used for debugging and tracingtask
- the actual task to perform using redispublic void pushToQueue(String queue, String data)
queue
- the name of the queuedata
- the data to push@Nullable public String pollQueue(String queue)
queue
- the name of the queuepublic void publish(String topic, String message)
topic
- the name of the topic to broadcast tomessage
- the message to sendpublic Map<String,String> getInfo()
public List<Redis.LockInfo> getLockList()
This is mainly inteded to be used for monitoring and maintenance (e.g. RedisCommand
)
protected Optional<Redis.LockInfo> computeLockInfo(redis.clients.jedis.Jedis redis, String key)
public boolean tryLock(@Nonnull String lock, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout)
The system will try to acquire the given lock. If the lock is currently in use, it will retry in regular intervals until either the lock is acquired or the acquireTimeout is over.
A sane value for the timeout might be in the range of 5-50s, highly depending on the algorithm being protected by the lock. If the value is null, no retries will be performed.
The lockTimeout controls the max. age of the lock. After the given period, the lock will be released, even if unlock wasn't called. This is to prevent a cluster from locking itself out due to a single node crash. However, it is very important to chose a sane value here.
lock
- the name of the lock to acquireacquireTimeout
- the max duration during which retires (in 1 second intervals) will be performedlockTimeout
- the max duration for which the lock will be kept before auto-releasing itpublic void tryLocked(@Nonnull String lock, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout, @Nonnull Runnable lockedTask)
See tryLock(String, Duration, Duration)
for details on acquiring a lock.
If the lock cannot be acquired, nothing will happen (neighter the task will be execute nor an exception will be thrown).
lock
- the name of the lock to acquireacquireTimeout
- the max duration during which retires (in 1 second intervals) will be performedlockTimeout
- the max duration for which the lock will be kept before auto-releasing itlockedTask
- the task to execute while holding the given lock. The task will not be executed if the
lock cannot be acquired within the given periodpublic boolean isLocked(@Nonnull String lock)
lock
- the lock to checkpublic void unlock(String lock)
lock
- the lock to releasepublic void unlock(String lock, boolean force)
lock
- the lock to releaseforce
- if true, the lock will even be released if it is held by another node. This is a very
dangerous operation and should only be used by maintenance and management tools like RedisCommand
.Copyright © 2018. All rights reserved.