Redis官方ORM框架比RedisTemplate更優(yōu)雅
RedisOM簡(jiǎn)介
之前在SpringBoot項(xiàng)目中,我一直使用RedisTemplate來(lái)操作Redis中的數(shù)據(jù),這也是Spring官方支持的方式。對(duì)比Spring Data對(duì)MongoDB和ES的支持,這種使用Template的方式確實(shí)不夠優(yōu)雅!最近發(fā)現(xiàn)Redis官方新推出了Redis的專(zhuān)屬ORM框架RedisOM
,用起來(lái)夠優(yōu)雅,推薦給大家!
SpringBoot實(shí)戰(zhàn)電商項(xiàng)目mall(50k+star)地址:github.com/macrozheng/…
RedisOM是Redis官方推出的ORM框架,是對(duì)Spring Data Redis的擴(kuò)展。由于Redis目前已經(jīng)支持原生JSON對(duì)象的存儲(chǔ),之前使用RedisTemplate直接用字符串來(lái)存儲(chǔ)JOSN對(duì)象的方式明顯不夠優(yōu)雅。通過(guò)RedisOM我們不僅能夠以對(duì)象的形式來(lái)操作Redis中的數(shù)據(jù),而且可以實(shí)現(xiàn)搜索功能!
JDK 11安裝
由于目前RedisOM僅支持JDK 11
以上版本,我們?cè)谑褂们暗孟劝惭b好它。
- 首先下載
JDK 11
,下載地址:http://chabaoo.cn/softs/638448.html - 下載壓縮包版本即可,下載完成后解壓到指定目錄;
- 然后在IDEA的項(xiàng)目配置中,將對(duì)應(yīng)模塊的JDK依賴(lài)版本設(shè)置為
JDK 11
即可。
使用
接下來(lái)我們以管理存儲(chǔ)在Redis中的商品信息為例,實(shí)現(xiàn)商品搜索功能。注意安裝Redis的完全體版本RedisMod
,具體可以參考RediSearch 使用教程 。
- 首先在
pom.xml
中添加RedisOM相關(guān)依賴(lài);
<!--Redis OM 相關(guān)依賴(lài)--> <dependency> <groupId>com.redis.om</groupId> <artifactId>redis-om-spring</artifactId> <version>0.3.0-SNAPSHOT</version> </dependency>
- 由于RedisOM目前只有快照版本,還需添加快照倉(cāng)庫(kù);
<repositories> <repository> <id>snapshots-repo</id> <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url> </repository> </repositories>
- 然后在配置文件
application.yml
中添加Redis連接配置;
spring: redis: host: 192.168.3.105 # Redis服務(wù)器地址 database: 0 # Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0) port: 6379 # Redis服務(wù)器連接端口 password: # Redis服務(wù)器連接密碼(默認(rèn)為空) timeout: 3000ms # 連接超時(shí)時(shí)間
- 之后在啟動(dòng)類(lèi)上添加
@EnableRedisDocumentRepositories
注解啟用RedisOM的文檔倉(cāng)庫(kù)功能,并配置好文檔倉(cāng)庫(kù)所在路徑;
@SpringBootApplication @EnableRedisDocumentRepositories(basePackages = "com.macro.mall.tiny.*") public class MallTinyApplication { public static void main(String[] args) { SpringApplication.run(MallTinyApplication.class, args); } }
- 然后創(chuàng)建商品的文檔對(duì)象,使用
@Document
注解標(biāo)識(shí)其為文檔對(duì)象,由于我們的搜索信息中包含中文,我們需要設(shè)置語(yǔ)言為chinese
;
/** * 商品實(shí)體類(lèi) * Created by macro on 2021/10/12. */ @Data @EqualsAndHashCode(callSuper = false) @Document(language = "chinese") public class Product { @Id private Long id; @Indexed private String productSn; @Searchable private String name; @Searchable private String subTitle; @Indexed private String brandName; @Indexed private Integer price; @Indexed private Integer count; }
分別介紹下代碼中幾個(gè)注解的作用;
@Id
:聲明主鍵,RedisOM將會(huì)通過(guò)全類(lèi)名:ID
這樣的鍵來(lái)存儲(chǔ)數(shù)據(jù);@Indexed
:聲明索引,通常用在非文本類(lèi)型上;@Searchable
:聲明可以搜索的索引,通常用在文本類(lèi)型上。
接下來(lái)創(chuàng)建一個(gè)文檔倉(cāng)庫(kù)接口,繼承RedisDocumentRepository
接口;
/** * 商品管理Repository * Created by macro on 2022/3/1. */ public interface ProductRepository extends RedisDocumentRepository<Product, Long> { }
- 創(chuàng)建測(cè)試用的Controller,通過(guò)
Repository
實(shí)現(xiàn)對(duì)Redis中數(shù)據(jù)的創(chuàng)建、刪除、查詢(xún)及分頁(yè)功能;
/** * 使用Redis OM管理商品 * Created by macro on 2022/3/1. */ @RestController @Api(tags = "ProductController", description = "使用Redis OM管理商品") @RequestMapping("/product") public class ProductController { @Autowired private ProductRepository productRepository; @ApiOperation("導(dǎo)入商品") @PostMapping("/import") public CommonResult importList() { productRepository.deleteAll(); List<Product> productList = LocalJsonUtil.getListFromJson("json/products.json", Product.class); for (Product product : productList) { productRepository.save(product); } return CommonResult.success(null); } @ApiOperation("創(chuàng)建商品") @PostMapping("/create") public CommonResult create(@RequestBody Product entity) { productRepository.save(entity); return CommonResult.success(null); } @ApiOperation("刪除") @PostMapping("/delete/{id}") public CommonResult delete(@PathVariable Long id) { productRepository.deleteById(id); return CommonResult.success(null); } @ApiOperation("查詢(xún)單個(gè)") @GetMapping("/detail/{id}") public CommonResult<Product> detail(@PathVariable Long id) { Optional<Product> result = productRepository.findById(id); return CommonResult.success(result.orElse(null)); } @ApiOperation("分頁(yè)查詢(xún)") @GetMapping("/page") public CommonResult<List<Product>> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) { Pageable pageable = PageRequest.of(pageNum - 1, pageSize); Page<Product> pageResult = productRepository.findAll(pageable); return CommonResult.success(pageResult.getContent()); } }
- 當(dāng)我們啟動(dòng)項(xiàng)目時(shí),可以發(fā)現(xiàn)RedisOM會(huì)自動(dòng)為文檔建立索引;
- 接下來(lái)我們?cè)L問(wèn)Swagger進(jìn)行測(cè)試,先使用
導(dǎo)入商品
接口導(dǎo)入數(shù)據(jù),訪問(wèn)地址:http://localhost:8088/swagger-ui/
- 導(dǎo)入成功后我們可以發(fā)現(xiàn)RedisOM已經(jīng)向Redis中插入了原生JSON數(shù)據(jù),以全類(lèi)名:ID的形式命名了鍵,同時(shí)將全部的ID存儲(chǔ)到了一個(gè)SET集合中去了;
- 我們可以通過(guò)ID來(lái)查詢(xún)商品信息;
- 當(dāng)然RedisOM也是支持衍生查詢(xún)的,通過(guò)我們創(chuàng)建的方法名稱(chēng)就可以自動(dòng)實(shí)現(xiàn)查詢(xún)邏輯,比如根據(jù)品牌名稱(chēng)查詢(xún)商品,根據(jù)名稱(chēng)和副標(biāo)題關(guān)鍵字來(lái)搜索商品;
/** * 商品管理Repository * Created by macro on 2022/3/1. */ public interface ProductRepository extends RedisDocumentRepository<Product, Long> { /** * 根據(jù)品牌名稱(chēng)查詢(xún) */ List<Product> findByBrandName(String brandName); /** * 根據(jù)名稱(chēng)或副標(biāo)題搜索 */ List<Product> findByNameOrSubTitle(String name, String subTitle); }
- 在Controller中可以添加如下接口進(jìn)行測(cè)試;
/** * 使用Redis OM管理商品 * Created by macro on 2022/3/1. */ @RestController @Api(tags = "ProductController", description = "使用Redis OM管理商品") @RequestMapping("/product") public class ProductController { @Autowired private ProductRepository productRepository; @ApiOperation("根據(jù)品牌查詢(xún)") @GetMapping("/getByBrandName") public CommonResult<List<Product>> getByBrandName(String brandName) { List<Product> resultList = productRepository.findByBrandName(brandName); return CommonResult.success(resultList); } @ApiOperation("根據(jù)名稱(chēng)或副標(biāo)題搜索") @GetMapping("/search") public CommonResult<List<Product>> search(String keyword) { List<Product> resultList = productRepository.findByNameOrSubTitle(keyword, keyword); return CommonResult.success(resultList); } }
- 我們可以通過(guò)品牌名稱(chēng)來(lái)查詢(xún)商品;
- 也可以通過(guò)關(guān)鍵字來(lái)搜索商品;
- 這類(lèi)根據(jù)方法名稱(chēng)自動(dòng)實(shí)現(xiàn)查詢(xún)邏輯的衍生查詢(xún)有什么規(guī)則呢,具體可以參考下表。
總結(jié)
今天體驗(yàn)了一把RedisOM,用起來(lái)確實(shí)夠優(yōu)雅,和使用Spring Data來(lái)操作MongoDB和ES的方式差不多。不過(guò)目前RedisOM只發(fā)布了快照版本,期待Release版本的發(fā)布,而且Release版本據(jù)說(shuō)會(huì)支持JDK 8
的!
如果你想了解更多Redis實(shí)戰(zhàn)技巧的話(huà),可以試試這個(gè)帶全套教程的實(shí)戰(zhàn)項(xiàng)目(50K+Star):github.com/macrozheng/…
參考資料
- 項(xiàng)目地址:github.com/redis/redis…
- 官方文檔:developer.redis.com/develop/jav…
項(xiàng)目源碼地址github.com/macrozheng/…
以上就是Redis官方ORM框架比RedisTemplate更優(yōu)雅的詳細(xì)內(nèi)容,更多關(guān)于Redis官方ORM框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Redis中哈希結(jié)構(gòu)(Dict)的實(shí)現(xiàn)
本文主要介紹了Redis中哈希結(jié)構(gòu)(Dict)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06深入解析Redis的LRU與LFU算法實(shí)現(xiàn)
這篇文章主要重點(diǎn)介紹了Redis的LRU與LFU算法實(shí)現(xiàn),并分析總結(jié)了兩種算法的實(shí)現(xiàn)效果以及存在的問(wèn)題,并闡述其優(yōu)劣特性,感興趣的小伙伴跟著小編一起來(lái)看看吧2023-07-07redis中key使用冒號(hào)分隔的原理小結(jié)
Redis是一種高性能的鍵值對(duì)非關(guān)系型數(shù)據(jù)庫(kù),通過(guò)redis不同類(lèi)型命令可以為其中的鍵指定不同的數(shù)據(jù)類(lèi)型,其中每個(gè)鍵的命名規(guī)范通常使用冒號(hào)符號(hào)分隔字符串,本文主要介紹了redis中key使用冒號(hào)分隔的原理小結(jié),感興趣的可以了解一下2024-01-01Redis數(shù)據(jù)結(jié)構(gòu)之intset整數(shù)集合使用學(xué)習(xí)
這篇文章主要為大家介紹了Redis數(shù)據(jù)結(jié)構(gòu)之整數(shù)集合使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07redis實(shí)現(xiàn)計(jì)數(shù)器-防止刷單方法介紹
本文主要向大家介紹了redis實(shí)現(xiàn)計(jì)數(shù)器防止刷單的方法和有關(guān)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Redis?異常?read?error?on?connection?的解決方案
這篇文章主要介紹了Redis異常read?error?on?connection的解決方案,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08壓縮列表犧牲速度來(lái)節(jié)省內(nèi)存,Redis是膨脹了嗎
這篇文章主要給大家解釋了Redis 當(dāng)中的 ziplist(壓縮列表)犧牲速度來(lái)節(jié)省內(nèi)存的原因,希望大家能夠喜歡2021-02-02Mac中Redis服務(wù)啟動(dòng)時(shí)錯(cuò)誤信息:NOAUTH Authentication required
這篇文章主要介紹了Mac中使用Redis服務(wù)啟動(dòng)時(shí)錯(cuò)誤信息:"NOAUTH Authentication required"問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08