日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Redis —— SpringBoot工程下的GeoHash工具类

發布時間:2023/12/14 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis —— SpringBoot工程下的GeoHash工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、依賴引入

<!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--redis連接依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

二、工具類代碼

@Component @Slf4j public class RedisUtil {@Value("${spring.redis.geo.key}")private String GEO_REDIS_KEY;@Resourceprivate RedisTemplate<String, Object> redisTemplate;/*** 指定緩存失效時間*/public boolean expire(String key, long time) {try {if (time > 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {log.error("set expire time error.key:{}.", key, e);return false;}}/*** 根據key 獲取過期時間*/public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 判斷key是否存在*/public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {log.error("redis hasKey method error.key:{}.", key, e);return false;}}/*** 刪除緩存*/public void del(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete(Arrays.asList(key));}}}//------------------------------------GEO----------------------------------------/*** 增加用戶位置*/public Long addGeoLocation(String username, double x, double y) {try {RedisGeoCommands.GeoLocation geoLocation = new RedisGeoCommands.GeoLocation(username, new Point(x, y));return redisTemplate.opsForGeo().add(GEO_REDIS_KEY, geoLocation);} catch (Exception exception) {throw new RuntimeException("addGeoLocation error.", exception);}}/*** 刪除用戶位置,GEO實際上放在ZSET結構的數據,因此可用ZREM刪除*/public Long deleteGeoLocation(String username) {try {return redisTemplate.opsForZSet().remove(GEO_REDIS_KEY, username);} catch (Exception exception) {throw new RuntimeException("deleteGeoLocation error.", exception);}}/*** 獲取用戶位置*/public List<Point> getGeoLocation(String username) {try {return redisTemplate.opsForGeo().position(GEO_REDIS_KEY, username);} catch (Exception e) {throw new RuntimeException("getGetLocation error.", e);}}/*** 計算用戶相隔距離*/public Distance getDistance(String usernameOne, String usernameTwo, RedisGeoCommands.DistanceUnit unit) {try {return redisTemplate.opsForGeo().distance(GEO_REDIS_KEY, usernameOne, usernameTwo,unit);} catch (Exception e) {throw new RuntimeException("getDistance error.", e);}}/*** 獲取某個用戶 附近 n個距離單位m 內的附近z個用戶*/public GeoResults<RedisGeoCommands.GeoLocation<Object>> getRadius(String username, int radius, RedisGeoCommands.DistanceUnit unit, int limit) {try {Distance distance = new Distance(radius, unit);RedisGeoCommands.GeoRadiusCommandArgs args =RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(limit);return redisTemplate.opsForGeo().radius(GEO_REDIS_KEY, username, distance, args);} catch (Exception e) {throw new RuntimeException("getDistance error.", e);}} }

三、使用樣例

添加Redis相關yml配置。

# redis配置,以下有默認配置的也可以使用默認配置 spring:redis:host: 127.0.0.1port: 6379password: 123456 # 如果無密碼,直接注釋掉即可timeout: 1000pool:minIdle: 5maxIdle: 10maxWait: -1maxActive: 20geo:key: spring_homeworkapplication:name: position-provider

工具類相關Bean。

// ----------------------UserPosition------------------------------ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;import java.io.Serializable;@Data @NoArgsConstructor @AllArgsConstructor public class UserPosition implements Serializable {private String username; // 用戶名private double longitude; // 經度private double latitude; // 緯度 }// -----------------------UserNearby----------------------------- import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.geo.Distance;import java.io.Serializable;@Data @AllArgsConstructor @NoArgsConstructor public class UserNearby implements Serializable {private Distance distance;private UserPosition position; }

Service層使用RedisUtil。

@Slf4j public class PositionServiceImpl implements PositionService {@Autowiredprivate RedisUtil redisUtil;@Overridepublic List<UserPosition> getGeoLocation(String username) {log.info("username:{} delete geoLocation.",username);List<Point> points = redisUtil.getGeoLocation(username);List<UserPosition> positions = new ArrayList<>();for (Point point : points) {if (Objects.nonNull(point)) {positions.add(new UserPosition(username,point.getX(),point.getY()));}}return positions;}@Overridepublic Long addGeoLocation(String username, double x, double y) {return redisUtil.addGeoLocation(username, x, y);}@Overridepublic Long deleteGeoLocation(String username) {log.info("username:{} delete geoLocation.",username);return redisUtil.deleteGeoLocation(username);}@Overridepublic Distance getDistance(String usernameOne, String usernameTwo, RedisGeoCommands.DistanceUnit unit) {return redisUtil.getDistance(usernameOne, usernameTwo, unit);}@Overridepublic List<UserNearby> getRadius(String username, int radius, RedisGeoCommands.DistanceUnit unit, int limit) {GeoResults<RedisGeoCommands.GeoLocation<Object>> result = redisUtil.getRadius(username, radius, unit, limit);List<UserNearby> userNearbyList = new ArrayList<>();result.getContent().forEach((one) -> {userNearbyList.add(new UserNearby(one.getDistance(), new UserPosition((String) one.getContent().getName(),one.getContent().getPoint().getX(),one.getContent().getPoint().getY())));});return userNearbyList;} }

總結

以上是生活随笔為你收集整理的Redis —— SpringBoot工程下的GeoHash工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。