本文介绍了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"

到期后,我仍然有数据:

After the expiration, I still have data :

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存储库使用多个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-主条目到期后未删除索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:25