当前位置:网站首页>. NETCORE redis geo type
. NETCORE redis geo type
2022-06-30 21:11:00 【Xiaodou whole sugar (Cx)】
Redis GEO It is mainly used to store geographic information , And operate the stored information , This function is available in Redis 3.2 Version added .
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. Store the location name and latitude and longitude in the location collection 2. Take the latitude and longitude from the location set according to the given location name 3. Calculate the straight-line distance between two positions 4. According to the given position or longitude and latitude , Find other positions within the specified radius 5. Get the GEOHASH value */
// Store the location name and latitude and longitude in the location collection
// return TRUE Means new ,FALSE Update
public async Task<Task<bool>> AddGeo(string name,double longitude, double latitude,string key)
{
return _database.GeoAddAsync(key, longitude, latitude,name);
}
/// <summary>
/// Delete
/// </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);
}
// Get the coordinates of the specified location
public async Task<Task<GeoPosition?>> GEOPOS(string name,string key)
{
return _database.GeoPositionAsync(key, name);
}
/// <summary>
///
/// Calculate the straight-line distance between two points ,GeoUnit Designated units
/// </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>
/// Obtain the location within the specified radius according to longitude and latitude , And sort , Return distance
/// </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>
/// Calculation location hash , According to hash Deduce the longitude and latitude
/// </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;
}
}
}
边栏推荐
- Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
- Deflection lock / light lock / heavy lock lock is healthier. How to complete locking and unlocking
- Vite2兼容低版本chrome(如搜狗80),通过polyfills处理部分需求高版本的语法
- Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
- Lumiprobe dye hydrazide - BDP FL hydrazide solution
- 有趣插件汇总
- On the charm of code language
- 软工UML画图
- 【微服务~Nacos】Nacos之配置中心
- Peking University ACM problems 1001:exposition
猜你喜欢
随机推荐
Adobe Photoshop (PS) - script development - remove file bloated script
stacking集成模型预测回归问题
ICML2022 | 序列决策的效用理论
企业保护 API 安全迫在眉睫
毕业设计
凤凰架构——架构师的视角
三个火枪手
银行集体下架的智能投顾产品,为何成了“鸡肋”?
微信小程序怎么实现圆心进度条
19.04 distributor
MySQL高级篇3
ssh-server配置文件参数PermitRootLogin介绍
Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
ncat详细介绍(转载)
利用日志服务器输出各种apache的日志的TOPN
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
Go build server Foundation
B_QuRT_User_Guide(31)
有趣网站汇总
在线教育项目用户登录和注册








![翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]](/img/a8/6472e2051a295f5e42a88d64199517.png)
