Spring Redis - 主条目过期后未删除索引 [英] Spring Redis - Indexes not deleted after main entry expires

查看:27
本文介绍了Spring Redis - 主条目过期后未删除索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Data Repository 保存新条目.每个条目的 TTL 为 10 秒.

I am saving new entries with a Spring Data Repository. I have a TTL of 10 seconds for each entry.

当我用索引保存一个条目时,这是我在 Redis 中得到的

When I save an entry with indexes, here is what i get in Redis

127.0.0.1:6379> keys *
1) "job:campaignId:aa"
2) "job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be"
3) "job:recipient:dd"
4) "job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be:phantom"
5) "job:listId:cc"
6) "job:accountId:bb"
7) "job"
8) "job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be:idx"

过期后我还有数据:

127.0.0.1:6379> keys *
1) "job:campaignId:aa"
2) "job:recipient:dd"
3) "job:listId:cc"
4) "job:accountId:bb"
5) "job"
6) "job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be:idx"

没有任何 TTL.

他们为什么不删除自己?我怎么能这样做?

Why aren't they deleting themself ? How could I do that ?

推荐答案

Spring Data Redis Repositories 使用多个 Redis 功能在 Redis 中持久化域对象.

Spring Data Redis Repositories use multiple Redis features to persist domain objects in Redis.

域对象主要存储在散列中(job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be).任何过期都会直接应用于哈希,因此 Redis 可以使密钥过期.Spring Data Redis 还维护二级索引(job:campaignId:aajob:recipient:dd)以提供按特定字段值的查找.集合中的单个元素不能过期.只有整个数据结构可以过期,但这不是您想要做的事情,因为所有未过期的元素都会以这种方式消失.

Domain objects are stored primarily in a hash (job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be). Any expiry is applied directly to the hash so Redis can expire the key. Spring Data Redis also maintains secondary indexes (job:campaignId:aa, job:recipient:dd) to provide lookup by particular field values. Individual elements inside a set cannot be expired. Only the whole data structure can expire, but that's not the thing you want to do because all non-expired elements would disappear that way.

因此 Spring Data Redis 将原始散列的副本作为幻像散列 (job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be:phantom) 保留,并且 TTL 稍长.

So Spring Data Redis persists a copy of the original hash as phantom hash (job:a6d6e491-5d75-4fd0-bd8e-71692f6d18be:phantom) with a slightly longer TTL.

Spring Data Redis 订阅键事件(通过设置 @EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP) 来监听过期事件.一旦原始哈希过期,Spring Data Redis 就会加载幻像散列以执行清理(从二级索引中删除引用).

Spring Data Redis subscribes to key-events (with setting @EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP) to listen to expiry events. As soon as the original hash expires, Spring Data Redis loads the phantom hash to perform cleanups (remove references from secondary indexes).

未执行数据清理的原因可能有多种:

The reason why the cleanup of your data wasn't performed can have multiple reasons:

  1. 如果您运行控制台应用程序只是为了插入数据并终止,那么到期会删除散列但不会执行索引清理,因为您的应用程序不再运行.Redis 发布的任何事件都是暂时的,如果您的应用程序未在侦听,那么这些事件将丢失
  2. 如果您仅使用 @EnableRedisRepositories 启用了存储库支持(未启用 keyspace-events),则 Keyspace 事件侦听器不会处于活动状态,并且 Spring Data Redis 不会订阅任何到期事件.
  1. If you run a console application just to insert data and terminate, then the expiry removes the hashes but does not perform the index cleanup since your application is not running anymore. Any events published by Redis are transient, and if your application is not listening, then these events are lost
  2. If you have enabled repository support with just @EnableRedisRepositories (without enabling keyspace-events), then the Keyspace event listener is not active, and Spring Data Redis is not subscribed to any expiry events.

这篇关于Spring Redis - 主条目过期后未删除索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆