0

I`m using RedisTemplate provided by spring framework for In-Memory caching. And MySql as primary database. I need to update the cache whenever new row is added or updated in primary database. How can i accomplish this using java? Is there any inbuilt feature provided by Redis server to achieve this?

Update:

@Override public void getEmployeeDetailsForRedisTemplate(List<Employee> employee) { 
  logger.info("Saving " + employee.size() + " record to redis template"); 
  for (Employee emp : employee) { 
    listOperations.leftPush(EnumConstants.EMPLOYEE_KEY.getValue(), emp); 
  } 
} 

I have been polling database in regular interval and based on status column in DB i was pushing updated data to redis server. Which is not a efficient way of doing it

KeyMaker00
  • 6,194
  • 2
  • 50
  • 49
  • What have you tried so far? Can you post some code snippet that you have tried? even if it wasn't a successful one. – KeyMaker00 Aug 08 '18 at 06:36
  • `@Override public void getEmployeeDetailsForRedisTemplate(List employee) { logger.info("Saving " + employee.size() + " record to redis template"); for (Employee emp : employee) { listOperations.leftPush(EnumConstants.EMPLOYEE_KEY.getValue(), emp); } }` I have been polling database in regular interval and based on status column in DB i was pushing updated data to redis server. Which is not a efficient way of doing it – Nagabhushan NC Aug 08 '18 at 06:58
  • I have put your code inside the question for a better understanding. – KeyMaker00 Aug 08 '18 at 07:26

1 Answers1

0

Sorry I wanted to ask whether there is a code snippet of the Java code that is invoked when a new row is added/updated but I am new so I can't comment

If you are using Redis as a cache then you might want to use @Cacheable annotation provided by Spring-Data-Redis by setting up RedisCacheManager in your configuration

@Cacheable - Annotation indicating that the result of invoking a method (or all methods in a class) can be cached. Each time an advised method is invoked, caching behavior will be applied, checking whether the method has been already invoked for the given arguments. A sensible default simply uses the method parameters to compute the key, but a SpEL expression can be provided via the key() attribute, or a custom KeyGenerator implementation can replace the default one (see keyGenerator()).

If no value is found in the cache for the computed key, the target method will be invoked and the returned value stored in the associated cache. Note that Java8's Optional return types are automatically handled and its content is stored in the cache if present.

So you could actually use it like

@Override
@Cacheable(value = "cacheName", key = "#employee.id")
public void getEmployeeDetailsForRedisTemplate(List<Employee> employee) { 
  logger.info("Saving " + employee.size() + " record to redis template"); 
  for (Employee emp : employee) { 
    listOperations.leftPush(EnumConstants.EMPLOYEE_KEY.getValue(), emp); 
  } 
} 

Hope this helps

JWiryo
  • 371
  • 4
  • 11