Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2222

Redis Server is not getting Refreshed

$
0
0

I'm using redis in my springboot redis 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 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. I wanted my redis cache to refresh after a given time set by me. Any advice on this? Here is my code below:

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);}
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);    }}
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;    @Cacheable    public FlightModel getFlightByFlightName(String flightName)    {
package com.dg.model;import java.io.Serializable;import java.util.List;public class FlightModel implements Serializable{    private List<Object> listofflightname;    public List<Object> getListofflightname() {        return listofflightname;    }    public void setListofflightname(List<Object> listofflightname) {        this.listofflightname = listofflightname;    }}
package com.dg.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class FlightEntity {    @Id    @Column(name="txtBookingCode")    private String bookingCode;    @Column(name="txtFlightName")    private String flightname;    public String getBookingCode() {        return bookingCode;    }    public void setBookingCode(String bookingCode) {        this.bookingCode = bookingCode;    }    public String getFlightname() {        return flightname;    }    public void setFlightname(String flightname) {        this.flightname = flightname;    }}
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;    }}
package com.dg;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublic class RedisTestApplication {    public static void main(String[] args) {        SpringApplication.run(RedisTestApplication.class, args);    }}

Viewing all articles
Browse latest Browse all 2222

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>