SpringBoot+Redis實現(xiàn)查找附近用戶的示例代碼
前言
簡單記錄一下使用Redis的GEO命令,SpringDataRedis提供了十分簡單的地理位置定位的功能,實現(xiàn)查找附近用戶的功能。
一、Redis的GEO命令之GEOADD、GEORADIUS命令
1.GEOADD 命令
(1)用法:GEOADD key [longitude][ latitude ][member]
(2)作用:用于存儲地理位置信息,以便進(jìn)行地理位置搜索和距離計算等操作。
(3)返回值:成功添加的成員數(shù)量。
(4)示例:
redis > GEOADD cities 116.4074 39.9042 Beijing redis > GEOADD cities NX 121.4737 31.2304 Shanghai
(5)可選參數(shù):
- NX:只在 key 不存在時才執(zhí)行操作。
- XX:只在 key 存在時才執(zhí)行操作。
- CH:修改成功的成員數(shù)量將被返回
2.GEORADIUS 命令
(1)用法:GEORADIUS key longitude latitude radius
(2)作用:用于查詢指定地理位置附近的其他地理位置的命令。
(3)返回值:返回成員列表。
(4)示例:
redis > GEORADIUS User-Location 116.4074 39.9042 100 km
二、示例代碼
1.控制層
(1)UserController.java
/**
* 更新用戶位置信息
* {
* "longitude": 113.936099,
* "latitude": 22.542364
* }
*/
@PostMapping("updateUserLocation")
@ResponseBody
@CrossOrigin
public <T> T updateUserLocation(@RequestBody HashMap<String, Object> data) {
return userService.updateUserLocation(data);
}
/**
* 更新用戶位置信息
* {
* "longitude": 113.936099,
* "latitude": 22.542364,
* "radius": 10
* }
*/
@PostMapping("nearby")
@ResponseBody
@CrossOrigin
public <T> T nearby(@RequestBody HashMap<String, Object> data) {
return userService.nearby(data);
}2.接口層
(1)IUserService.java
<T> T updateUserLocation(HashMap<String, Object> data); <T> T nearby(HashMap<String, Object> data);
3.實現(xiàn)層
(1)UserServiceImpl.java
@Override
public <T> T updateUserLocation(HashMap<String, Object> data) {
HashMap<String, Object> responseObj = new HashMap<>();
// 獲取登錄用戶
UserDTO userDTO = RequestHolder.getUser();
// 獲取經(jīng)緯度
Double longitude = (Double) data.get("longitude"); // 經(jīng)度
Double latitude = (Double) data.get("latitude"); // 維度
String USER_LOCATION_KEY = "User-Location";
String phone = userDTO.getPhone();
stringRedisTemplate.opsForGeo().add(USER_LOCATION_KEY, new Point(longitude, latitude), phone);
responseObj.put("code", 200);
responseObj.put("success", true);
responseObj.put("msg", "更新完成");
return (T) responseObj;
}
@Override
public <T> T nearby(HashMap<String, Object> data) {
HashMap<String, Object> responseObj = new HashMap<>();
// 獲取登錄用戶
UserDTO userDTO = RequestHolder.getUser();
// 獲取經(jīng)緯度,以及半徑
Double longitude = (Double) data.get("longitude"); // 經(jīng)度
Double latitude = (Double) data.get("latitude"); // 維度
Integer radius = (Integer) data.get("radius"); // 半徑
String USER_LOCATION_KEY = "User-Location";
String phone = userDTO.getPhone();
Distance distance = new Distance(radius, Metrics.KILOMETERS); // 距離,單位為千米
Circle circle = new Circle(new Point(longitude, latitude), distance); // 圓心
// 使用Redis的地理位置操作對象,在指定范圍內(nèi)查詢附近的用戶位置信息
GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = stringRedisTemplate.opsForGeo().radius(USER_LOCATION_KEY, circle);
List<Object> nearbyUsers = new ArrayList<>();
for (GeoResult<RedisGeoCommands.GeoLocation<String>> geoResult : geoResults.getContent()) {
Object memberId = geoResult.getContent().getName();
// 排除查詢用戶本身
if (!memberId.equals(phone)) {
nearbyUsers.add(memberId);
}
}
responseObj.put("code", 200);
responseObj.put("success", true);
responseObj.put("msg", "更新完成");
responseObj.put("data", nearbyUsers);
return (T) responseObj;
}到此這篇關(guān)于SpringBoot+Redis實現(xiàn)查找附近用戶的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot Redis查找附近用戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java自動化設(shè)置PowerPoint幻燈片背景顏色和背景圖片
在日常工作中,PowerPoint?演示文稿是不可或缺的工具,本文將為你揭示如何通過Java,高效、專業(yè)地設(shè)置PowerPoint幻燈片的背景顏色和背景圖片,有需要的可以了解下2025-12-12
Spring響應(yīng)式編程之Reactor操作符詳解
文章介紹了Reactor庫中常用的響應(yīng)式流操作符,分為創(chuàng)建、轉(zhuǎn)換、組合、條件和錯誤處理五類,詳細(xì)列舉了每類操作符的功能和用途,這些操作符旨在提高響應(yīng)式流的可讀性和開發(fā)效率,幫助開發(fā)者更高效地處理數(shù)據(jù)流2025-09-09
springboot動態(tài)定時任務(wù)的實現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于springboot動態(tài)定時任務(wù)的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法
這篇文章主要介紹了Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實例演示了Java引用pinyin4j庫實現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12
JavaWeb實現(xiàn)mysql數(shù)據(jù)庫數(shù)據(jù)的添加和刪除
這篇文章主要介紹了如何利用JavaWeb實現(xiàn)mysql數(shù)據(jù)庫數(shù)據(jù)的添加和刪除功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03
@Transactional注解異常報錯之多數(shù)據(jù)源詳解
這篇文章主要介紹了@Transactional注解異常報錯之多數(shù)據(jù)源詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

