重磅官宣:Redis OM 發布,Redis 對象映射框架來了!
Redis OM
前幾天,Redis 發布了對象映射框架:Redis OM,即:Object Mapping,不過目前它還是預覽版。
當然,Redis OM 的橫空出世不僅僅是對象映射,它更多的是提供一個高級的抽象類庫,目標就是讓開發人員更簡單、方便的使用 Redis 數據。
Redis OM 支持的第一個抽象就是:對象映射,支持基于對象的 Redis 數據持久化、流式查詢操作。
目前只支持 4 種開發語言:
- Redis OM for Spring(Java)
- Redis OM for .NET
- Redis OM for Node.js
- Redis OM for Python
相信后續會支持更多語言。
Redis OM for Spring
Redis OM for Spring 是對咱們 Java 的支持的對象映射類庫。
棧長看了官方倉庫源碼說明,它其實就是擴展了 Spring Data Redis,可以提供更好的數據搜索、文檔模型等,可以理解為 MyBatis-plus 和 MyBatis 的關系,相輔相成的。
快速開始
Redis OM 可以和 Spring Boot 快速集成。
Maven 依賴:
- <dependency>
- <groupId>com.redis.om.spring</groupId>
- <artifactId>redis-om-spring</artifactId>
- <version>${version}</version>
- </dependency>
Spring Boot 配置:
- @SpringBootApplication
- @Configuration
- @EnableRedisDocumentRepositories(basePackages = "cn.javastack.documents.*")
- public class RdsDocumentsApplication {
- public static void main(String[] args) {
- SpringApplication.run(RdsDocumentsApplication.class, args);
- }
- }
使用 @EnableRedisDocumentRepositories 注釋掃描 @Document 注解的 Spring 模型,通過注入實現 RedisDocumentRepository 的 repositories bean,后面就可以進行 CRUD 和自定義查詢操作了。
Spring Boot 基礎就不介紹了,學習筆記分享給你,實戰倉庫源碼:https://github.com/javastacks/spring-boot-best-practice
對象模型映射
Redis OM 也是通過注解的方式映射對象模型,如下面示例:
- /**
- * 商戶操作倉庫
- * 作者:棧長
- * 來源公眾號:Java技術棧
- */
- @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;
- // ...
- }
注解說明:
- @Document: 將 Spring Data 模型映射到 Redis JSON 文檔;
- @Id: 使用 ULID 取代傳統的 UUID 主鍵生成策略,生成速度更快、更易用;
- @Searchable: 聲明全文搜索索引;
- @Indexable: 聲明索引;
定義倉庫
Spring Data Redis 這里就不詳細介紹了,棧長之前寫過一些,不會用的可以關注公眾號:Java技術棧,在公眾號菜單欄中閱讀,棧長都已經整理好了。
使用 Repository 倉庫也很簡單,繼承 RedisDocumentRepository 就行了:
- /**
- * 商戶操作倉庫
- * 作者:棧長
- * 來源公眾號:Java技術棧
- */
- public interface MerchantRepository extends RedisDocumentRepository<Merchant, String> {
- // 查找單個商戶
- Optional<Merchant> findMerchantByName(String name);
- }
使用倉庫
先注入 MerchantRepo 倉庫:
- @Autowired
- MerchantRepository merchantRepo;
然后就可以持久化數據和查詢操作了。
數據持久化:
- /**
- * 持久化數據
- * 作者:棧長
- * 來源公眾號:Java技術棧
- */
- @Bean
- CommandLineRunner initData() {
- return args -> {
- // 清空數據
- 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"));
- // 持久化數據
- merchantRepo.save(javastack1);
- merchantRepo.save(javastack2);
- };
- }
數據查詢:
- /**
- * 查找單個商戶
- * 作者:棧長
- * 來源公眾號:Java技術棧
- */
- @GetMapping("name/{name}")
- Optional<Merchant> byName(@PathVariable("name") String name) {
- return merchantRepo.findMerchantByName(name);
- }
Redis OM 全程都是通過對象的方式,更好的面向對象編程的思想,不需要多余的對象轉換操作了。
總結
Redis OM 對象映射只是擴展了 Spring Data Redis,它不能脫離 Spring Data Redis,更不能脫離其他 Redis 客戶端,它只是一個更高層級的抽象庫而已,可以使我們更簡單、方便的使用 Redis。
可以簡單的說它是 Redis 界的 Hibernate,真香!
現在還是預覽版,功能也不全,我們還是期待它的正式發布吧,棧長會第一時間跟進和體驗分享。
更多的細節可以參考:
https://redis.com/blog/introducing-redis-om-client-libraries/
https://github.com/redis/redis-om-spring
好了,今天的分享就到這里了,后面棧長會分享更多好玩的 Java 技術和最新的技術資訊,關注公眾號Java技術棧第一時間推送,我也將主流 Java 面試題和參考答案都整理好了,在公眾號后臺回復關鍵字 "面試" 進行刷題。
本文轉載自微信公眾號「Java技術?!?,可以通過以下二維碼關注。轉載本文請聯系Java技術棧公眾號。