package me.chyxion.tigon.redis.pub.support;

import me.chyxion.tigon.redis.pub.PubProducer;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Nov 7, 2015 5:07:50 PM
 */
public class PubProducerSupport 
	implements PubProducer {
	@Autowired
	private RedisTemplate<String, Object> redisTpl;
	@Autowired
	private StringRedisTemplate strRedisTpl;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void publish(String topic, Object message) {
		publish(redisTpl, topic, message);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void publish(String topic, String message) {
		publish(strRedisTpl, topic, message);
	}

	// --
	// private methods
	
	private <T> void publish(
		RedisTemplate<String, T> redisTpl, String topic, T message) {
		redisTpl.opsForList().rightPush(topic + PUB_QUEUE_SUFFIX, message);
		strRedisTpl.convertAndSend(MESSAGE_CHANEL, topic);
	}
}
