如何从Redis Stream中清除记录,仅保留最新的N条记录?

huangapple go评论76阅读模式
英文:

How to clear records from Redis Stream, left only N newest ones?

问题

以下是翻译好的内容:

我有一个SpringBoot应用程序,其中微服务从Redis流发送和接收一些数据。因此,每个服务都发送一个Map记录:

StringRedisTemplate redisTemplate = new StringRedisTemplate();
Map<String, String> recordMap = new HashMap<>();
recordMap.put("topic", "topicName");
recordMap.put("user", "John");
recordMap.put("somethingElse", "someData");
redisTemplate.opsForStream().add(
    StreamRecords.newRecord()
        .in("name_of_stream")
        .ofMap(recordMap))

其他服务有一个:

public class RedisMessagesListener implements StreamListener<String, MapRecord<String, String, String>>

它会在Redis中的每条消息上触发,查找记录中的“topic”值并执行一些操作。

问题是,一旦将记录发送到Redis流,它们就会被Redis永久存储。

我打算在Java中创建一个@Scheduled方法?
我需要删除特定主题的旧记录,只保留最新的N条记录(例如,1000000条)?
我应该如何做到这一点?

英文:

I have SpringBoot app where microservices send and receive some data from redis stream. So, every service send record which is Map:

StringRedisTemplate redisTemplate = new StringRedisTemplate();
Map&lt;String, String&gt; recordMap = new HashMap&lt;&gt;();
recordMap.put(&quot;topic&quot;, &quot;topicName&quot;);
recordMap.put(&quot;user&quot;, &quot;John&quot;);
recordMap.put(&quot;somethingElse&quot;, &quot;someData&quot;);
redisTemplate.opsForStream().add(
            StreamRecords.newRecord()
                .in(&quot;name_of_stream&quot;)
                .ofMap(recordMap)))

Other services have

public class RedisMessagesListener implements StreamListener&lt;String, MapRecord&lt;String, String, String&gt;&gt;

which is triggered on each message in Redis, looks for "topic" value in record and do some staff.

The problem is that records, once sent to Redis Stream, are always stored by Redis.

I'm about to create @Scheduled method in Java ?<br>
I need to delete old records of certain topics, left only N newest records (e.g., 1 000 000) ?. <br>How can I do that?

答案1

得分: 1

Redis的XTRIM命令正是为这种类型的定时任务而设计的。[https://stackoverflow.com/questions/59404054/how-to-xtrim-trim-redis-stream-in-the-safe-way] 该线程已经讨论了一些解决方案。

英文:

Redis XTRIM command is exactly meant for this sort of schedule jobs. [https://stackoverflow.com/questions/59404054/how-to-xtrim-trim-redis-stream-in-the-safe-way] thread already have some solutions discussed

huangapple
  • 本文由 发表于 2020年4月10日 21:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定