做网站要主机还是服务器,那些做电影的网站赚钱吗,长沙旅游攻略美食,辛集市建设局网站Redis Geo 使用场景API列表名词API列表Springboot使用mavenyamlTest 注意事项 Redis Geo 是Redis在3.2版本中新增的功能#xff0c;用于存储和操作地理位置信息 使用场景
滴滴打车#xff1a;这是一个对地理位置精度要求较高的场景。通过使用Redis的GEO功能#xff0c;滴滴… Redis Geo 使用场景API列表名词API列表Springboot使用mavenyamlTest 注意事项 Redis Geo 是Redis在3.2版本中新增的功能用于存储和操作地理位置信息 使用场景
滴滴打车这是一个对地理位置精度要求较高的场景。通过使用Redis的GEO功能滴滴打车可以存储房源和店铺的地理位置信息并根据用户所在位置的经纬度加上范围查询到附近的房源和店铺列表放到高德地图中展现出来。直播业务比如主播开播的时候写入主播Id的经纬度关播的时候删除主播Id元素这样就维护了一个具有位置信息的在线主播集合提供给线上检索。自如、蛋壳、链家、美团等平台也有根据距离找房源或者商铺的功能这个功能也是使用的Redis的GEO功能。
API列表名词
字段含义longitude经度latitude纬度member位置名称radius距离中心位置的距离m/km/ft/mi距离中心位置的单位米/千米/英里/英尺WITHCOORD返回距离中心位置元素及经纬度WITHDIST返回距离中心位置元素及距离WITHHASH返回距离中心位置元素及geohash 值COUNT返回距离中心位置元素的元素个数ASC/DESC距离远近排序STORE key返回的集合元素存储到某个key中STOREDIST key返回的元素距离集合存储到某个key中BYRADIUS按圆形扫描BYBOX按矩形扫描
API列表
名称含义指令GEOADD用于存储指定的地理空间位置可以将一个或多个经度(longitude)、纬度(latitude)、位置名称(member)添加到指定的 key 中GEOADD key longitude latitude member [longitude latitude member …]GEOPOS用于从给定的 key 里返回所有指定名称(member)的位置经度和纬度GEOPOS key member [member …]GEOHASH用于获取一个或多个位置元素的 geohash 值GEOHASH key member [member …]GEODIST用于返回两个给定位置之间的距离GEODIST key member1 member2 [m/km/ft/mi]GEORADIUS以给定的经纬度为中心 返回键包含的位置元素中 与中心的距离不超过给定最大距离radius的所有位置元素GEORADIUS key longitude latitude radius [m/km/ft/mi] [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASCGEORADIUSBYMEMBER与GEORADIUS 相似只是该指令是以位置(member)为中心GEORADIUSBYMEMBER key member radius [m/km/ft/mi] [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC/DESC] [STORE key] [STOREDIST key]GEOSEARCH存在高版本Redis中GEORADIUS升级除了可以设置扫描范围还可以设置扫描形状圆形矩形geosearch的功能更加强大和灵活可以满足更多的使用场景和需求GEOSEARCH key member / longitude latitude [BYRADIUS radius [m/km/ft/mi] ]/ [BYBOX width height [m/km/ft/mi]] [ASC/DESC] [COUNT count] [WITHCOORD] [WITHDIST][WITHHASH]
Springboot使用
maven
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-undertow/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdexclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactId/exclusion/exclusions/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdscopetest/scope/dependencyyaml
spring:redis:host: 127.0.0.1 # Redis 服务器地址port: 6379 # Redis 服务器端口database: 0 # Redis 数据库索引默认为0Test
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.test.context.junit4.SpringRunner;import java.util.List;RunWith(SpringRunner.class)
SpringBootTest
public class MyDemoApplicationTests {AutowiredStringRedisTemplate redisTemplate;/*** 将指定的地理空间位置纬度、经度、名称添加到指定的key中。*/Testpublic void redisTestAdd() {Long a redisTemplate.opsForGeo().add(geo, new Point(13.261391222476959, 38.215556214674542), a);//params: key, Point(经度, 纬度), 地方名称Long b redisTemplate.opsForGeo().add(geo, new Point(15.087267458438873, 37.50266842333162), b);//params: key, Point(经度, 纬度), 地方名称System.out.println(a);System.out.println(b);}/*** 从key里返回所有给定位置元素的位置经度和纬度。*/Testpublic void redisTestGeoGet() {ListPoint points redisTemplate.opsForGeo().position(geo, a, b);//params: key, 地方名称...System.out.println(points);}/*** 返回两个给定位置之间的距离。*/Testpublic void testDist() {Distance distance redisTemplate.opsForGeo().distance(geo, a, b, RedisGeoCommands.DistanceUnit.KILOMETERS);//params: key, 地方名称1, 地方名称2, 距离单位System.out.println(distance);}/*** 以给定的经纬度为中心 返回键包含的位置元素当中 与中心的距离不超过给定最大距离的所有位置元素并给出所有位置元素与中心的平均距离。*/Testpublic void redisTestNearByXY() {Circle circle new Circle(new Point(114.05, 22.55), new Distance(200, Metrics.KILOMETERS));//Point(经度, 纬度) Distance(距离量, 距离单位)RedisGeoCommands.GeoRadiusCommandArgs args RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResultsRedisGeoCommands.GeoLocationString results redisTemplate.opsForGeo().radius(geo, circle, args);//params: key, Circle, GeoRadiusCommandArgsSystem.out.println(results);}/*** 以给定的城市为中心 返回键包含的位置元素当中 与中心的距离不超过给定最大距离的所有位置元素并给出所有位置元素与中心的平均距离。*/Testpublic void testNearByPlace() {Distance distance new Distance(200, Metrics.KILOMETERS);//params: 距离量, 距离单位RedisGeoCommands.GeoRadiusCommandArgs args RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResultsRedisGeoCommands.GeoLocationString results redisTemplate.opsForGeo().radius(geo, a, distance, args);//params: key, 地方名称, Circle, GeoRadiusCommandArgsSystem.out.println(results);}/*** 返回一个或多个位置元素的 Geohash 表示*/Testpublic void testGeoHash() {ListString results redisTemplate.opsForGeo().hash(geo, a, b);//params: key, 地方名称...System.out.println(results);}}注意事项
在Redis的集群环境中不建议将大量的数据存储在一个zset集合中因为这会导致集群迁移时出现卡顿现象影响线上服务的正常运行。如果数据量过大需要对数据进行拆分按国家、省份、城市等拆分。建议将频繁访问的数据存储在Redis中而将低频数据存储在其他数据库中。避免将不相关的数据业务都放到一个Redis中这可以避免业务相互影响避免单实例膨胀并能在故障时降低影响面快速恢复。由于Redis是单线程服务消息过大会阻塞并拖慢其他操作。因此需要保持消息内容在1KB以下严禁超过50KB的单条记录。