0
点赞
收藏
分享

微信扫一扫

Redis以二进制形式存储对象

niboac 2022-04-07 阅读 96
java后端

代码已测试 直接测试就可以 ,注意 实体类一定要 实现 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<String, Object> 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);
}

}

举报

相关推荐

0 条评论