static void Main(string[] args) { using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379")) { IDatabase db = redis.GetDatabase(); //向Redis数据库中插入geo类型数据 //下面插入的兴趣点是商店, //最后一个参数中的”1”、”2”是点的主键,点的名称、地址、电话等存到其他表中 //db.GeoAdd("ShopsGeo", new GeoEntry(116.34039, 39.94218, "1")); //db.GeoAdd("ShopsGeo", new GeoEntry(116.340934, 39.942221, "2")); //db.GeoAdd("ShopsGeo", new GeoEntry(116.341082, 39.941025, "3")); //db.GeoAdd("ShopsGeo", new GeoEntry(116.340848, 39.937758, "4")); //db.GeoAdd("ShopsGeo", new GeoEntry(116.342982, 39.937325, "5")); //db.GeoAdd("ShopsGeo", new GeoEntry(116.340866, 39.936827, "6")); //上面的插入数据的方式需要连接Redis服务器多次,下面只需要一次,注意这样可以提高性能 GeoEntry[] geos = new GeoEntry[] { new GeoEntry(116.34039, 39.94218, "1"), new GeoEntry(116.340934, 39.942221, "2"), new GeoEntry(116.341082, 39.941025, "3"), new GeoEntry(116.340848, 39.937758, "4"), new GeoEntry(116.342982, 39.937325, "5"), new GeoEntry(116.340866, 39.936827, "6") }; //计算两点之间的距离 //查询key为"ShopsGeo"中“1”点和“2”点的距离 double?dist = db.GeoDistance("ShopsGeo", "1", "2", GeoUnit.Kilometers); //查询某个点周围一定半径的兴趣点 //以“1”点为中心,半径200米中的兴趣点 //当然也可以以某个经纬度为中心点 //GeoRadiusResult[] items = db.GeoRadius("ShopsGeo",34.59669,119.22295, 200, GeoUnit.Meters); GeoRadiusResult[] items = db.GeoRadius("ShopsGeo", "1", 200, GeoUnit.Meters); foreach (var item in items) { Console.WriteLine($"距离{item.Member}点,有{item.Distance}米"); } Console.ReadKey(); } }
/// <summary> /// Adds the specified geospatial members (latitude, longitude, object) to the specified key. /// </summary> /// <typeparam name="T">The member type</typeparam> /// <param name="key">The redis key.</param> /// <param name="members">The members to add.</param> /// <param name="tags">The tags to relate to the members.</param> /// <returns>The number of elements added to the sorted set, not including elements already existing.</returns> public int GeoAdd <T>(string key, GeoMember <T>[] members, string[] tags = null) { var db = RedisConnection.GetDatabase(); var values = new GeoEntry[members.Length]; for (int i = 0; i < values.Length; i++) { var member = members[i]; values[i] = new GeoEntry(member.Position.Longitude, member.Position.Latitude, Serializer.Serialize(member.Value)); } int result = (int)db.GeoAdd(key, values); // Relate the tags (if any) if (tags != null && tags.Length > 0) { foreach (var member in members) { _cacheProvider.AddTagsToSetMember(key, member.Value, tags); } } return(result); }
public Task <bool> GeoAddAsync(RedisKey key, GeoEntry value, CommandFlags flags = CommandFlags.None) => Inner.GeoAddAsync(ToInner(key), value, flags);
public bool GeoAdd(RedisKey key, GeoEntry value, CommandFlags flags = CommandFlags.None) { return(Inner.GeoAdd(ToInner(key), value, flags)); }
/// <summary> /// 增加 GeoHash 批处理 /// </summary> /// <example> /// var batch = database.CreateBatch(); /// set.BatchAdd(batch,entry); /// batch.Execute(); /// </example> /// <param name="batch"></param> /// <param name="entry"></param> /// <returns></returns> Task IBatchGeoHashSet <TKey> .BatchAdd(IBatch batch, GeoEntry entry) { return(batch.GeoAddAsync(SetKey, entry)); }
/// <summary> /// Add the specified member to the set stored at key. Specified members that are /// already a member of this set are ignored. If key does not exist, a new set is /// created before adding the specified members. /// </summary> /// <param name="entry">The geo value to store.</param> /// <returns></returns> async Task <bool> IAsyncGeoHashSet <TKey> .AddAsync(GeoEntry entry) { return(await Database.GeoAddAsync(SetKey, entry)); }
/// <summary> /// Add the specified member to the set stored at key. /// Specified members that are already a member of this set are ignored. /// If key does not exist, a new set is created before adding the specified members. /// </summary> /// <param name="entry">The geo value to store.</param> /// <returns></returns> public bool Add(GeoEntry entry) { return(Database.GeoAdd(SetKey, entry)); }
public Task <bool> GeoAddAsync(RedisKey key, GeoEntry value, CommandFlags flags = CommandFlags.None) { return(this.db.GeoAddAsync(key, value, flags)); }
bool IDatabase.GeoAdd(RedisKey key, GeoEntry value, CommandFlags flags) => Multiplexer.Wait(((IDatabaseAsync)this).GeoAddAsync(key, value, flags));
/// <summary> /// 添加一个 地理信息 /// </summary> /// <param name="key">键</param> /// <param name="geo">地理信息</param> /// <returns></returns> public bool GeoAdd(string key, GeoEntry geo) { return(_base.GeoAdd(key, geo)); }