Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis MSET with TTL in Spring RedisTemplate

I have got a Spring Boot application that needs to take millions of key value pairs and insert them into Redis.

Currently I'm using the multiSet method 1,000 key value pairs at a time.

@Autowired
private final StringRedisTemplate template;

...

Map<String, String> keyValuePairs = new HashMap<>();
template.opsForValue().multiSet(keyValuePairs);

However we also need to set a TTL for each pair. There doesn't seem to be a way to do this with multiSet. There is a way with set but this would have to be called millions of times so may not be efficient.

// For each key value pair
template.opsForValue().set(key, value, timeout, unit);

Does anyone know a way of doing this or using the set in a performant way?

Thanks

like image 904
ptimson Avatar asked Aug 18 '16 21:08

ptimson


People also ask

What is TTL in Redis cache?

Redis TTL command is used to get the remaining time of key expiry in seconds. Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset. Syntax: TTL KEY_NAME.

How does spring boot integrate with Redis cache?

1) Start Redis Server. 2) Start your Spring Boot Application. 3) Make a Rest Call using any REST client on operations which are get, update & delete. You can use any REST client such as any User Interface, RestTemplate or Postman etc.

Is RedisTemplate thread safe?

Once configured, the template is thread-safe and can be reused across multiple instances. Out of the box, RedisTemplate uses a Java-based serializer for most of its operations.

How do I store items in Redis cache spring boot?

In spring boot you need to set spring. cache. type=redis and this would create your cache using Redis. @Autowired RedisTemplate<String, String> redisTemplate; redisTemplate.


1 Answers

Pipelining should solve your problem; you create a pipeline, fill it with all the commands you want to perform, and then send those in bulk to redis. Using SET with EX you should be able to send 10,000 or more at a time.

like image 51
Chris Tanner Avatar answered Sep 21 '22 12:09

Chris Tanner