.netcore redis GEO类型

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);
        }

        /// 
        /// 删除
        /// 
        /// 
        /// 
        /// 
        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);
        }

        /// 
        /// 
        /// 计算两点间的直线距离,GeoUnit 指定单位
        /// 
        /// 
        /// 
        /// 
        /// 

        public async Task<Task<double?>> GeoDistance(string position1,string position2,string key)
        {
            return _database.GeoDistanceAsync(key,position1 ,position2,GeoUnit.Kilometers);
        }


        /// 
        /// 根据经纬度获取指定半径内的地点,并排序,返回距离
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        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);
        }

        
        /// 
        /// 计算位置的hash , 可以根据hash反推出来经纬度
        /// 
        /// 
        /// 
        /// 
        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/
        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/
        [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//5
        [HttpDelete]
        public async Task<bool> Delete([FromQuery] string key, [FromQuery] string name)
        {
            return await _redisGeo.DeleteGeo(name,key).Result;
        }


    }
}

你可能感兴趣的:(Redis,.netCore,.netcore,redis,java)