All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.cache.redis.CacheClearingComponent Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.cache.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.infusers.core.logger.ILogger;

@Component
@ConditionalOnProperty(name = "redis.hostname")
public class CacheClearingComponent {

	private ILogger log = new ILogger(CacheClearingComponent.class);    

    private final ApplicationContext applicationContext;

    @Autowired
    public CacheClearingComponent(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    
    public void clearCache() {
        String[] redisTemplateNames = applicationContext.getBeanNamesForType(RedisTemplate.class);

        for (String redisTemplateName : redisTemplateNames) {
            RedisTemplate redisTemplate = applicationContext.getBean(redisTemplateName, RedisTemplate.class);
            redisTemplate.getConnectionFactory().getConnection().flushDb();
            // Or delete specific keys if needed
            // redisTemplate.delete("key1", "key2", "key3");
        }
        // redisTemplate.delete("key1", "key2", "key3");
        
		log.warnWithSeparator("CacheClearingComponent.clearCache() --> Redis cache cleared on Startup/Shutdown/On New user created!!");    	
    }
        
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy