Last active
June 24, 2018 07:51
-
-
Save tsungtwu/f0206aa2ff7e2bdbd48e9771c0ac290f to your computer and use it in GitHub Desktop.
Redis DAO for spring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
@Transactional | |
public class TopicRedisDAO { | |
@Autowired | |
RedisTemplate < String, Object > redisTemplate; | |
ValueOperations < String, Object > valueOperations = null; | |
TimeUnit TOPIC_EXPIRED_TIME_UNIT = TimeUnit.HOURS; | |
int TOPIC_TIME_OUT = 3; // 3 hours | |
public TopicRedisDAO() {} | |
public TopicRedisDAO(RedisTemplate < String, Object > redisTemplate) { | |
this.redisTemplate = redisTemplate; | |
} | |
public boolean setTopic(String topic_key, String topic_value) { | |
valueOperations = redisTemplate.opsForValue(); | |
/** if topic_key doesn't exist in redis, add to redis. */ | |
boolean hasKey = redisTemplate.hasKey(topic_key); | |
if (!hasKey) | |
valueOperations.set(topic_key, topic_value, TOPIC_TIME_OUT, TOPIC_EXPIRED_TIME_UNIT); | |
return true; | |
} | |
public String getTopic(String topic_key) { | |
String topic_info = null; | |
valueOperations = redisTemplate.opsForValue(); | |
boolean hasKey = redisTemplate.hasKey(topic_key); | |
if (hasKey) { | |
topic_info = (String) valueOperations.get(topic_key); | |
} | |
return topic_info; | |
} | |
public RedisTemplate < String, Object > getRedisTemplate() { | |
return redisTemplate; | |
} | |
public void setRedisTemplate(RedisTemplate < String, Object > redisTemplate) { | |
this.redisTemplate = redisTemplate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment