import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisKit {
private RedisTemplate redisTemplate;
/**
* 存入redis GEO缓存
* @param key 键
* @param longitude 经度 String类型
* @param latitude 纬度 String类型
* @param member 店铺id
* @return true/false
*/
public boolean setGeo(String key,double longitude, double latitude,String member){
boolean result = false;
try {
redisTemplate.opsForGeo().add(key,new Point(longitude,latitude),member);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 移除地理位置信息
* @param key
* @param members 店铺id
* @ReturnType:boolean
*/
public boolean removeGeo(String key, String members) {
try {
redisTemplate.opsForGeo().remove(key,members);
} catch (Throwable t) {
logger.error("移除[" + key +"]" + "失败" +", error[" + t + "]");
}
return true;
}
/**
* 获得某个位置的经纬度
* @param key 键
* @param member 店铺id
*
*/
public List<Point> getGeo(String key,String member){
try {
GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
List<Point> position = geoOps.position(key, member);
return position;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据给定地理位置坐标获取指定范围内的地理位置集合
* @param key 键
* @param longitude 经度 String类型
* @param latitude 纬度 String类型
* @param distance 距离 没有时默认5km
* @return list 商家id集合
*/
public List<GeoVo> getShopIdByGeo(String key, double longitude, double latitude, String mi){
double distance = Double.valueOf(StrKit.isEmpty(mi)?"3000":mi);//不选择时,默认3000m
List<GeoVo> geos = new ArrayList<>();
BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps(key);
Point point = new Point(longitude, latitude);
Circle within = new Circle(point, distance);
RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
//返回的结果包括 距离 和 坐标
geoRadiusArgs = geoRadiusArgs.includeDistance();
//限制查询返回的数量
// geoRadiusArgs.limit(10);
//按查询出的坐标距离中心坐标的距离进行排序
geoRadiusArgs.sortAscending();
GeoResults<GeoLocation<String>> geoResults = boundGeoOperations.radius(within, geoRadiusArgs);
List<GeoResult<GeoLocation<String>>> geoResultList = geoResults.getContent();
//GeoVo自己由于某些需求而做的一个封装类,可以直接用geoResultList
GeoVo geoVo = null;
for (GeoResult<GeoLocation<String>> geoResult : geoResultList) {
geoVo = new GeoVo();
String name = geoResult.getContent().getName();
Distance distance1 = geoResult.getDistance();
geoVo.setMember(name);
String str = String.valueOf(distance1);
String m1 = StringUtils.substringBeforeLast(str, "M");
geoVo.setDistance(m1);
geos.add(geoVo);
}
return geos;
}
}
Redis实现获取附近店铺
最新推荐文章于 2023-03-01 20:53:13 发布