当前位置:网站首页>Ding Dong, here comes the redis om object mapping framework
Ding Dong, here comes the redis om object mapping framework
2022-07-02 07:17:00 【Software development heart】
Ding Dong ,Redis OM Here comes the object mapping framework !
One 、Redis OM coming
11 month 23 Japan ,redis Announced four new Redis Advanced client preview , These libraries are called Redis OM(Redis Object Mapping)----- Mapping objects .
Its framework with powerful Spring Data Redis (SDR) Framework based , Provide a high-level Abstract toolbox , We are familiar with object-oriented programming to express .
Its purpose is to focus on object mapping and smooth query , That is, use it as easily as possible Redis . It can transparently save domain objects in Redis in , And can use fluent 、 Language centered API Query them .
Redis OM Some of the advanced features of depend on the two available Redis The core function of the module :RediSearch and RedisJSON.
as follows , Launched a pair of .NET、Node.js、Python and Java (Spring) Language support
- Redis OM for .NET
- Redis OM for Node.js
- Redis OM for Python
- Redis OM for Spring
Two 、 understand
Here are Redis OM for Spring For example, by viewing the source code demo:rds-documents Module simulation to further understand Redis OM
Download the official package :redis-om-spring-main
development environment :JDK11
springboot edition :2.6.0-M1
notes :Redis OM The abstraction of is built on spring-data-redis (SDR) above
Code parsing
1、 Mapping objects
Through annotation @Document
take Spring Data Object to Redis JSON@Indexable
Declarative search @Searchable
Full text search index @RequiredArgsConstructor(staticName = "of")
Indicates the pass method of() To build objects @NonNull
Can't be empty
Add :@Bloom
Determine whether a value is in the set very quickly
/** * place */
@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document
public class Address {
@Id
private String id;
@NonNull
@Searchable
private String addressName;
@Indexed
private Set<String> addressTags = new HashSet<String>();
@NonNull
private String picUrl;
@NonNull
@Indexed
private Point location;
@NonNull
@Indexed
private Integer yearRegister;
private boolean publiclyListed;
}
2、 Object persistence
By expanding the interface RedisDocumentRepository To carry out CRUD operation , similar JPA Custom query of .
Through inheritance spring data The interface in :PagingAndSortingRepository
/** * By inheritance RedisDocumentRepository To carry out CRUD Operation and similar JPA Custom query of */
public interface AddressRepository extends RedisDocumentRepository<Address, String> {
// find one by property adopt addressName Inquire about
Optional<Address> findOneByAddressName(String name);
// find by tag field, using JRediSearch "native" annotation
@Query("@tags:{$tags}")
Iterable<Address> findByTags(@Param("tags") Set<String> tags);
}
Query definition
- By deriving the query directly from the method name .
- By using @Query or @Aggregation Annotations use manually defined queries .
3、 And spring Integration and use of
Through annotation @EnableRedisDocumentRepositories
Scan the specified package with @Document
Annotated object , Inject RedisDocumentRepository, You can use CRUD Operate custom query @Document
The annotations Bean
/** * Starting entrance */
@SpringBootApplication
@Configuration
@com.redis.om.spring.annotations.EnableRedisDocumentRepositories(basePackages = "com.redis.documents.*")
public class RdsDocumentsApplication {
public static void main(String[] args) {
SpringApplication.run(RdsDocumentsApplication.class, args);
}
...
}
4、 Save the data
@Autowired
private AddressRepository addressRepository;
@Bean
CommandLineRunner loadTestData() {
return args -> {
// Delete data
addressRepository.deleteAll();
//save Save the data
addressRepository.save(Address.of(" Guangzhou vipshop address ", "https://www.vip.com/?wq=1", new Point(113.124500, 23.640160), 2000));
addressRepository.save(Address.of(" Baidu address in Guangzhou ", "https://www.baidu.com/", new Point(113.235614, 23.342545), 2020));
};
}
5、 Interface call
@RestController
@RequestMapping("/api/address")
public class AddressController {
@Autowired
private AddressRepository addressRepository;
@GetMapping("name/{name}")
Optional<Address> byAddressName(@PathVariable("name") String name) {
return addressRepository.findOneByAddressName(name);// Method name derived query
}
@GetMapping("tags")
Iterable<Address> byTags(@RequestParam("tags") Set<String> tags) {
return addressRepository.findByTags(tags);// Method name derived query
}
@GetMapping("all")
Page<Address> all(Pageable pageable) {
return addressRepository.findAll(pageable);//spring data Bring their own
}
@GetMapping("{id}")
Optional<Address> byId(@PathVariable("id") String id) {
return addressRepository.findById(id);//spring data Bring their own
}
}
6、Maven rely on
<dependency>
<groupId>com.redis.om.spring</groupId>
<artifactId>redis-om-spring</artifactId>
<version>${
version}</version>
</dependency>
Reference documents
The official introduction :https://redis.com/blog/introducing-redis-om-client-libraries/
git Source code :https://github.com/redis/redis-om-spring
summary
Redis OM After the preview version is released , It has attracted extensive attention of programmers , They call it Redis Bounded Hibernate
Some people say ,Springboot Redis Already very concise , This object mapping is also equivalent to a Map, Develop a new framework , It's superfluous .
Others say , Can let redis It is easier to use , More in line with the norms of the language , The so-called beauty of language , An important part of it is its standardization , Is a good progress .
Official explanation ,
at present redis The client does already support Redis All kinds of tools , Including the cache 、 Distributed lock 、 Message queuing, etc
The new framework was created because not everyone has time to repackage these tools . Not everyone has time to Redis Hash maps to Java class ( vice versa ). By building Redis OM, So even if you don't have time to use Redis Provide the core data structure and write your own abstraction , Also available Redis Performance of .
My idea is , Generation of each framework , Are a summary of ideas , It can provide more technical options for our development .
And technological progress , It is precisely because these lovely programmers are constantly expanding and improving .
Because I shared with the company before redis Foundation and advanced , I intend to finish the last section Redis Related distributed locks , But see Redis OM The preview , It makes me full of expectation , So share it .
Let's look forward to the release of the official version , Expect more complete functions !
author :yana
边栏推荐
猜你喜欢
MySQL中的正则表达式
view的绘制机制(一)
【信息检索导论】第七章搜索系统中的评分计算
Basic knowledge of software testing
UEditor . Net version arbitrary file upload vulnerability recurrence
Agile development of software development pattern (scrum)
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
CSRF attack
Sqli - Labs Clearance (less6 - less14)
腾讯机试题
随机推荐
2021-07-19c CAD secondary development creates multiple line segments
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
ORACLE 11G SYSAUX表空间满处理及move和shrink区别
php中计算树状结构数据中的合计
Yolov5 practice: teach object detection by hand
Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
在php的开发环境中如何调取WebService?
Oracle rman半自动恢复脚本-restore阶段
Sqli-labs customs clearance (less18-less20)
中年人的认知科普
SSM二手交易网站
Principle analysis of spark
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
外币记账及重估总账余额表变化(下)
sparksql数据倾斜那些事儿
【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
ORACLE EBS 和 APEX 集成登录及原理分析
IDEA2020中PySpark的两表关联(字段名相同)
Oracle apex Ajax process + dy verification
sqli-labs通關匯總-page2