Object Mapping และเพิ่มเติม สำหรับ Redis และ Spring
Redis OM Spring เป็น client library ใหม่ที่ออกแบบมาเพื่อช่วยคุณสร้างโมเดลโดเมนและ persist ข้อมูลไป Redis ใน Spring applications ของคุณ Library นี้ทำงานกับ open source Redis แต่ให้ความสามารถ indexing และ querying เพิ่มเติมมากเมื่อใช้กับ RedisJSON
Redis ร่วมกับโมดูลของมัน ทำให้เป็นหนึ่งใน data platforms ที่เร็วที่สุดบนดาวเคราะห์ แต่มันไม่ได้ง่ายเสมอไปที่จะใช้ประโยชน์จากความเร็วของมัน Redis OM Spring ให้ developer experience ที่น่าสนใจ และในโพสต์นี้ฉันจะแสดงให้คุณเห็นว่าคุณสร้างอะไรได้ด้วยมัน
แรงจูงใจ
มันหายากที่จะเป็นนักพัฒนาและพบเทคโนโลยีที่ทรงพลังและเรียบง่ายพร้อมกัน ด้วย OSS Redis เพียงอย่างเดียว คุณมีโครงสร้างข้อมูลและคำสั่งที่เกี่ยวข้องเยอะมาก สำหรับทำ data-oriented และ messaging tasks ที่ enterprise applications ต้องการ
เช่นเดียวกัน ในโลก enterprise Java development Spring ได้ง้าวดังความซับซ้อนของการสร้างแอปพลิเคชันขนาดใหญ่ feature-rich ตลอด 18 ปีที่ผ่านมา ในพื้นที่นี้ Redis ได้รับการสนับสนุนโดย Spring Data Redis ที่เก๋ๆ ซึ่งให้ low-level และ high-level abstractions สำหรับ interact กับ OSS Redis
หนึ่งในความท้าทายในการใช้ Spring Data Redis คือมันไม่รองรับ Redis modules ที่นี่ Redis เราพัฒนา game-changing Redis modules หลายตัวที่ समृद्ध Redis core data structures ด้วย search, JSON indexing และ querying, graph data, time series data, และ framework สมบูรณ์สำหรับ server-side computation (Redis Gears) เราสร้าง Redis OM Spring เพื่อใช้ประโยชน์จากโมดูลเหล่านี้และให้ชุดความสามารถที่กว้างขึ้น
Redis OM Spring
Redis OM family ของ client libraries มุ่งให้ high-level abstractions ที่ implemented อย่าง idiomatic สำหรับ language/platform ที่คุณเลือก เรารองรับ Node.js, Python, .NET, และ Spring communities ปัจจุบัน Redis OM Spring ให้นักพัฒนาเพิ่มพลังของ Redis เข้า Spring Boot applications ได้อย่างง่ายดาย
โดยเฉพาะ Redis OM Spring ให้ robust repository และ custom object-mapping abstractions สร้างบน Spring Data Redis (SDR) framework ที่เก๋ๆ
Preview release ปัจจุบันให้ความสามารถทั้งหมดของ Spring Data Redis เพิ่ม:
- Java to JSON mapping
- Enhanced Hash mappings
- Automatic search index generation
- Spring Data Repositories backed by RediSearch
Java to JSON Mapping
เพื่อ map Java objects ไป JSON documents จัดเก็บใน Redis ใช้ RedisJSON คุณสามารถใช้ annotation
@Documentเหมือน annotation entity mapping ของ Spring Data อื่นๆ คุณเพิ่มมันที่ class declaration ตัวอย่าง สมมติคุณอยาก map objects ของ type
Companyเพียงเพิ่ม
@Documentตามด้านล่าง:
คุณอาจสังเกตว่า body ของคลาสนี้มี annotations หลายตัว
@Idมาจาก Spring Data และมันประกาศ property
idเป็น Redis key ที่จะจัดเก็บ JSON document ของเรา
Arguably ฟีเจอร์ยอดนิยมที่สุดของ Spring Data family คือ declarative Data Repositories สอง annotations ถัดไปให้คุณใช้ RediSearch จัดดัชนี JSON documents นี้ exposed ผ่าน interface
RedisDocumentRepository:
Empty repository declaration คือทั้งหมดที่คุณต้องการสำหรับ basic CRUD functionality/pagination และ sorting สำหรับ POJOs ของคุณ
Redis OM Spring ใช้ POJO fields ที่ annotated ด้วย
@Indexedหรือ
@Searchableเพื่อสร้าง index schema ในกรณีของ
CompanyPOJO เรามี property name annotated ว่า “searchable” หมายความว่าเราได้ full-text search capabilities บน field นั้น สะท้อนใน schema field definition
$.name AS name TEXT
อีกด้านหนึ่ง field tags annotated ว่า “indexable” หมายความว่าเราได้ index field type TAG หมายความว่าเราสามารถค้นหา Companies ด้วยค่าที่แน่นอนของ field ได้ อีกครั้ง สะท้อนใน schema field definition:
$.tags[*] AS tags TAG
สร้าง Simple Dynamic Queries
คุณสมบัติที่น่าสนใจที่สุดของ Redis OM Spring คือความสามารถในการสร้าง repository implementations อัตโนมัติใน runtime จาก repository interface ด้วย fields ที่ถูก index อย่างเหมาะสม คุณตอนนี้สามารถทำให้ queries ทั้งหมดด้านล่างทำงานได้โดยไม่ต้องเขียนโค้ดเพิ่มเติม:
Repository proxy มี 2 วิธี derive store-specific query จาก method name:
- โดย derive query จาก method name โดยตรง
- โดยใช้ manually defined query ด้วย annotations
@Query
หรือ@Aggregation
มาตรวจสอบ method declarations บางตัวที่เพิ่มเข้าไปใน repository interface
findOneByName
Redis OM Spring ใช้ method name และ parameters สร้าง query ที่เหมาะสม มันใช้ method return type กำหนดวิธี package และ return result
findOneByNameคืนค่า
Optional of Company
findOneบ่งชี้ว่าเพียงหนึ่งผลลัพธ์จะคืนค่า แม้จะมีหลายผลลัพธ์ Library parse method name เพื่อกำหนดจำนวนพารามิเตอร์ที่คาดหวัง ตัวอย่าง
ByNameportion ของ method บอกเราว่าคาดหวัง single parameter ชื่อ
name
findByLocationNear
Redis OM Spring รองรับ GeoJSON types จัดเก็บ geospatial data โดยใช้ keyword
nearใน queries เรากำลังบอกโค้ดให้คาดหวัง
Point
(org.springframework.data.geo.Point)และ
Distance (org.springframework.data.geo.Distance)type เป็น parameters
การใช้ Repository
เหมือน Spring Data Repositories อื่นๆ คุณสามารถ inject repository เข้า Spring component อื่น:
สรุป
นี่คือ preview release ของ Redis OM Spring ยังมีอะไรมากที่จะสร้าง แต่ตอนนี้เราต้องการความช่วยเหลือจากคุณ! คุณสามารถดู README สำหรับรายละเอียดติดตั้งโปรเจกต์ใน Spring application ของคุณ เรายินดีต้อนรับ feedback, PRs, และ issues!