package com.dw.study.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @Author
* @ClassName RedisUtils
* @Description
* @Date 2021/1/20 12:22
* @Version 1.0
*/
@Component
public class RedisUtils {
private final static Logger log = LoggerFactory.getLogger(RedisUtils.class);
@Autowired
private RedisTemplate redisTemplate;
// ##########################【操作String类型】#####################################################
/**
* 设置缓存
*
* @param key
* @param value
* @return
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 设置值并设置过期时间(单位秒)
*
* @param key
* @param value
* @param time 过期时间
* @return
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 设置一个已经存在的key的值,并返回旧值
*
* @param key
* @param value
* @return
*/
public Object getAndSet(String key, Object value) {
try {
Object andSet = redisTemplate.opsForValue().getAndSet(key, value);
return andSet;
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* 如果不存在则设置值value,返回true。 否则返回false
*
* @param key
* @param value
* @return
*/
public boolean setIfAbsent(String key, String value) {
try {
return redisTemplate.opsForValue().setIfAbsent(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 批量设置 k->v 到 redis
*
* @param valueMap
* @return
*/
public boolean multiSet(HashMap valueMap) {
try {
redisTemplate.opsForValue().multiSet(valueMap);
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 如果不存在对应的Map,则批量设置 k->v 到 redis
*
* @param valueMap
* @return
*/
public boolean multiSetIfAbsent(HashMap valueMap) {
try {
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 在原有的值基础上新增字符串到末尾
*
* @param key
* @param value
* @return
*/
public boolean append(String key, String value) {
try {
redisTemplate.opsForValue().append(key, value);
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 获取value
*
* @param key
* @return
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 批量获取值
*
* @param keys
* @return
*/
public List