重磅发布:Redis 对象映射框架来了,操作大大简化!

点击上方“芋道源码”,选择“设为星标

管她前浪,还是后浪?

能浪的浪,才是好浪!

每天 10:33 更新文章,每天掉亿点点头发...

源码精品专栏

 

89fad239f41e49be6b1f9a5269d970d3.png


前言

最近,Redis 官方博客宣布推出四个用于 Redis 的客户端工具库:Redis OM(对象映射库,Object Mapping),目标是让应用程序尽可能地容易使用 Redis 和 Redis 模块。

四个客户端工具库分别对应四种编程语言

  • Redis OM for .NET: .Net 平台的 Redis OM,依赖 StackExchange.Redis 实现。借助 Redis OM .NET 可以实现对象操作的方式操作 Redis 数据,脱离 key/value 的操作方式。查询支持大部分.Neter 最爱的 LINQ。

  • Redis OM for Node.js: 为 TypeScript 和 JavaScript 提供一级支持

  • Redis OM for Python: 原生集成流行的 FastAPI 框架,将 FastAPI 与 Redis 结合可构建高性能 Web 服务。Redis OM Python 库还支持同步和异步使用

  • Redis OM for Spring: 原生集成 Spring,扩展了 Spring Data Redis(提供熟悉的界面),添加了对 RedisBloo 的部分支持。

换言之,在之后的开发中,开发者可以通过 Redis OM 库直观地将域对象 (domain objects) 保存在 Redis,然后使用流畅的、以语言为中心的 API 进行查询。

推荐下自己做的 Spring Boot 的实战项目:

https://github.com/YunaiV/ruoyi-vue-pro

Redis OM for Spring

作为一枚java开发,我们重点了解一下 Redis OM for Spring。

简介

Redis OM for Spring提供了强大的存储库和基于强大的 Spring Data Redis (SDR) 框架构建的自定义对象映射抽象。

  • @Document 将 Spring Data 模型映射到 Redis JSON 文档的注释

  • @RedisHash通过以下方式增强 SDR @EnableRedisEnhancedRepositories

    • 使用 Redis 的原生搜索引擎 (RediSearch) 进行二级索引

    • 将ULID用于带@Id注释的字段

  • RedisDocumentRepository 自动实现存储库接口以实现复杂的查询功能,使用 @EnableRedisDocumentRepositories

  • 声明性搜索索引通过 @Indexable

  • 全文检索索引通过 @Searchable

  • @Bloom 注释可以非常快速地确定一个值是否在集合中。

实践

我们通过案例来了解一下Redis OM for Spring的具体用法

相关依赖

目前快照地址

<repositories>
    <repository>
      <id>snapshots-repo</id>
      <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
  </repositories>

pom文件

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

SpringBoot配置

package com.redis.om.documents;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.geo.Point;

import com.redis.om.documents.domain.Company;
import com.redis.om.documents.repositories.CompanyRepository;

@SpringBootApplication
@Configuration
@EnableRedisDocumentRepositories(basePackages = "com.redis.om.documents.*")
public class RomsDocumentsApplication {

  @Autowired
  CompanyRepository companyRepo;

  @Bean
  CommandLineRunner loadTestData() {
    return args -> {
      companyRepo.deleteAll();

      // 创建两组实体域 redis & microsoft
      Company redis = Company.of(
        "Redis", "https://redis.com", new Point(-122.066540, 37.377690), 526, 2011 
      );
      redis.setTags(Set.of("fast", "scalable", "reliable"));

      Company microsoft = Company.of(
        "Microsoft", "https://microsoft.com", new Point(-122.124500, 47.640160), 182268, 1975 
      );
      microsoft.setTags(Set.of("innovative", "reliable"));

      // 将创建的两组实体域持久化
      companyRepo.save(redis);
      companyRepo.save(microsoft);
    };
  }

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

整体使用上非常清晰,重点是类上开启@EnableRedisDocumentRepositories注解,就可以注入可用于 CRUD 操作和自定义查询的@Document存储库 bean。

实体对象映射

这个我们使用SpringBoot非常熟悉,Redis OM Spring 也提供@Document注释来为我们将模型保存为 JSON 文档

package com.redis.om.documents.domain;

import java.util.HashSet;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.geo.Point;
import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Searchable;
import lombok.*;

@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document
public class Company {
  @Id private String id;
  @Searchable private String name;
  @Indexed private Point location;
  @Indexed private Set<String> tags = new HashSet<String>();
  @Indexed private Integer numberOfEmployees;
  @Indexed private Integer yearFounded;
  private String url;
  private boolean publiclyListed;

  // ...
}

添加@Document注释即可,定义格式省去了自己做转换,Redis OM 库直观地将域对象 (domain objects) 保存在 Redis上。

接口使用

可以使用流畅的、以语言为中心的 API 进行查询,更符合我们平常的编写习惯,看一组官方给的实现案例:

package com.redis.om.documents.repositories;

import java.util.*;

import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.repository.query.Param;

import com.redis.om.documents.domain.Company;
import com.redis.om.spring.annotations.Query;
import com.redis.om.spring.repository.RedisDocumentRepository;

public interface CompanyRepository extends RedisDocumentRepository<Company, String> {
  // find one by property
  Optional<Company> findOneByName(String name);

  // geospatial query
  Iterable<Company> findByLocationNear(Point point, Distance distance);

  // find by tag field, using JRediSearch "native" annotation
  @Query("@tags:{$tags}")
  Iterable<Company> findByTags(@Param("tags") Set<String> tags);

  // find by numeric property
  Iterable<Company> findByNumberOfEmployees(int noe);

  // find by numeric property range
  Iterable<Company> findByNumberOfEmployeesBetween(int noeGT, int noeLT);

  // starting with/ending with
  Iterable<Company> findByNameStartingWith(String prefix);
}

推荐下自己做的 Spring Cloud 的实战项目:

https://github.com/YunaiV/onemall

总结

整体上,Redis OM Spring为我们节省了很多项目中整合Redis的步骤,可以更好的面向对象编程,省去不少数据格式的转换,同时也提供了我们更熟悉的API接口,大大的赞,不过这轮操作下来,Redis更像一个数据库了。

注意:Redis OM Spring 目前仅适用于 Jedis,同时 Redis OM 的一些高级特性依赖于两个可用 Redis 模块的核心特性:RediSearch和RedisJSON。

你对这个Redis 对象映射库期待吗?

地址:https://github.com/redis/redis-om-spring



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢

2de8b0f8d4e7f4d1d51694bfef7e1a41.png

已在知识星球更新源码解析如下:

2d4781d57080a9406cdf54aeb1e6c966.png

368e8f572b68a8861326bfcc4eeaf234.png

6f294b743ca8537776e94ba5bfe805a4.png

99abfb4ce7a1ed385bcf6c4fb9fb4218.png

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值