Spring Data Redis删除条目(哈希),但留下索引

问题描述 投票:0回答:1

在Redis中使用Spring Data时遇到的情况是它在#findAll调用时提供了空对象。

当在实体上调用#deleteAll时,有时:idx键不会被删除。

对我来说,看起来Spring Data正在使用#findAll的二级索引而无法获取对象,因为它已经在Redis中删除了。

我们已经发现@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)可以提供帮助,但我想问你是否可以解释发生了什么并提供任何其他解决方案,因为我对Redis不太熟悉。

在Redis中直接删除时,情况也是如此。看起来像这样:

127.0.0.1:6379> keys *
 1) "entry:key:testKey3"
 2) "entry:1234_testKey1"
 3) "entry:eid:1234"
 4) "entry"
 5) "entry:1234_testKey3"
 6) "entry:1234_testKey1:idx"
 7) "entry:key:testKey2"
 8) "entry:1234_testKey2:idx"
 9) "entry:1234_testKey2"
10) "entry:key:testKey1"
11) "entry:1234_testKey3:idx"
127.0.0.1:6379> del entry:1234_testKey1
(integer) 1

并且,正如您所见,entry:1234_testKey1:idx索引尚未删除。

 1) "entry:key:testKey3"
 2) "entry:eid:1234"
 3) "entry"
 4) "entry:1234_testKey3"
 5) "entry:1234_testKey1:idx"
 6) "entry:key:testKey2"
 7) "entry:1234_testKey2:idx"
 8) "entry:1234_testKey2"
 9) "entry:key:testKey1"
10) "entry:1234_testKey3:idx"

也许在redis中有某种设置我可以在删除条目时设置清理索引?我找不到任何相关信息。

实体类看起来像这样:

@RedisHash("entry")
public class Entry {

    @Id
    private String id;
    @Indexed
    private String key;
    @Indexed
    private String eid;

我们只是在#save上使用#deleteAll#deleteByIdCrudRepository

redis spring-data spring-data-redis
1个回答
0
投票

我已经测试了我的cli,请看下面的结果:

127.0.0.1:6379> SET test1 redis
OK
127.0.0.1:6379> SET test2 redis
OK
127.0.0.1:6379> keys *
1) "test2"
2) "test1"
3) "PawanTest"
4) "test"
127.0.0.1:6379> DEL test1
(integer) 1
127.0.0.1:6379> keys *
1) "test2"
2) "PawanTest"
3) "test"
127.0.0.1:6379>

如您所见,我已经向redis添加了一些数据,然后使用DEL命令删除了一个密钥,它已被删除。

PS:我的系统上没有为redis做过任何配置。刚刚使用sudo apt-get install redis-server安装,它工作正常。

注意:如果它不起作用,那么您可以尝试使用FLUSHALL清理所有数据集,然后再尝试一些数据。

© www.soinside.com 2019 - 2024. All rights reserved.