How to Search for Java Objects Stored in Redis
In this article, we’ll discuss how Redisson treats the topic of Java objects, and how you can use Redisson to search for Java objects in Redis.
Join the DZone community and get the full member experience.
Join For FreeWhat Is a Live Object in Java?
Java is an object-oriented programming language, which means there are few concepts more important in Java than objects. The power of objects in Java is enhanced with distributed objects, which enable you to build distributed systems across multiple processes or computers.
In Java, a live object (also known as a live distributed object) is an “enhanced” version of a standard object. With live objects, instance references can be shared not only between Java Virtual Machine (JVM) threads but also between different JVMs on different machines.
Redis is an open-source in-memory data structure store that is often used to build distributed NoSQL key-value databases. To use Java objects with Redis, developers make use of Redis Java clients such as Redisson.
You may also like: All About Object in Java
In this article, we’ll discuss how Redisson treats the topic of Java objects, and how you can use Redisson to search for Java objects in Redis.
Redisson Live Objects (RLOs)
Redisson Live Objects (RLOs) are a reimplementation of the concept of live objects for Redis. With an RLO, all the fields inside a Java class are mapped to a Redis hash. This mapping is done by a proxy class constructed at runtime.
More specifically, the get/set methods of each field in the Java class are translated into hget/hset methods in the Redis hash. This makes the RLO accessible to any client connected to the Redis server.
With RLOs, sharing objects between multiple applications and servers is just as easy as sharing them within a single application. By removing the need to handle serialization and deserialization, RLOs dramatically simplify the process of distributed programming.
Searching for Java Objects in Redis With Redisson
Below is an example of how to create a Java live object with Redisson. The @REntity and @RId annotations are required to use live objects.
public class MyObject {
private String id;
private String field1;
private Integer field2;
private Long field3;
}
Once you’ve created an RLO and stored it in Redis, you can also search for it using Redisson. The search conditions available include:
- Conditions.eq: "EQUALS" condition restricting a property to a defined value.
- Conditions.and: "AND" condition for a collection of nested conditions.
- Conditions.or: "OR" condition for a collection of nested conditions.
- Conditions.in: "IN" condition restricting a property to a set of defined values.
- Conditions.gt: "GREATER THAN" condition restricting a property to a defined value.
- Conditions.ge: "GREATER THAN OR EQUAL" condition restricting a property to a defined value.
- Conditions.lt: "LESS THAN" condition restricting a property to a defined value.
- Conditions.le: "LESS THAN OR EQUAL" condition restricting a property to a defined value.
Below is an example of how to search for Java live objects in Redisson. This example search will find all objects where field1 = value and field2 < 12, or where field1 = value and field2 > 23, or where field3 is in the range [1, 2].
xxxxxxxxxx
RLiveObjectService liveObjectService = redisson.getLiveObjectService();
liveObjectService.persist(new MyObject());
Collection<MyObject> objects = liveObjectService.find(MyObject.class,
Conditions.or(Conditions.and(Conditions.eq("field1", "value"), Conditions.lt("field2", 12)),
Conditions.and(Conditions.eq("field1", "value2"), Conditions.gt("field2", 23)),
Conditions.in("field3", 1L, 2L));
Redisson PRO implements a Redis search engine for Java objects that’s up to 10 times faster than the open-source version, all while lowering JVM memory consumption. To learn more about the features of Redisson PRO—including additional features, higher performance, and 24x7 technical support — check out the Redisson PRO website.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments