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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With