The Top 5 Redis-Based Java Objects

As the "most loved" database technology by developers, Redis has a lot of features to recommend it, including speed, scalability, and availability. However, one issue is that Redis doesn't include built-in support for programming languages like Java. Instead, users need to install a third-party Java client for Redis such as Redisson.

Redisson is a Redis Java client that includes many of the familiar Java objects, interfaces, and collections, making it easier than ever for Java developers to get started with Redis. Below, we'll discuss 5 of the top Redis-based Java objects that Redisson users have come to rely on.

1. Map

The Map interface in Java is used for objects that map keys to values.

Redisson implements Map in Redis with the RMap interface. RMap implements all of the familiar Map methods in Redis: containsKey(), containsValue(), get(), isEmpty(), keySet(), put(), putIfAbsent(), remove(), replace(), values(), and more.

Below is a quick example of how to use the RMap interface in Redisson:

RMap<String, SomeObject> map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");

The RMap interface includes support for Async, Reactive, and RxJava2. In addition, RMap has features such as local caching and data partitioning, so that you can massively speed up read operations and improve scalability.

2. Set

The Set interface in Java is used for unordered collections of elements without duplicate values.

Redisson implements Set in Redis with the RSet interface. RSet implements all of the familiar Set methods in Redis: add(), addAll(), clear(), contains(), containsAll(), equals(), isEmpty(), remove(), removeAll(), size(), and more.

Below is a quick example of how to use the RSet interface in Redisson:

RSet<SomeObject> set = redisson.getSet("anySet");
set.add(new SomeObject());
set.remove(new SomeObject());

Like RMap, RSet includes support for data partitioning in order to improve performance and speed up read/write operations. In addition, users can define the eviction policy for the elements in the RMap (i.e. the time to live for each entry).

3. Holder

The Holder class in Java is used to store a value of a specified type while allowing side-effect modifications of the value (for example, in order to implement call by reference within Java).

Redisson implements the Holder class in Redis with the RBucket interface, which can hold any type of object. RBucket implements methods such as compareAndSet(), get(), getAndDelete(), getAndSet(), set(), size(), trySet(), and more. The maximum size of the object is 512 megabytes.

Below is a quick example of how to use the RBucket interface in Redisson:

RBucket<AnyObject> bucket = redisson.getBucket("anyObject");

bucket.set(new AnyObject(1));
AnyObject obj = bucket.get();

bucket.trySet(new AnyObject(3));
bucket.compareAndSet(new AnyObject(4), new AnyObject(5));
bucket.getAndSet(new AnyObject(6));

The RBucket interface includes support for Async, Reactive, and RxJava2.

4. Lock

The Lock class in Java is used to "lock down" access to a shared resource in order to synchronize multiple threads. Locks are a crucial part of multithreaded and distributed programming, which is why they're also very popular among Redisson users.

Redisson implements the Lock class in Redis with the RLock interface. RLock implements all of the familiar Lock methods in Redis: lock(), lockInterruptibly(), tryLock(), unlock(), and more.

Below is a quick example of how to use the RLock interface in Redisson:

RLock lock = redisson.getLock("myLock");

// traditional lock method
lock.lock();

// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);

// or wait for lock aquisition up to 100 seconds 
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
   try {
     ...
   } finally {
       lock.unlock();
   }
}

The RLock interface includes support for Async, Reactive, and RxJava2. Redisson also implements various types of locks such as fair locks, MultiLocks, Redlocks, and ReadWriteLocks.

5. ExecutorService

The ExecutorService interface in Java is used to execute Runnable and Callable objects asynchronously.

Redisson implements the ExecutorService interface in Redis with the RExecutorService interface. RExecutorService implements all of the familiar ExecutorService methods in Redis: invokeAll(), invokeAny(), isShutdown(), isTerminated(), shutdown(), shutdownNow(), submit(), and more.

Below is a quick example of how to use the RExecutorService interface in Redisson:

RExecutorService executorService = redisson.getExecutorService("myExecutor", options);
executorService.submit(new RunnableTask(123));

RExecutorService executorService = redisson.getExecutorService("myExecutor", options);
Future<Long> future = executorService.submit(new CallableTask());
Long result = future.get();

The RExecutorService interface includes support for Runnable and Callable objects, Lambda tasks, and tasks with Spring beans.

Conclusion

Redisson includes built-in support for many of the most common Java objects in Redis, including Map, Set, Holder, Lock, and ExecutorService.

Similar articles