Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using Redis in my Spring Boot REST application to store cache. But the problem I'm facing is once it is stored in Redis my API only hits the Redis, not the database. I've added a time out property, but it didn't work. I've tried CacheManager to get the cache and call CacheEvict to clear the cache and then CachePut to put the data again, but it didn't work.

These are the things I've tried so far. I wanted my Redis cache to refresh after a given time set by me. Any advice on this? Here is my code below:

Java
package com.dg.repo;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.dg.entity.FlightEntity;

public interface FlightRepo extends JpaRepository<FlightEntity, String> {
    
    @Query(value="select distinct txtFlightName\r\n"
            + "from {test-schema}flights", nativeQuery = true)
    List<String> getAllFlights();
    
    @Query(value="select distinct txtFlightName from {test-schema}flights \r\n"
            + "where txtFlightName LIKE %:flightname%",nativeQuery = true)
    List<String> getListofFlights(@Param("flightname")String flightname);
    
    @Query(value="select distinct txtBookingCode,txtFlightName from {test-schema}flights \r\n"
            + "where txtFlightName LIKE %:flightname%",nativeQuery = true)
    List<FlightEntity> getFlightEntity(@Param("flightname")String flightname);

}

Java
package com.dg.model;

import java.io.Serializable;
import java.util.List;

public class FlightModel implements Serializable{

Java
package com.dg.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableRedisRepositories
@Profile("test")
public class RedisConfig {
    
    @Value("${spring.redis.cluster.nodes}")
    private String nodesProperty;
    
    @Bean
    public JedisConnectionFactory jedisConnectionFactory()
    {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMinIdle(2);
        poolConfig.setMaxIdle(5);
        poolConfig.setMaxTotal(20);
        poolConfig.setEvictorShutdownTimeoutMillis(10000);
        
        String [] nodesArray=nodesProperty.split(",");
        List<String> nodes = new ArrayList<String>(Arrays.asList(nodesArray));
        
        RedisClusterConfiguration configuration=new RedisClusterConfiguration(nodes);
        configuration.setMaxRedirects(100);
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
        connectionFactory.setPoolConfig(poolConfig);
        return connectionFactory;
    }
    
    @Bean
    public RedisTemplate redisTemplate()
    {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

}

Java
package com.dg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class RedisTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisTestApplication.class, args);
    }

}

Java
package com.dg.service;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import com.dg.model.FlightModel;
import com.dg.repo.FlightRepo;

public class FlightService {
    
    @Autowired
    FlightRepo flightRepo;
    
    @Autowired
    ModelMapper modelMapper;
    
    @Scheduled(fixedRate = 50000)
    @Caching(evict = {@CacheEvict(value="getFlightList", key="#flightname")})
    public FlightModel getFlightByFlightName(String flightName)
    {

Java
package com.dg.repo;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;

import com.dg.entity.FlightEntity;

public abstract class FlightRepoImpl implements FlightRepo {
    
    RedisTemplate template;
    
    HashOperations hashOperations;

    public FlightRepoImpl(RedisTemplate template, HashOperations hashOperations) {
        super();
        this.template = template;
        this.hashOperations = template.opsForHash();
    }

    @Override
    public List<String> getAllFlights() {
        return hashOperations.values("FlightModel");
    }
    
    @Override
    public List<String> getListofFlights(String flightname) {
        return (List<String>) hashOperations.get("FlightModel", flightname);
    }

}


What I have tried:

I've added time out property it didn't work. I've tried CacheManager to get the cache and call CacheEvict to clear the cache and then CachePut to put the data again, but it didn't work. These are the things I've tried so far.
Posted
Updated 21-Jan-22 4:33am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900