Redis以二進制形式存儲對象

代碼已測試 直接測試就可以  ,注意 實體類一定要  實現  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);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章