高品质的网站设计制作,怎么判断网站是否被收录,前端开发面试题,温州网站设计Redis 3.2版本以后#xff0c;基于geohash和数据结构Zset提供了地理位置相关功能。通过上边两种mysql的实现方式发现#xff0c;附近的人功能是明显的读多写少场景#xff0c;所以用redis性能更会有很大的提升。
redis 实现附近的人功能主要通过Geo模块的六个命令。
GEOADD基于geohash和数据结构Zset提供了地理位置相关功能。通过上边两种mysql的实现方式发现附近的人功能是明显的读多写少场景所以用redis性能更会有很大的提升。redis 实现附近的人功能主要通过Geo模块的六个命令。GEOADD将给定的位置对象纬度、经度、名字添加到指定的key;GEOPOS从key里面返回所有给定位置对象的位置经度和纬度;GEODIST返回两个给定位置之间的距离;GEOHASH返回一个或多个位置对象的Geohash表示;GEORADIUS以给定的经纬度为中心返回目标集合中与中心的距离不超过给定最大距离的所有位置对象;GEORADIUSBYMEMBER以给定的位置对象为中心返回与其距离不超过给定最大距离的所有位置对象。GEOADD key longitude latitude member [longitude latitude member ...] 如下 GEOADD hotel 119.98866180732716 30.27465803229662 酒馆java中使用redis实现package com.demo.controller; import lombok.Data; import lombok.experimental.Accessors; import org.example.DemoRedisApplication; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.geo.*; import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.ArrayList; import java.util.List; SpringBootTest(classes DemoRedisApplication.class) public class Test1 { Autowired private StringRedisTemplate stringRedisTemplate; Autowired private RedisTemplateString, Object redisTemplate; Test public void test1() { //save(); ListUserInfo userInfos nearBySearch(100, 116.397428, 39.90923); userInfos.forEach(System.out::println); } //GEO相关命令用到的KEY private final static String KEY user_info; public boolean save() { ListUserInfo list new ArrayList(); list.add(new UserInfo().setName(张三).setLatitude(39.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(王五).setLatitude(39.90923).setLongitude(26.397428)); list.add(new UserInfo().setName(赵六).setLatitude(19.90923).setLongitude(106.397428)); list.add(new UserInfo().setName(小二).setLatitude(9.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小三).setLatitude(69.90923).setLongitude(96.397428)); list.add(new UserInfo().setName(小四).setLatitude(39.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小五).setLatitude(69.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小六).setLatitude(59.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小七).setLatitude(79.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小八).setLatitude(9.90923).setLongitude(116.397428)); list.add(new UserInfo().setName(小九).setLatitude(32.90923).setLongitude(106.397428)); for (UserInfo userInfo : list){ Long flag redisTemplate.opsForGeo().add(KEY, new RedisGeoCommands.GeoLocation( userInfo.getName(), new Point(userInfo.getLongitude(), userInfo.getLatitude())) ); } return true; /*Long flag redisTemplate.opsForGeo().add(KEY, new RedisGeoCommands.GeoLocation( userInfo.getName(), new Point(userInfo.getLongitude(), userInfo.getLatitude())) ); return flag ! null flag 0;*/ } /** * 根据当前位置获取附近指定范围内的用户 * param distance 指定范围 单位km 可根据{link org.springframework.data.geo.Metrics} 进行设置 * param userLng 用户经度(-180,180) * param userLat 用户纬度(-90,90) * return */ public ListUserInfo nearBySearch(double distance, double userLng, double userLat) { ListUserInfo users new ArrayList(); // 1.GEORADIUS获取附近范围内的信息 GeoResultsRedisGeoCommands.GeoLocationObject reslut redisTemplate.opsForGeo().radius(KEY, new Circle(new Point(userLng, userLat), new Distance(distance, Metrics.KILOMETERS)), RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs() .includeDistance() .includeCoordinates().sortAscending()); //2.收集信息存入list ListGeoResultRedisGeoCommands.GeoLocationObject content reslut.getContent(); //3.过滤掉超过距离的数据 content.forEach(a- { users.add( new UserInfo() .setName(a.getContent().getName().toString()) .setDistance(a.getDistance().getValue()) .setLatitude(a.getContent().getPoint().getX()) .setLongitude(a.getContent().getPoint().getY())); }); return users; } Data Accessors(chain true) public static class UserInfo{ private String name;// 酒店 private double latitude;// 用户经度(-180,180) private double longitude;// 用户纬度(-90,90) private double distance;// 距离 } }测试一下ok了还可以使用 mongdb来实现。