Redis
working with Redis and Spring Boot
Redis with Spring Boot
@Configuration
@EnableRedisRepositories
public class RedisConfig {
// configuration for connection
@Bean
public JedisConnectionFactory connectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
// load from properties
configuration.setHostName("localhost");
configuration.setPort(6379);
return new JedisConnectionFactory(configuration);
}
// configuration for using redis with java
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
}Making class compatible with redis
Annotate class with :
@RedisHash("Product")Implement with
Serializableinterface
Using Redis as DocStore
As
docstorebecause it gives the ability with to search and operate on valuesTo add obj to Redis with id as identifier
To get all values of obj
To get document by id
To delete obj with id
Enable caching at controller level
Annotate with
@EnableCachingTo enable caching at method level
To remove obj from cache when delete from db
To update obj from cache when delete from db
RedisTemplate for Pub/Sub
Redis Stream Publisher template
Redis Stream Subscriber template
Last updated
Was this helpful?