Vivek
Vivek

Reputation: 13298

How to programmatically control the persistence in redis

I'm using redis with my spring boot application using jedis, spring-data-redis. I want to control the persistence mechanism of redis in the following way.

I've read the redis data persistence policies and it seems that it allows storing the data on some time intervals, but is there any way I can control the persistence only programmatically & not use the time interval based persistence mechanism

Upvotes: 0

Views: 1336

Answers (1)

Vivek
Vivek

Reputation: 13298

I found solution to this issue. So the changes I made to control the persistence only programmatically are as below

  • Comment all the save lines from redis configuration file. This will stop redis's default time/operations based persistence mechanism.

  • Then call save() or bgsave() whenever you want to persist the data on disc.

So using the RedisTemplate you can call

redisTemplate.getConnectionFactory().getConnection().save();

and this will persist the changes on disc.

For my requirement, I'm passing a shouldPersist flag to my repository methods to decide whether to persist the data or not. If the data is not persisted using save or bgsave it will be discarded on server restart. Which is what I want for some of my scenarios.

Upvotes: 1

Related Questions