由于最近项目中需要用到距离计算, 附近位置等功能, 就想到redis了的GeoHash, 关于GeoHash的概念就不过多介绍, 用法也比较简单, 但由于项目中使用的是Redission, 网上有关Redission 使用GeoHash的工具类也非常之少, 无奈只能自己动手写了。
话不多说,直接上工具类, 做了两层封装。
一.RedissonTemplate
public class RedissonTemplate {
@Resource
private RedissonClient redissonClient;
private static RedissonTemplate redissonTemplate;
public static RedissonTemplate getInstance() {
if (redissonTemplate == null) {
redissonTemplate = SpringUtils.getBean(RedissonTemplate.class);
}
return redissonTemplate;
}
public boolean zrem(String name, Object o) {
RScoredSortedSet<Object> scoredSortedSet = redissonClient.getScoredSortedSet(name);
return scoredSortedSet.remove(o);
}
public Long geoAdd(String name, double lng, double lat, Object member) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.add(lng, lat, member);
}
public Double geoDist(String name, Object firstMember, Object secondMember, GeoUnit geoUnit) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.dist(firstMember, secondMember, geoUnit);
}
public Map<Object, GeoPosition> geoPos(String name, Object... members) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.pos(members);
}
public Map<Object, String> geoHash(String name, Object... members) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.hash(members);
}
public List<Object> geoRadius(String name, double lng, double lat, double radius, GeoUnit geoUnit) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(lng, lat, radius, geoUnit);
}
public List<Object> geoRadius(String name, double lng, double lat, double radius, GeoUnit geoUnit, int count) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(lng, lat, radius, geoUnit, count);
}
public List<Object> geoRadius(String name, double lng, double lat, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(lng, lat, radius, geoUnit, geoOrder);
}
public List<Object> geoRadiusByMember(String name, Object member, double radius, GeoUnit geoUnit) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(member, radius, geoUnit);
}
public List<Object> geoRadiusByMember(String name, Object member, double radius, GeoUnit geoUnit, int count) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(member, radius, geoUnit, count);
}
public List<Object> geoRadiusByMember(String name, Object member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(member, radius, geoUnit, geoOrder);
}
public List<Object> geoRadiusByMember(String name, Object member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
RGeo<Object> geo = redissonClient.getGeo(name);
return geo.radius(member, radius, geoUnit, geoOrder, count);
}