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

Entity cache in Redis with TTL dosent work in spring boot

$
0
0

I want to cache a UserInfo entity in redis for just 3600 seconds and accord to my searches and examples that I saw, I implemented Redis caching in spring like below code and i have an issue with cache and uncache (with define ttl) when I cache a UserInfo entity in redis.

The whole code is here:

UserInfo entity:

@RedisHash(value = "UserInfo", timeToLive = 3600)public class UserInfo implements Serializable{    private static final long serialVersionUID = 2048972398004272098L;    @Id    private int id = ThreadLocalRandom.current().nextInt(100000);    private Long expiration;    private String userName;    private String name;    private String familyName;    private String givenName;    private String language;    private Set<String> roles;    private Set<String> permissions;    private String authorizationToken;    private String action;    private String path;    private String remoteAddr;    //getter & setter}

This is my RedisConfiguration:

@EnableCaching@EnableScheduling@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP, keyspaceConfiguration = RedisKeyspaceConfiguration.class)@Configurationpublic class RedisConnectionConfig{    private static final Logger logger = LogManager.getLogger(RedisConnectionConfig.class);    @Value("${spring.redis.host}")    private String host;    @Value("${spring.redis.port}")    private Integer port;    @Value("${spring.redis.password}")    private String redisPass;    @Bean    JedisConnectionFactory jedisConnectionFactory()    {        logger.info("================================================================");        logger.info("Redis config : {} : {} ", host, port);        logger.info("================================================================");        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(host, port);                     redisConfig.setPassword(redisPass);                    return new JedisConnectionFactory(redisConfig);    }    @Bean    public RedisTemplate<String, Object> template() {        RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(jedisConnectionFactory());        template.setKeySerializer(new StringRedisSerializer());        template.setHashKeySerializer(new StringRedisSerializer());        template.setHashKeySerializer(new JdkSerializationRedisSerializer());        template.setValueSerializer(new JdkSerializationRedisSerializer());        template.setEnableTransactionSupport(true);        template.afterPropertiesSet();        return template;    }    @Bean    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()                .entryTtl(Duration.ofSeconds(3600));        return RedisCacheManager                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))                .cacheDefaults(redisCacheConfiguration).build();    }    }

And this is RedisKeyspaceConfiguration class that I define in

@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP, keyspaceConfiguration = RedisKeyspaceConfiguration.class)

annotation on redis config class :

public class RedisKeyspaceConfiguration extends KeyspaceConfiguration {@Overridepublic boolean hasSettingsFor(Class<?> type) {    return true;}@Overridepublic KeyspaceConfiguration.KeyspaceSettings getKeyspaceSettings(Class<?> type) {    KeyspaceSettings keyspaceSettings = new KeyspaceSettings(UserInfo.class, "UserInfo");    keyspaceSettings.setTimeToLive(3600L);    return keyspaceSettings;}}

And RedisRepository :

    public interface UserAuthInfoRepository extends CrudRepository<UserInfo, Integer>{    UserInfo findById(int id);    UserInfo findByUserName(String userName);}

And finally this is when I cache it into redis:

  public UserInfo cacheUserAuthDataToRedis(UserInfo userInfo) throws AdminUserAuthorizationException{    UserInfo result = null;    result = userAuthInfoRepository.save(userInfo);    return result;    }

And also application.properties file:

#Redis Configspring.redis.host=localhostspring.redis.port=6379spring.redis.password=spring.cache.redis.time-to-live=360000

Now when I call cacheUserAuthDataToRedis method as you can see in screenshot 1, data stored in redis and they has a TTL time into TTL box and they will be delete from redis after ttl time runs out and everything is ok :

enter image description here

But as you can see in picture2 there is a another row name UserInfo and the item that there is here they do not disappear because the TTL of them is -1 and they are will stored forever:

enter image description here

Please

  1. Tell me if you know a better solution for implement this purpose
  2. Help me resolve the issue in screenshot #2

I appreciate your help


Viewing all articles
Browse latest Browse all 2204

Trending Articles



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