package com.gkhy.safePlatform.specialWork.service.impl; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.utils.StringUtils; import com.gkhy.safePlatform.specialWork.service.RedisService; import com.gkhy.safePlatform.specialWork.util.RedisUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service("specialWorkRedisService") public class RedisServiceImpl implements RedisService { @Autowired private RedisUtils redisUtils; @Override public void setConsumerMessageCacheKeyAndExpireTime(String key, Long expireTime) { if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } if (expireTime == null) { expireTime = 5L; } // 任意塞的一个值 redisUtils.set(key, Boolean.TRUE, expireTime, TimeUnit.SECONDS); } @Override public String getCacheKey(String key) { if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } Object o = redisUtils.get(key); if (o != null) { return o.toString(); } return null; } }