RedisUtils工具类

文章展示了在SpringBoot应用中如何配置Redis连接,包括host、port和timeout等参数。同时,给出了一个名为RedisUtils的工具类,用于执行基本的Redis操作,如获取、设置、更新和删除缓存。该类还包含了异常处理和日志记录。此外,文章中还有一个服务类示例,展示了如何在业务逻辑中使用RedisUtils进行数据的存取和管理。

redis配置

  # redis config
    redis:
      host: 127.0.0.1
      port: 6379
      timeout: 2000
      password:
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        min-idle: 0
        max-idle: 8

RedisUtils工具类

package com.maize.trees.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author: ming
 * @Date: 2022/8/1 17:37
 * @Description: redisUtils 工具类
 */
@Slf4j
@Component
public class RedisUtils {

    //    @Autowired
//    private RedisTemplate redisTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * @Description 获得缓存数据
     * @Author ming
     * @Date 2022/8/1 17:54
     * @Param key:
     * @return:
     * @Exception
     */
    public Object get(final String key) {
        Object result = null;
        try {
            result = redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            e.printStackTrace();

        }
        return result;
    }

    /**
     * 更新缓存
     */
    public boolean getAndSet(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            result = redisTemplate.delete(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Description :是不是存在key
     * @Author ming
     * @Date 2022/8/4 10:42
     * @Param String:key
     * @return: boolean
     * @Exception
     */
    public boolean hasKey(final String key) {
        boolean result = false;
        try {
            result = redisTemplate.hasKey(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            e.printStackTrace();
        }
        return result;
    }


}

service

package com.maize.trees.Iservice.impl;

import com.maize.trees.Iservice.AutoSaveService;
import com.maize.trees.config.jpa.JpaStatementInspector;
import com.maize.trees.controller.pojo.dto.AutoSaveTaskDTO;
import com.maize.trees.controller.pojo.vo.AutoSaveVO;
import com.maize.trees.dao.DeviceAppointDao;
import com.maize.trees.persistence.jpa.repositories.TreesDeviceTypeRepository;
import com.maize.trees.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;


/**
 * @author: ming
 * @Date: 2022/8/1 16:35
 * @Description:
 */
@Slf4j
@Service
public class AutoSaveServiceImpl implements AutoSaveService {
    //    @Autowired
//    private AutoSaveVO autoSaveVO;
    @Resource
    private RedisUtils redisUtils;

    @Autowired
    private DeviceAppointDao deviceAppointDao;
    @Autowired
    private TreesDeviceTypeRepository treesDeviceTypeRepository;

    /**
     * @Description 自动保存任务数据到redis
     * @Author ming
     * @Date 2022/8/1 16:44
     * @Param dto:
     * @Param lab:
     * @return: com.github.pagehelper.PageInfo<com.maize.trees.controller.pojo.vo.AutoSaveVO>
     * @Exception
     */
    @Override
    public boolean autoSaveTaskOne(AutoSaveTaskDTO dto) {
        boolean result = redisUtils.set("Tress:AutoSaveTask:" + dto.getTaskInformation().getId(), dto);
        return result;
    }

    /**
     * @Description 获取redis中缓存的数据
     * @Author ming
     * @Date 2022/8/4 10:03
     * @Param taskId:
     * @Param lab:
     * @return: java.lang.Object
     * @Exception
     */
    @Override
    public AutoSaveVO getAutoSaveTaskOne(Integer taskId) {

        AutoSaveTaskDTO dto = (AutoSaveTaskDTO) redisUtils.get("Tress:AutoSaveTask:" + taskId);
        AutoSaveVO vo = AutoSaveVO.builder()
                .taskInformation(dto.getTaskInformation())
                .parmasStep(dto.getParmasStep())
                .protectList(dto.getProtectList())
                .recoverList(dto.getRecoverList())
                .loopList(dto.getLoopList())
                .build();
        return vo;
    }

    /**
     * @Description 更新redis里面的任务缓存
     * @Author ming
     * @Date 2022/8/4 10:11
     * @Param dto:
     * @return: boolean
     * @Exception
     */
    @Override
    public boolean updateAutoSaveTaskOne(AutoSaveTaskDTO dto) {
        boolean result = redisUtils.getAndSet("Tress:AutoSaveTask:" + dto.getTaskInformation().getId(), dto);
        return result;
    }

    /**
     * @Description
     * @Author ming
     * @Date 2022/8/4 10:15
     * @Param : 任务id  设备id
     * @return: boolean
     * @Exception
     */
    @Override
    public boolean deleteAutoSaveTaskOne(Integer taskId) {
//        Integer deviceType = deviceAppointDao.getDeviceTypeByDeviceId(taskId);
        JpaStatementInspector.isInspectSetter.set(Collections.singletonMap(String.valueOf(Thread.currentThread().getId()), Boolean.FALSE));
        Integer deviceType = treesDeviceTypeRepository.getDeviceTypeByTaskId(taskId);
        boolean result = false;
//        除开烘箱的其它设备需要清除redis缓存
        try {
            if (!deviceType.equals(4)) {
                result = redisUtils.delete("Tress:AutoSaveTask:" + taskId);
            } else {
                result = true;
            }
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }


        return result;
    }

    /**
     * @param taskId
     * @Description 判断是否存在key
     * @Author ming
     * @Date 2022/8/4 10:48
     * @Param taskId:
     * @return: boolean
     * @Exception
     */
    @Override
    public boolean hasAutoSaveTaskOne(Integer taskId) {
        boolean result = redisUtils.hasKey("Tress:AutoSaveTask:" + taskId);

        return result;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值