当前位置:网站首页>.netcore redis GEO类型
.netcore redis GEO类型
2022-06-30 21:08:00 【小兜全糖(Cx)】
Redis GEO 主要用于存储地理位置信息,并对存储的信息进行操作,该功能在 Redis 3.2 版本新增。
demo
using CoreRedis.Config;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
namespace CoreRedis.RedisDataType
{
public class RedisGeo
{
private readonly ConnectionMultiplexer _connectionMultiplexer;
private readonly IDatabase _database;
private readonly RedisConfig _redisConfig;
public RedisGeo(IOptionsMonitor<RedisConfig> redisConfig)
{
_redisConfig = redisConfig.CurrentValue;
_connectionMultiplexer = ConnectionMultiplexer.Connect(_redisConfig.Value);
_database = _connectionMultiplexer.GetDatabase();
}
/* 1. 将位置名称以及经纬度存储到位置集合中 2. 根据给定的位置名称从位置集合中取出经纬度 3. 计算两个位置之间的直线距离 4. 根据给定的位置或者经纬度,找出指定半径内的其他位置 5. 获取指定位置的GEOHASH值 */
//将位置名称以及经纬度存储到位置集合中
//返回TRUE表示新增,FALSE表示更新
public async Task<Task<bool>> AddGeo(string name,double longitude, double latitude,string key)
{
return _database.GeoAddAsync(key, longitude, latitude,name);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <returns></returns>
public async Task<Task<bool>> DeleteGeo(string name,string key)
{
return _database.GeoRemoveAsync(key, name);
}
//获取指定位置的坐标
public async Task<Task<GeoPosition?>> GEOPOS(string name,string key)
{
return _database.GeoPositionAsync(key, name);
}
/// <summary>
///
/// 计算两点间的直线距离,GeoUnit 指定单位
/// </summary>
/// <param name="position1"></param>
/// <param name="position2"></param>
/// <param name="key"></param>
/// <returns></returns>
public async Task<Task<double?>> GeoDistance(string position1,string position2,string key)
{
return _database.GeoDistanceAsync(key,position1 ,position2,GeoUnit.Kilometers);
}
/// <summary>
/// 根据经纬度获取指定半径内的地点,并排序,返回距离
/// </summary>
/// <param name="key"></param>
/// <param name="longitude"></param>
/// <param name="latitude"></param>
/// <param name="radius"></param>
/// <param name="order"></param>
/// <param name="count"></param>
/// <returns></returns>
public async Task<Task<GeoRadiusResult[]>> GEORADIUS(string key, double longitude, double latitude,double radius,Order order=Order.Descending,int count=-1)
{
return _database.GeoRadiusAsync(key, longitude, latitude,radius,GeoUnit.Kilometers,count,order,GeoRadiusOptions.WithDistance);
}
/// <summary>
/// 计算位置的hash , 可以根据hash反推出来经纬度
/// </summary>
/// <param name="key"></param>
/// <param name="name"></param>
/// <returns></returns>
public async Task<Task<string>> GEOHASH(string key,string name)
{
return _database.GeoHashAsync(key, name);
}
}
}
controller
using CoreRedis.RedisDataType;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace CoreRedis.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class RedisGeoController : ControllerBase
{
// GET: api/<RedisGeoController>
private readonly RedisGeo _redisGeo;
public RedisGeoController(RedisGeo redisGeo)
{
_redisGeo = redisGeo;
}
[HttpGet]
public async Task<GeoPosition?> GEOPOS(string key,string name)
{
return await _redisGeo.GEOPOS(name, key).Result;
}
// POST api/<RedisGeoController>
[HttpPost]
public async Task<bool> AddGeo([FromQuery] string key, [FromQuery] string name, [FromQuery] double longitude, [FromQuery] double latitude)
{
return await _redisGeo.AddGeo(name, longitude, latitude, key).Result;
}
// DELETE api/<RedisGeoController>/5
[HttpDelete]
public async Task<bool> Delete([FromQuery] string key, [FromQuery] string name)
{
return await _redisGeo.DeleteGeo(name,key).Result;
}
}
}
边栏推荐
- ArcGIS construction and release of simple road network data service and rest call test
- Lumiprobe蛋白质定量丨QuDye 蛋白定量试剂盒
- Electronic scheme development - Intelligent rope skipping scheme
- Personal developed penetration testing tool Satania
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等
- uniapp-第三方网络请求
- go搭建服务器基础
- Lumiprobe protein quantitation - qudye Protein Quantitation Kit
- Metauniverse may become a new direction of Internet development
猜你喜欢

3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充

Lumiprobe细胞生物学丨DiA,亲脂性示踪剂说明书
![翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]](/img/a8/6472e2051a295f5e42a88d64199517.png)
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]

BioVendor sRAGE Elisa试剂盒测试原理和注意事项

Qiao NPMS: search for NPM packages

MySQL introduction, detailed installation steps and usage | dark horse programmer

Web APIs 综合案例-Tab栏切换 丨黑马程序员

Basic components of STL

B_QuRT_User_Guide(31)

文本生成模型退化怎麼辦?SimCTG 告訴你答案
随机推荐
Label Contrastive Coding based Graph Neural Network for Graph Classification
微信小程序怎么实现圆心进度条
[original] unable to use the code dialog height and width to solve the screen problem
Go build server Foundation
三个火枪手
Go learning notes
雷达数据处理技术
Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
B_QuRT_User_Guide(34)
uniapp-生命周期/路由跳转
B_QuRT_User_Guide(32)
Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
uniapp-路由uni-simple-router
On the charm of code language
MySQL简介、详细安装步骤及使用 | 黑马程序员
ssh-server配置文件参数PermitRootLogin介绍
DM8:生成DM AWR报告
转:用实际行动赢得别人追随
WebRTC系列-网络传输之本地scoket端口
Vite2兼容低版本chrome(如搜狗80),通过polyfills处理部分需求高版本的语法