英文:
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<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)))
Other services have
public class RedisMessagesListener implements StreamListener<String, MapRecord<String, String, String>>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论