Redis存储与取出对象

本文主要探讨如何使用Redis存储和检索Java对象,详细阐述了相关步骤和关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Redis存储与取出对象

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Tian Chi
 * @Date: 2021/01/13/16:56
 * @Description: 聪明伶俐又帅气!@@@
 */
@Service
@Log4j2
public class RedisServiceImpl{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    public void setStr(String key, String value) {
        setStr(key, value, null);
    }


    public void setStr(String key, Object value, Long time){
            if(value == null){
               return;
              }
            if(value instanceof String){
                String obj = (String) value;
                stringRedisTemplate.opsForValue().set(key, obj);
                }else if(value instanceof List){
                        List obj = (List) value;
                        stringRedisTemplate.opsForList().leftPushAll(key,obj);
                    }else if(value instanceof Map){
                            Map obj = (Map) value;
                            stringRedisTemplate.opsForHash().putAll(key,obj);
                    }
            if (time != null)
                        stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
                 }


    public Object getKey(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void delKey(String key) {
        stringRedisTemplate.delete(key);
    }

    public void setKeyOutTime(String key,Long value) {
        stringRedisTemplate.expire(key,value,TimeUnit.SECONDS);
    }





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值