Heavy official announcement: Redis OM is released, and Redis object mapping framework is coming!!

Posted by mrjoseph.com on Mon, 29 Nov 2021 07:45:28 +0100

Redis OM

A few days ago, Redis released the Object Mapping framework: Redis OM, that is, Object Mapping, but it is still a preview version.

Of course, the birth of Redis OM is not only object mapping, but also provides an advanced abstract class library. The goal is to make it easier and convenient for developers to use Redis data.

The first abstraction supported by Redis OM is object mapping, which supports object-based Redis data persistence and streaming query operations.

Currently, only four development languages are supported:

  • Redis OM for Spring(Java)
  • Redis OM for .NET
  • Redis OM for Node.js
  • Redis OM for Python

I believe more languages will be supported in the future.

Redis OM for Spring

Redis OM for Spring is an object mapping class library that supports Java.

The stack leader read the source code description of the official warehouse. In fact, it extends Spring Data Redis to provide better data search and document model. It can be understood as the relationship between MyBatis plus and MyBatis, which complement each other.

Quick start

Redis OM can be quickly integrated with Spring Boot.

Maven dependency:

<dependency>
  <groupId>com.redis.om.spring</groupId>
  <artifactId>redis-om-spring</artifactId>
  <version>${version}</version>
</dependency>

Spring Boot configuration:

@SpringBootApplication
@Configuration
@EnableRedisDocumentRepositories(basePackages = "cn.javastack.documents.*")
public class RdsDocumentsApplication {

  public static void main(String[] args) {
      SpringApplication.run(RdsDocumentsApplication.class, args);
  }
  
}

Use the @ enablereedisdocumentrepositories annotation to scan the Spring model of @ Document annotation. Inject the repositories bean that implements RedisDocumentRepository, and then CRUD and user-defined query operations can be performed.

I won't introduce the basics of Spring Boot, Study notes Share with you the source code of actual combat warehouse:
https://github.com/javastacks...

Object model mapping

Redis OM also maps the object model through annotation, as shown in the following example:

/**
 * Merchant operation warehouse
 * Author: stack length
 * Official account: Java technology stack
 */
@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document
public class Merchant {

  @Id 
  private String id;
  
  @Searchable 
  private String name;
  
  @Indexed 
  private Point location;
  
  @Indexed 
  private Set<String> sites = new HashSet<String>();
  
  @Indexed 
  private Integer numberOfEmployees;
  
  @Indexed 
  private Integer yearFounded;
  
  private String url;
  
  private boolean publiclyListed;

  // ...
}

Notes:

  • @Document: Map Spring Data model to Redis JSON document;
  • @Id: use ULID to replace the traditional UUID primary key generation strategy, which is faster and easier to use;
  • @Searchable: declare full-text search index;
  • @Indexable: declare an index;

Define warehouse

Spring Data Redis here is not detailed, and some of the stack length has been written before, and it can not be used to official account: Java technology stack, reading in the official account menu bar, the stack length has been sorted out.

It is also easy to use the Repository repository. Just inherit RedisDocumentRepository:

/**
 * Merchant operation warehouse
 * Author: stack length
 * Official account: Java technology stack
 */
public interface MerchantRepository extends RedisDocumentRepository<Merchant, String> {

  // Find a single merchant
  Optional<Merchant> findMerchantByName(String name);
  
}

Use warehouse

First inject into the merchant repo warehouse:

@Autowired
MerchantRepository merchantRepo;

You can then persist data and query operations.

Data persistence:
/**
 * Persistent data
 * Author: stack length
 * Official account: Java technology stack
 */
@Bean
CommandLineRunner initData() {
    return args -> {
      // wipe data 
      merchantRepo.deleteAll();
      
      Merchant javastack1 = Merchant.of(
        "javastack1", "https://javastack.cn", new Point(-122.066540, 37.377690), 526, 2011 //
      );
      javastack1.setTags(Set.of("fast", "scalable", "reliable"));

      Merchant javastack2 = Merchant.of(
        "javastack2", "https://javastack.cn", new Point(-122.124500, 47.640160), 182268, 1975 //
      );
      javastack2.setTags(Set.of("innovative", "reliable"));
      
      // Persistent data
      merchantRepo.save(javastack1);
      merchantRepo.save(javastack2);
    };
}

Data query:

/**
 * Find a single merchant
 * Author: stack length
 * Official account: Java technology stack
 */
@GetMapping("name/{name}")
Optional<Merchant> byName(@PathVariable("name") String name) {
    return merchantRepo.findMerchantByName(name);
}

Redis OM uses object-oriented programming throughout the whole process. It has a better idea of object-oriented programming and does not need redundant object conversion operations.

summary

Redis OM object mapping only extends Spring Data Redis. It cannot be separated from Spring Data Redis or other redis clients. It is just a higher-level abstract library, which can make it easier and convenient for us to use redis.

It can be simply said that it is Hibernate in Redis. It's really fragrant!

Now it's still a preview version with incomplete functions. We'd better look forward to its official release. The stack leader will follow up and experience sharing at the first time.

For more details, please refer to:

https://redis.com/blog/introd...

https://github.com/redis/redi...

Well, today's sharing is here. The stack will share more interesting Java technology and latest technology information. We will pay attention to the Java push for the official account. I will also organize the main Java interview questions and reference answers. I will reply to the key words in the official account.

Finally, what do you think of Redis OM? Welcome to leave a message for discussion!

This is really a good framework. Welcome to forward and share it with more Javaer partners!

Copyright notice: This article is the public number "Java technology stack" original, original is not easy, reprint, quote the content of this article please indicate the source, all the plagiarism official account + complaint, and retain the right to pursue its legal responsibility.

Recent hot article recommendations:

1.1000 + Java interview questions and answers (2021 latest version)

2.Stop playing if/ else on the full screen. Try the strategy mode. It's really fragrant!!

3.what the fuck! What is the new syntax of xx ≠ null in Java?

4.Spring Boot 2.6 was officially released, a wave of new features..

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don't forget to like + forward!

Topics: Java