import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class RedisService {
private static final Logger log = LoggerFactory.getLogger(RedisService.class);
public RedisTemplate redisTemplate;
public List range(String key, long start, long end) {
if (RedisStatusTask.redisUp()) {
ListOperations listOperations = redisTemplate.opsForList();
return listOperations.range(key,start,end);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public void leftPush(final String key,final T obj) {
if (RedisStatusTask.redisUp()) {
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPush(key,obj);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public boolean setNx(final String lockPrefix,final String key, long timeout) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.opsForValue().setIfAbsent(lockPrefix + key, System.currentTimeMillis(),timeout,TimeUnit.SECONDS);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public void setCacheObject(final String key, final T value) {
if (RedisStatusTask.redisUp()) {
redisTemplate.opsForValue().set(key, value);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public void setCacheObject(final T value, final String... keys) {
String key = StringUtils.join(keys, ":");
setCacheObject(key, value);
public void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
if (RedisStatusTask.redisUp()) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public void setCacheObject(final T value, final Long timeout, final TimeUnit timeUnit, final String... keys) {
String key = StringUtils.join(keys, ":");
setCacheObject(key, value, timeout, timeUnit);
public boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
public boolean expire(final long timeout,final TimeUnit unit,final String...keys) {
String key = StringUtils.join(keys, ":");
return expire(key, timeout, unit);
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.expire(key, timeout, unit);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public T getCacheObject(final String key) {
if (RedisStatusTask.redisUp()) {
ValueOperations operation = redisTemplate.opsForValue();
return operation.get(key);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public T getCacheObject(final String... keys) {
String key = StringUtils.join(keys, ":");
return getCacheObject(key);
public boolean deleteObject(final String key) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.delete(key);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public boolean deleteObject(final String... keys) {
String key = StringUtils.join(keys, ":");
return deleteObject(key);
public long deleteObject(final Collection collection) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.delete(collection);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Collection keys(final String pattern) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.keys(pattern);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Long incrBy(Long incr, String... keys) {
if (RedisStatusTask.redisUp()) {
String key = StringUtils.join(keys, ":");
return redisTemplate.opsForValue().increment(key, incr);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Long decrBy(Long decr, String... keys) {
if (RedisStatusTask.redisUp()) {
String key = StringUtils.join(keys, ":");
return redisTemplate.opsForValue().decrement(key, decr);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Set members(final String key) {
if (RedisStatusTask.redisUp()) {
return redisTemplate.opsForSet().members(key);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Long addSet(String key, Object... value) {
if (RedisStatusTask.redisUp()) {
size = redisTemplate.opsForSet().add(key, value);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Long removeSet(String key, Object value) {
if (RedisStatusTask.redisUp()) {
size = redisTemplate.opsForSet().remove(key, value);
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Long countSet(String key) {
if (RedisStatusTask.redisUp()) {
size = redisTemplate.opsForSet().size(key);
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Boolean hasMemberSet(String key, Object value) {
if (RedisStatusTask.redisUp()) {
exist = redisTemplate.opsForSet().isMember(key, value);
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public Boolean hasKey(String key) {
if (RedisStatusTask.redisUp()) {
exist = redisTemplate.hasKey(key);
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
public List getCacheList(final String... keys) {
String key = StringUtils.join(keys, ":");
return getCacheList(key);
public List getCacheList(final String key) {
if (RedisStatusTask.redisUp()) {
Collection keys = keys(key);
ValueOperations operation = redisTemplate.opsForValue();
return operation.multiGet(keys);
} catch (RedisConnectionFailureException e) {
log.error(e.getMessage());
RedisStatusTask.redisMayDown();
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.actuate.redis.RedisHealthIndicator;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
public class RedisStatusTask {
private static int status = 3;
private RedisConnectionFactory redisConnectionFactory;
@Scheduled(fixedDelay = 1*60*1000)
RedisHealthIndicator redisHealthIndicator = new RedisHealthIndicator(redisConnectionFactory);
Health health = redisHealthIndicator.getHealth(false);
String code = health.getStatus().getCode();
if(code.equals(Status.UP.getCode()) && status<3){
public static boolean redisUp(){
return RedisStatusTask.status > 0;
public static void redisMayDown(){