public IPDetailsModel GetById(long id) { IPDetailsModel result = _repository.GetById(id); //to do: _cache.Set(key: result.IP, value: result, options: _memoryCacheEntryOptions); return(result); }
public void Add(string ip, IPDetailsModel item, int?expirationInMinutes = 1) { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(expirationInMinutes.Value); Cache.Add(ip, item, policy); }
public async Task <IPDetailsModel> GetByIdAsync(long id) { IPDetailsModel result = await _repository.GetByIdAsync(id); //to do: _cache.Set(key: result.IP, value: result, options: _memoryCacheEntryOptions); return(result); }
public async Task <IPDetailsModel> AddAsync(IPDetailsModel entity) { IPDetailsModel result = await _repository.AddAsync(entity); _cache.Set(key: entity.IP, value: result, options: _memoryCacheEntryOptions); return(result); }
/// <summary> /// Processes a part of the batch. /// </summary> /// <param name="currentJob">The current job as described by it's model.</param> /// <param name="ipDetails">The part of the batch that will be processed.</param> private void BatchToProcess(ref JobModel currentJob, IEnumerable <IPDetailsToUpdateDTO> ipDetails) { int successfulUpdates = 0; foreach (IPDetailsToUpdateDTO det in ipDetails) { try { IPDetailsModel ipDetailsModel = _ipModelsStored.Where(li => li.IP == det.IpAddress).FirstOrDefault(); ipDetailsModel.City = string.IsNullOrWhiteSpace(det.City) ? ipDetailsModel.City : det.City; ipDetailsModel.Continent = string.IsNullOrWhiteSpace(det.Continent) ? ipDetailsModel.Continent : det.Continent; ipDetailsModel.Country = string.IsNullOrWhiteSpace(det.Country) ? ipDetailsModel.Country : det.Country; ipDetailsModel.Latitude = det.Latitude.GetValueOrDefault(ipDetailsModel.Latitude); ipDetailsModel.Longitude = det.Longitude.GetValueOrDefault(ipDetailsModel.Longitude); _ipDetailsRepository.UpdateAsync(ipDetailsModel).GetAwaiter().GetResult(); successfulUpdates++; } catch (Exception ex) { _logger.LogError($"Job: {currentJob.JobKey} failed to update detail: {det.IpAddress}. Exception message: {ex.Message}"); } } currentJob.ItemsLeft -= ipDetails.Count(); currentJob.ItemsDone += ipDetails.Count(); currentJob.ItemsSucceeded += successfulUpdates; _jobRepository.UpdateAsync(currentJob).GetAwaiter().GetResult(); }
public static void ValidateDetail(this IPDetailsModel detail) { if (string.IsNullOrEmpty(detail.IP)) { throw new MissingFieldException("IP cannot be null or empty"); } //TODO: anything alse to ckeck? }
public IPDetailsModel GetByIPAddress(string ip) { IPDetailsModel result; try { _cacheLock.Wait(); if (!_cache.TryGetValue(key: ip, out result)) { _logger.LogDebug($"Did not find ip: {ip} in cache."); result = _repository.GetByIPAddress(ip); if (result != null) { _cache.Set(key: result.IP, value: result, options: _memoryCacheEntryOptions); _logger.LogDebug($"Found ip: {ip} in database"); return(result); } _logger.LogDebug($"Did not find ip: {ip} in database. Will try to get it via the IPStack API."); IPDetails dets = _IIPInfoProvider.GetDetails(ip); result = new IPDetailsModel() { City = dets.City, Continent = dets.Continent, Country = dets.Country, IP = ip, Latitude = dets.Latitude, Longitude = dets.Longitude, DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow }; Task <IPDetailsModel> ipDetailsModelAddTask = new Task <IPDetailsModel>(() => { return(AddAsync(result).Result); }); ipDetailsModelAddTask.RunSynchronously(); return(ipDetailsModelAddTask.Result); } } catch (Exception ex) { _logger.LogError($"Could not get ip: {ip} in any of the three ways possible (IPStack API, Caching or Repository). Exception message: {ex.Message}"); throw ex; } finally { _cacheLock.Release(); } _logger.LogDebug($"Found ip: {ip} in memory cache."); return(result); }
private IPDetailsModel CopyToModel(IIpDetails ipDetails, string ip) { var model = new IPDetailsModel(); model.Ip = ip; model.City = ipDetails.City; model.Continent = ipDetails.Continent; model.Country = ipDetails.Country; model.Latitude = ipDetails.Latitude; model.Longitude = ipDetails.Longitude; return(model); }
public static IPDetailsModel ToDetailsModel(this IPDetailsDTO dbObj) { IPDetailsModel details = new IPDetailsModel { IP = dbObj.IP, City = dbObj.City, Continent = dbObj.Continent, Country = dbObj.Country, Latitude = dbObj.Latitude, Longitude = dbObj.Longitude }; return(details); }
public void AddIpDetails(string ip, IPDetailsModel item) { var dbItem = new IPDetail(); dbItem.Ip = ip; dbItem.City = item.City; dbItem.Country = item.Country; dbItem.Continent = item.Continent; dbItem.Latitude = item.Latitude.GetValueOrDefault().ToString(); dbItem.Longitude = item.Longitude.GetValueOrDefault().ToString(); _context.IPDetails.Add(dbItem); _context.SaveChanges(); }
public async Task <IPDetailsModel> GetByIPAddressAsync(string ip) { IPDetailsModel result; try { _cacheLock.Wait(); if (!_cache.TryGetValue(key: ip, out result)) { result = _repository.GetByIPAddress(ip); if (result != null) { _cache.Set(key: result.IP, value: result, options: _memoryCacheEntryOptions); return(result); } //could also go for the IPInfoProvider class and run the async method here, or break the pdf's contract //and add a GetDetailsAsync method to the contract. IPDetails detsFromIpInfoProvider = _IIPInfoProvider.GetDetails(ip); result = new IPDetailsModel() { City = detsFromIpInfoProvider.City, Continent = detsFromIpInfoProvider.Continent, Country = detsFromIpInfoProvider.Country, IP = ip, Latitude = detsFromIpInfoProvider.Latitude, Longitude = detsFromIpInfoProvider.Longitude, DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow }; return(await AddAsync(result)); } } catch (Exception ex) { _logger.LogError($"Could not get ip: {ip} in any of the three ways possible (IPStack API, Caching or Repository). Exception message: {ex.Message}"); throw ex; } finally { _cacheLock.Release(); } return(result); }
public IPDetailsModel GetIpDetails(string ip) { var dbItem = _context.IPDetails.FirstOrDefault(n => n.Ip == ip); IPDetailsModel ipDetails = null; if (dbItem != null) { ipDetails = new IPDetailsModel(); ipDetails.Ip = ip; ipDetails.City = dbItem.City; ipDetails.Country = dbItem.Country; ipDetails.Continent = dbItem.Continent; ipDetails.Latitude = Convert.ToDouble(dbItem.Latitude == null? "0": dbItem.Latitude); ipDetails.Longitude = Convert.ToDouble(dbItem.Longitude == null ? "0" : dbItem.Longitude); } return(ipDetails); }
public static IPDetailsDTO ToDetailsDTO(this IPDetailsModel model) { return(model.ToDetailsDTO(model.IP)); }
public async Task DeleteAsync(IPDetailsModel entity) { await _efRepository.DeleteAsync(entity); }
public async Task <IPDetailsModel> AddAsync(IPDetailsModel entity) { return(await _efRepository.AddAsync(entity)); }
public async Task UpdateAsync(IPDetailsModel entity) { await _repository.UpdateAsync(entity); _cache.Set(key: entity.IP, value: entity, options: _memoryCacheEntryOptions); }
public async Task DeleteAsync(IPDetailsModel entity) { _cache.Remove(key: entity.IP); await _repository.DeleteAsync(entity); }