@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
redisTemplate.opsForHash().put(HASH_KEY, product.getId(), product);
redisTemplate.opsForHash().values(HASH_KEY);
redisTemplate.opsForHash().get(HASH_KEY, id);
redisTemplate.opsForHash().delete(HASH_KEY, id);
Enable caching at controller level
@Cacheable( key = "#{id}", value = "HASH_KEY", unless = "#result.{field}>{value}" )
@CacheEvict(key = "#{id}", value = "HASH_KEY")
@CachePut(key = "#{id}", value = "HASH_KEY")
RedisTemplate for Pub/Sub
@Bean
public RedisTemplate<String,Object> template() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
@Bean
public ChannelTopic topic() {
return new ChannelTopic("pubsub:dev117uday");
}
@Bean
public MessageListenerAdapter messageListenerAdapter() {
return new MessageListenerAdapter(new Receiver());
}
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer( ) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.addMessageListener(messageListenerAdapter(), topic());
return container;
}
Redis Stream Publisher template
Gson gson = new Gson();
var json = gson.toJson(new Users("1", "1", "1"));
redisTemplate.convertAndSend(topic.getTopic(), json );
Redis Stream Subscriber template
public class RedisReceiver implements MessageListener {
Logger logger = LoggerFactory.getLogger(RedisReceiver.class);
@Override
public void onMessage(Message message, byte[] pattern) {
try {
var messageString = getObject(message.getBody());
Gson gson = new Gson();
var user = gson.fromJson((String) messageString, Users.class);
logger.info("Consumed event {}", user.toString());
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
private static Object getObject(byte[] byteArr) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(byteArr);
ObjectInput in = new ObjectInputStream(bis);
return in.readObject();
}
}