redis 查找附近的人
儿童定位手表,有个交友功能,查找附近的人,用redis的geo来实现比较简单,其实是一个ZSET(有序集合)
redis 版本要大于3.2
查看redis 版本 /usr/bin/redis-server --version
注意引入的jar版本:可能运行时候会报错,这时要检查jar包的版本,可能版本冲突导致报错
public class Coordinate {
//经度
private double longitude;
//纬度
private double latitude;
//用户id
private String key;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
public class RedisUtil {
private static JedisPool jedisPool = null;
// Redis服务器IP
private static String ADDR = "xx.xxx.xx.xx";
// Redis的端口号
private static int PORT = ;
// 访问密码
private static String AUTH = "xxxxxx";
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);
// 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
// 是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);
// 最大空闲连接数, 默认8个 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle();
// 最大连接数, 默认8个
config.setMaxTotal();
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis( * );
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
jedisPool = new JedisPool(config, ADDR, PORT, , AUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
*
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
public static void main(String[] args) {
Jedis jedis = RedisUtil.getJedis();
// //添加经纬度
// Coordinate coordinate=new Coordinate();
// coordinate.setLatitude(31.244803); //维度
// coordinate.setLongitude(121.483671); //经度
// coordinate.setKey("zhangsan"); //可以作为用户表的id
//
//
// //添加经纬度
// Coordinate coordinate1=new Coordinate();
// coordinate1.setLatitude(31.245321); //维度
// coordinate1.setLongitude(121.485015); //经度
// coordinate1.setKey("lisi"); //可以作为用户表的id
//
// //添加经纬度
// Coordinate coordinate2=new Coordinate();
// coordinate2.setLatitude(31.245456); //维度
// coordinate2.setLongitude(121.485285); //经度
// coordinate2.setKey("wangwu"); //可以作为用户表的id
//
// addReo(coordinate);
// addReo(coordinate1);
// addReo(coordinate2);
// Coordinate query = new Coordinate();
// query.setLongitude(121.485285);
// query.setLatitude(31.245456);
//
// List<GeoRadiusResponse> result = geoQuery(query);
// for(GeoRadiusResponse res : result) {
// System.out.println(res.getMemberByString());
// }
RedisUtil.close(jedis);
}
/**
* 添加坐标
* key 经度 维度 距离
* return m 表示单位为米*/
public static Long addReo(Coordinate coordinate) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//第一个参数可以理解为表名
return jedis.geoadd("test",coordinate.getLongitude(),coordinate.getLatitude(),coordinate.getKey());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 查询附近人
* key 经度 维度 距离
* return GeoRadiusResponse*/
public static List<GeoRadiusResponse> geoQuery(Coordinate coordinate) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//200F GeoUnit.KM表示km
return jedis.georadius("test",coordinate.getLongitude(),coordinate.getLatitude(),100F,GeoUnit.M, GeoRadiusParam.geoRadiusParam().withDist());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
}
引用相关jar

转载自:https://blog.csdn.net/liaodehong/article/details/59104451
redis 查找附近的人的更多相关文章
- Redis(6)——GeoHash查找附近的人
像微信 "附近的人",美团 "附近的餐厅",支付宝共享单车 "附近的车" 是怎么设计实现的呢? 一.使用数据库实现查找附近的人 我们都知道, ...
- Redis实战篇(四)基于GEO实现查找附近的人功能
如果现在要开发一个功能: 要为一款交友App实现查找附近的人,并按距离进行排序. 让你来开发这个功能,你会如何实现? MySQL 不合适 你可能想到,把用户用户的经纬度坐标使用MySQL等关系数据库( ...
- 使用PHP实现查找附近的人
https://zhuanlan.zhihu.com/p/31380780 LBS(基于位置的服务) 查找附近的人有个更大的专有名词叫做LBS(基于位置的服务),LBS是指是指通过电信移动运营商的无线 ...
- 源码编译Redis Desktop Manager | 懒人屋
原文:源码编译Redis Desktop Manager | 懒人屋 源码编译Redis Desktop Manager 2.3k 字 10 分钟 2019-10-10 文章背景 本 ...
- redis 的使用,及如何使用redis维护数亿人的登录状态
一.redis中几个常用的方法 redis的使用场景移步本文 select db redis 下默认有有16个表,0~15可以通过:select 2 或者 select 11这样的方式切换表 keys ...
- 【Redis数据库】再有人问你CAP理论是什么,就把这篇文章发给他
CAP是Consistency(一致性),Availability(可用性),Partition tolerance(分区容错性)的缩写.在学习redis过程中看到这个名词,查找各位大佬的文章发现这篇 ...
- redis查找大key
redis中查找出比较大的key 下面直接上代码 (请在测试机上测试) #!/usr/bin/env python import sys import redis def check_big_key( ...
- 终极二分查找--传说十个人写九个有bug
之前写过一篇极为罗嗦的二分查找,非常得意地以为以后就可以避免踩坑了,但是今天才知道二分查找可以写的既简洁又鲁棒,唉!还是要多学习啊! 给一个按照从大到小的顺序排序好的数组a[]={1,2,3,4,7, ...
- IntelliJ IDEA - 查找代码提交人
转载. https://blog.csdn.net/abcyyjjkk/article/details/88995503 如果Annocation不可用
随机推荐
- PHP生成器细说
之前写过关于生成器的文章,可能还不够详细,正好群里有朋友在讨论.觉得还是有必要再细说下,如果大家做过Python或者其他语言的,对于生成器应该不陌生.生成器是PHP 5.5.才引入的功能,也许大家觉得 ...
- python同步、互斥锁、死锁
目录 同步 同步的概念 解决线程同时修改全局变量的方式 互斥锁 使用互斥锁完成2个线程对同一个全局变量各加9999999 次的操作 上锁解锁过程 总结 死锁 避免死锁 同步 同步的概念 同步就是协同步 ...
- Android Studio 学习(四) 数据库
文件存储 写数据 String data = "Data ti save"; FileOutputStream out =null; BufferedWriter writer = ...
- JVM 垃圾回收机制
首先JVM的内存结构包括五大区域: 程序计数器.虚拟机栈.本地方法栈.方法区.堆区.其中程序计数器.虚拟机栈和本地方法栈3个区域随线程启动与销毁, 因此这几个区域的内存分配和回收都具有确定性,不需要过 ...
- CodeMirror 使用
codemirror是一款在线代码编辑器,官网:https://codemirror.net/doc/manual.html#overview 使用说明 第一步 安装: npm install cod ...
- JS检测浏览器Adobe Reader插件
Web应用中当我们希望向用户显示pdf文档时候,如果用户安装了Adobe Reader之类的pdf阅读器,就可以直接打开文档在浏览器中显示, 但是,当用户没有安装这类软件的时候,自然是打不开的,为了系 ...
- leetcode-83.删除排序链表中的重复元素
leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1- ...
- (网页)jquery-qrcode.js生成二维码
基础用法 <div id="qrcode"></div> <script type="text/javascript"> n ...
- Mac 电脑如何安装mac os 和win7双系统(win7多分区)
转载:Mac 电脑如何安装mac os 和win7双系统(win7多分区) 本文主要参考了2篇博文,并通过自己的亲身实践总结的.参考的2篇博文地址: http://wenku.baidu.com/li ...
- [20190322]测试相同语句遇到导致cursor pin S的疑问.txt
[20190322]测试相同语句遇到导致cursor pin S的疑问.txt--//昨天测试遇到的情况,链接:http://blog.itpub.net/267265/viewspace-26388 ...