在NoSql解决方案领域,Redis(远程字典服务器)应该被视为老人。这篇文章是关于Spring Data如何使用它的。之所以写这篇文章是因为Redis与通常的数据库不太相似,它支持不方便用于存储对象(缓存不计数)和搜索某些字段的数据类型。在这里,通过示例,我将尝试通过熟悉的CrudRepository和QueryDSL来描述Spring Data如何使用它。这不是HowTo的示例,它有很多。谁对内部感兴趣的人走得更远。
这些示例将基于一个简单的项目。Redis出现在docker容器中,该容器是一个弹簧启动应用程序,也可以在容器中与其通信。该应用程序包含一个简单的模型,存储库,服务和控制器。您可以通过在localhost:8080上大摇大摆来触摸所有这些内容。
除了服务执行到数据库的命令之外,我还将提供一个小的伪代码,可以更清楚地描述正在发生的事情。
我们将与学生实体合作:
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Student")
public class Student {
@Id
private String id;
private String name;
private int age;
}
在这里,有必要澄清一下,注释@RedisHash("Student")
说明所有实体都将在该密钥下聚合。
让我们保持第一个学生:
curl -X POST "http://localhost:8080/save" -H "accept: */*" -H "Content-Type: application/json" -d "{\"id\":\"1\",\"name\":\"Stephen\",\"age\":12}"
执行了3条命令:
"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.
缺点是要搜索的字段@Indexed
从一开始就必须带有注释。否则,将仅为添加此注释后保留的要素创建“索引”。是的,我知道Redis并不是满足此类需求的最佳解决方案,但是如果由于某种情况而有必要使用它,那么SpringData将能够很好地做到这一点。