Redis (Remote Dictionary Server) sepatutnya dianggap sebagai orang tua di dunia solusi NoSql. Posting ini adalah tentang cara kerja Spring Data dengannya. Ide penulisan posting ini muncul karena Redis tidak begitu mirip dengan database biasa, ia mendukung tipe data yang tidak nyaman digunakan untuk menyimpan objek (cache tidak dihitung) dan untuk mencari field tertentu. Di sini, dengan contoh, saya akan mencoba menjelaskan bagaimana Spring Data bekerja dengannya melalui CrudRepository dan QueryDSL yang sudah dikenal. Ini bukan contoh HowTo yang banyak. Siapa yang tertarik dengan bagian dalamnya melangkah lebih jauh.
Contohnya akan didasarkan pada proyek sederhana . Redis naik di kontainer buruh pelabuhan, aplikasi boot pegas yang juga berkomunikasi dengannya di dalam kontainer. Aplikasi ini berisi model, repositori, layanan, dan pengontrol sederhana. Anda dapat menyentuh semua ini melalui kesombongan di localhost: 8080.
Selain perintah yang dijalankan oleh layanan ke database, saya juga akan memberikan pseudo-code kecil yang menjelaskan dengan lebih jelas apa yang terjadi.
Kami akan bekerja dengan entitas Mahasiswa:
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Student")
public class Student {
@Id
private String id;
private String name;
private int age;
}Di sini, perlu diklarifikasi bahwa anotasi @RedisHash("Student")mengatakan di mana kunci semua entitas akan digabungkan.
Mari kita coba pertahankan siswa pertama:
curl -X POST "http://localhost:8080/save" -H "accept: */*" -H "Content-Type: application/json" -d "{\"id\":\"1\",\"name\":\"Stephen\",\"age\":12}"3 perintah dieksekusi:
"DEL" "Student:1"
"HMSET" "Student:1" "_class" "com.odis.redisserviceweb.model.Student" "id" "1" "name" "Stephen" "age" "12"
"SADD" "Student" "1" , - "DEL" "Student:1", "Student:1". @RedisHash + @Id.
"HMSET" "Student:1" "_class" "com.odis.redisserviceweb.model.Student" "id" "1" "name" "Stephen" "age" "12". "Student:1". -
Map "Student:1";
"Student:1".put("_class", "com.odis.redisserviceweb.model.Student");
"Student:1".put("id", "1");
"Student:1".put("name", "Stephen");
"Student:1".put("age", "12"); - "SADD" "Student" "1" - "Student" "1".
? Redis. - "Student:1", - "Student".
keys * - ( ) :
127.0.0.1:6379> keys *
1) "Student"
2) "Student:1", :
127.0.0.1:6379> type "Student"
set
127.0.0.1:6379> type "Student:1"
hash- ? .
@Id :
curl -X GET "http://localhost:8080/get/1" -H "accept: */*""Student:1" , :
"HGETALL" "Student:1"
1) "_class"
2) "com.odis.redisserviceweb.model.Student"
3) "id"
4) "1"
5) "name"
6) "Stephen"
7) "age"
8) "12", , :
curl -X POST "http://localhost:8080/save" -H "accept: */*" -H "Content-Type: application/json" -d "{\"id\":\"2\",\"name\":\"Macaulay\",\"age\":40}"
curl -X GET "http://localhost:8080/get" -H "accept: */*"3 :
"SMEMBERS" "Student"
1) "1"
2) "2"
"HGETALL" "Student:1"
1) "_class"
2) "com.odis.redisserviceweb.model.Student"
3) "id"
4) "1"
5) "name"
6) "Stephen"
7) "age"
8) "12"
127.0.0.1
"HGETALL" "Student:2"
1) "_class"
2) "com.odis.redisserviceweb.model.Student"
3) "id"
4) "2"
5) "name"
6) "Macaulay"
7) "age"
8) "40" . - "Student" - , "Student:@Id". , O (N) N - .
:
curl -X DELETE "http://localhost:8080/delete/1" -H "accept: */*":
"HGETALL" "Student:1"
1) "_class"
2) "com.odis.redisserviceweb.model.Student"
3) "id"
4) "1"
5) "name"
6) "Stephen"
7) "age"
8) "12"
"DEL" "Student:1"
(integer) 1
"SREM" "Student" "1"
(integer) 1
"SMEMBERS" "Student:1:idx"
(empty array)
"DEL" "Student:1:idx"
(integer) 0, Id . "Student" "1".
Student:1:idx. . , . :
List<Student> findAllByName(String name);:
curl -X POST "http://localhost:8080/save" -H "accept: */*" -H "Content-Type: application/json" -d "{\"id\":\"1\",\"name\":\"Stephen\",\"age\":12}"
curl -X GET "http://localhost:8080/get/filter/Stephen" -H "accept: */*", Redis :
"SINTER" "Student:name:Stephen"
(empty array) "SINTER" - , , - "Student:name:Stephen" .
, , @Id, @Indexed Spring Data , . . Redis . name :
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Student")
public class Student {
@Id
private String id;
@Indexed
private String name;
private int age;
}:
curl -X POST "http://localhost:8080/save" -H "accept: */*" -H "Content-Type: application/json" -d "{\"id\":\"1\",\"name\":\"Stephen\",\"age\":12}":
"DEL" "Student:1"
"HMSET" "Student:1" "_class" "com.odis.redisserviceweb.model.Student" "id" "1" "name" "Stephen" "age" "12"
"SADD" "Student" "1"
"SADD" "Student:name:Stephen" "1"
"SADD" "Student:1:idx" "Student:name:Stephen" , : "Student:name:Stephen" , , @Indexed . Id . Id Stephen Id . , . - :
Map "Student:1";
"Student:1".put("_class", "com.odis.redisserviceweb.model.Student");
"Student:1".put("id", "1");
"Student:1".put("name", "Stephen");
"Student:1".put("age", "12");
Set "Student";
"Student".add("1");
Set "Student:name:Stephen";
"Student:name:Stephen".add("1");
Set "Student:1:idx";
"Student:1:idx".add("Student:name:Stephen");, , Redis :
"SINTER" "Student:name:Stephen"
"HGETALL" "Student:1" Id , . .
SINTER . id .
.
, Spring Data Redis. Spring.
Kerugiannya adalah field yang akan dicari harus ditandai @Indexeddengan anotasi sejak awal. Jika tidak, "indeks" hanya akan dibuat untuk fitur yang dipertahankan setelah menambahkan anotasi ini. Dan ya, saya memahami bahwa Redis bukanlah solusi terbaik untuk kebutuhan seperti itu, tetapi jika karena situasi tertentu harus menggunakannya, maka SpringData akan dapat melakukannya dengan cukup baik.