private async Task EnsureOriginAsync(Trace t) { try { TraceOrigin traceOrigin; // try from cache if (!_cache.TryGetValue(t.Origin, out traceOrigin)) { _logger.LogDebug("Origin NOT in cache"); // try from db traceOrigin = await _originsRepository.GetByNameAsync(t.Origin); if (traceOrigin == null) { _logger.LogDebug("Origin NOT in db"); // save data in database TraceOrigin origin = new TraceOrigin() { Origin = t.Origin }; traceOrigin = _originsRepository.Add(origin); await _originsRepository.SaveAsync(); _logger.LogDebug("Origin ADDED in db"); } else { _logger.LogDebug("Origin in db"); } // set cache options var cacheEntryOptions = new MemoryCacheEntryOptions() // keep in cache for this time, reset time if accessed .SetSlidingExpiration(TimeSpan.FromSeconds(5)); // save data in cache _cache.Set(traceOrigin.Origin, traceOrigin, cacheEntryOptions); _logger.LogDebug("Origin ADDED in cache"); } else { _logger.LogDebug("Origin in cache"); } } catch (Exception ex) { _logger.LogError(ex.ToString()); } }
public TraceOrigin Update(TraceOrigin origin) { var item = (from o in _dataContext.TraceOrigins where o.OriginId == origin.OriginId select o).SingleOrDefault(); if (item == null) { return(null); } else { item.Origin = origin.Origin; _dataContext.Update(item); return(item); } }
public TraceOrigin Add(TraceOrigin origin) { _dataContext.Add <TraceOrigin>(origin); return(origin); }