SpringBoot+Redis實現(xiàn)查找附近用戶的示例代碼
前言
簡單記錄一下使用Redis的GEO命令,SpringDataRedis提供了十分簡單的地理位置定位的功能,實現(xiàn)查找附近用戶的功能。
一、Redis的GEO命令之GEOADD、GEORADIUS命令
1.GEOADD 命令
(1)用法:GEOADD key [longitude][ latitude ][member]
(2)作用:用于存儲地理位置信息,以便進行地理位置搜索和距離計算等操作。
(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的Object里wait()實現(xiàn)原理講解
這篇文章主要介紹了java的Object里wait()實現(xiàn)原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09idea2020.1.3 手把手教你創(chuàng)建web項目的方法步驟
這篇文章主要介紹了idea 2020.1.3 手把手教你創(chuàng)建web項目的方法步驟,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08