using System; namespace Quasar.Client.IpGeoLocation { /// /// Factory to retrieve and cache the last IP geolocation information for minutes. /// public static class GeoInformationFactory { /// /// Retriever used to get geolocation information about the WAN IP address. /// private static readonly GeoInformationRetriever Retriever = new GeoInformationRetriever(); /// /// Used to cache the latest IP geolocation information. /// private static GeoInformation _geoInformation; /// /// Time of the last successful location retrieval. /// private static DateTime _lastSuccessfulLocation = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// /// The minimum amount of minutes a successful IP geolocation retrieval is valid. /// private const int MINIMUM_VALID_TIME = 60 * 12; /// /// Gets the IP geolocation information, either cached or freshly retrieved if more than minutes have passed. /// /// The latest IP geolocation information. public static GeoInformation GetGeoInformation() { var passedTime = new TimeSpan(DateTime.UtcNow.Ticks - _lastSuccessfulLocation.Ticks); if (_geoInformation == null || passedTime.TotalMinutes > MINIMUM_VALID_TIME) { _geoInformation = Retriever.Retrieve(); _lastSuccessfulLocation = DateTime.UtcNow; } return _geoInformation; } } }