public async Task InvokeAsync(HttpContext context) { string address = context.Connection.RemoteIpAddress.ToString(); IGeoIp geoIp = null; if (this.cache != null) { geoIp = this.cache.Get(address); if (geoIp == null) { geoIp = this.provider.Resolve(address); this.cache.Put(address, geoIp); } } else { geoIp = this.provider.Resolve(address); } if (!context.Items.ContainsKey("geoIp")) { context.Items.Add("geoIp", geoIp); } await this.next(context); }
public void Put(string address, IGeoIp geoIp) { if (this.cache.TryGetValue(address, out IGeoIp oldGeoIp)) { this.cache.AddOrUpdate(address, geoIp, (p, q) => geoIp); } else { if (this.maxSize.HasValue && (this.cache.Count + 1) > this.maxSize) { this.cache.Clear(); } this.cache.TryAdd(address, geoIp); } }