Common methods
1. Object name
Name of Redisson object stored as a key in Redis or Valkey.
Example:
2. Common methods
Each Redisson object implements RObject and RExpirable interfaces.
Below are the most commonly used methods.
RObject object = ...
// Copy methods
object.copy("myNewCopy");
object.copyAndReplace("myNewCopy");
// Delete methods
object.delete();
object.unlink(); // works faster because it's executed on the database side in a different thread
// Rename methods
object.rename("myNewName");
object.renamenx("myNewName"); // rename only if the new key doesn't exist
// Dump and restore methods
byte[] state = object.dump();
object.restore(state);
3. Listeners per Redisson object instance
Listeners can be attached per Redisson object instance. Base listeners are ExpiredObjectListener
and DeletedObjectListener
. Redisson objects may support specific listeners. Like RScoredSortedSet, RStream and others.
RObject object = ...
// listening to expired events
object.addListener((ExpiredObjectListener) name -> {
//...
});
// listening to delete events
object.addListener((DeletedObjectListener) name -> {
//...
});
4. Operations over all Redisson object instances
Operations over all objects are exposed by RKeys interface.
Usage example:
RKeys keys = redisson.getKeys();
// Keys iteration
Iterable<String> allKeys = keys.getKeys();
Iterable<String> foundedKeys = keys.getKeys(KeysScanOptions.defaults().pattern("key*"));
String randomKey = keys.randomKey();
long keysAmount = keys.count();
long keysAmount = keys.countExists("obj1", "obj2", "obj3"); // amount of existing keys
// Delete methods
long delKeys = keys.delete("obj1", "obj2", "obj3");
long delKeys = keys.deleteByPattern("test?");
long delKeys = keys.unlink("obj1", "obj2", "obj3"); // works faster because it's executed on the database side in a different thread
long delKeys = keys.unlinkByPattern("test?"); // works faster because it's executed on the database side in a different thread
keys.flushall(); // Delete all keys of all existing databases
keys.flushallParallel(); // Delete all keys of all existing databases in background without blocking server
keys.flushdb(); // Delete all keys of currently selected database
5. Global listeners
Global listeners are attached to all Redisson object instances.
Available listeners:
- TrackingListener,
- SetObjectListener,
- NewObjectListener,
- ExpiredObjectListener,
- DeletedObjectListener,
- FlushListener
Usage example:
RKeys keys = redisson.getKeys();
int id = keys.addListener((NewObjectListener) name -> {
//...
});
int id = keys.addListener((DeletedObjectListener) name -> {
//...
});
// Flush listener is executed on flushall/flushdb commands execution.
int id = keys.addListener((FlushListener) address -> {
//...
});
keys.removeListener(id);