com.soento.redis.handler.RedisHandler Maven / Gradle / Ivy
package com.soento.redis.handler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* @author yantao.zeng
*/
@Component
public class RedisHandler {
private static RedisTemplate redisTemplate;
@Autowired
public RedisHandler(RedisTemplate redisTemplate) {
RedisHandler.redisTemplate = redisTemplate;
}
public static RedisTemplate template() {
return redisTemplate;
}
public static ValueOperations value() {
return redisTemplate.opsForValue();
}
public static HashOperations hash() {
return redisTemplate.opsForHash();
}
public static void set(Object key, Object value) {
value().set(key, value);
}
public static void set(Object key, Object value, long timeout) {
value().set(key, value, timeout, TimeUnit.SECONDS);
}
public static Object get(Object key) {
return value().get(key);
}
public static void hashSet(Object name, Object key, Object value) {
hash().put(name, key, value);
}
public static Object hashGet(Object name, Object key) {
return hash().get(name, key);
}
public static void delete(Object key) {
template().delete(key);
}
}