Redisオブジェクトをバイナリ形式で格納
902 ワード
, Serializable
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtils {
@Resource// @Autowired spring
private RedisTemplate redisTemplate;
public void setObject(String key, Object value) {
setObject(key, value, null);
}
public void setObject(String key, Object value, Long timeOut) {
redisTemplate.opsForValue().set(key, value);
if (null != timeOut) {
redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
}
}
public Object getObject(String key) {
return redisTemplate.opsForValue().get(key);
}
}