Uses IP to look up geolocation information and save it in MemoryCache for faster queries.
$ dotnet add package IPGeolocationMemoryCacheIPGeolocationMemoryCache is a lightweight and efficient library designed to resolve the geolocation of an IP address using free API services. It solves the challenge of repeated API calls for high-traffic applications by caching the resolved geolocation data in memory for 10 minutes (configurable).
By utilizing this library, you can reduce API request overhead and improve the performance of your application when dealing with frequent IP geolocation lookups. It is ideal for scenarios where geolocation data is critical but maintaining low latency is equally important.
IPGeolocationMemoryCache is available using nuget. To install IPGeolocationMemoryCache, run the following command in the Package Manager Console
Install-Package IPGeolocationMemoryCache
https://www.nuget.org/packages/IPGeolocationMemoryCache
// All result implement the IIPGeolocationResult
public interface IIPGeolocationResult
{
public string? Status { get; }
public DateTime? LastQuery { get; }
public string? Country { get; }
public string? CountryCode { get; }
public string? State { get; }
public string? City { get; }
public string? Zip { get; }
public dynamic? Latitude { get; }
public dynamic? Longitude { get; }
public string? Timezone { get; }
public string? ISP { get; }
public string? Org { get; }
public bool? Mobile { get; }
public string? Others { get; set;}
}
In Program.cs add MemoryCache ServiceCollection
builder.Services.AddMemoryCache();
Create new instance of IPGeolocationMemoryCache.IpGeoLocation() with MemoryCache
using IPGeolocationMemoryCache;
using Microsoft.Extensions.Caching.Memory;
public class IpLog
{
private readonly ILogger<IpLog> _logger;
private readonly IMemoryCache _cache;
public IpLog(ILogger<IpLog> log, IMemoryCache memoryCache)
{
_logger = log;
_cache = memoryCache;
}
public void LogIPInfo()
{
string Ip = "179.162.174.33";
string host = "somehost.com";
var ipGL = new IpGeoLocation(_cache);
var ipInfo = ipGL.GetIPGeolocationAsync(Ip).Result;
var content = @$"
Host: {host} |
{ipInfo.Country} -
{ipInfo.CountryCode} -
{ipInfo.State} -
{ipInfo.City} -
{ipInfo.ISP} -
{ipInfo.Org}";
_logger.LogInformation(content.Trim());
Console.WriteLine($"{content.Trim()}");
}
}
using Microsoft.Extensions.Caching.Memory;
// It is not mandatory to use the constructor with MemoryCache.
// But not using it may result in higher latency in API calls and there may be more queries than the free services allow.
public class IpInfo
{
public void GetIPGeo()
{
var _cache = new MemoryCache(new MemoryCacheOptions());
var ipGeo = new IpGeoLocation(_cache);
var lstIp = new List<string> { "179.162.174.33", "35.208.27.10", "18.217.72.66", "35.208.27.10", "179.162.174.33" };
var lstResult = new List<IIPGeolocationResult?>();
foreach (var item in lstIp)
{
lstResult.Add(ipGeo.GetIPGeolocationAsync(item).Result);
Task.Delay(3000);
}
Console.WriteLine($"1th coutry = {lstResult[0]?.Country} | 5th coutry = {lstResult[4]?.Country}");
Console.WriteLine($"2th coutry = {lstResult[2]?.Country} | 4th coutry = {lstResult[3]?.Country}");
}
}
Start using IPGeolocationMemoryCache today to enhance your application's geolocation capabilities while maintaining efficiency and performance!