private void RetryCacheEntries(object state) { _cacheCheckTimer.Change(Timeout.Infinite, Timeout.Infinite); IEnumerable <DataLogDatabaseResult> results = _cache?.RetryFromCache(); if (results?.Any() == true) { CacheOperationsRetried?.Invoke(this, new DataLogCacheEventArgs(results)); } _cacheCheckTimer.Change(_cacheCheckFrequency, _cacheCheckFrequency); }
/// <summary> /// Inserts data from the specified <see cref="LogTableRecord" /> into the table designated by the <see cref="LogTableDefinition" />. /// </summary> /// <param name="table">The <see cref="LogTableDefinition" />.</param> /// <param name="record">The <see cref="LogTableRecord" />.</param> /// <returns><c>true</c> if submission was successful, <c>false</c> otherwise.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="table" /> is null. /// <para>or</para> /// <paramref name="record" /> is null. /// </exception> public bool Insert(LogTableDefinition table, LogTableRecord record) { if (table == null) { throw new ArgumentNullException(nameof(table)); } if (record == null) { throw new ArgumentNullException(nameof(record)); } LogTrace($"INSERT request received for {table.Name} (primary key = {record[table.PrimaryKey]})"); DataLogDatabaseResult result = _writer.Insert(table, record); bool insertSuccessful = result.Success; // If the operation failed, try the alternate (if one exists) if (!insertSuccessful && _alternateWriter != null) { LogTrace($"INSERT failed. Attempting insert to alternate database."); DataLogDatabaseResult alternateResult = _alternateWriter.Insert(table, record); insertSuccessful = alternateResult.Success; } // If the operation hasn't succeeded, check to see if we should add this to the cache. if (!insertSuccessful && _cache != null) { if (result.Error.Contains("Violation of PRIMARY KEY constraint", StringComparison.OrdinalIgnoreCase)) { // Bypass the cache for primary key violations result.Retries = -1; CacheOperationsRetried?.Invoke(this, new DataLogCacheEventArgs(new[] { result })); } else { _cache.Add(table, record, true); } } return(insertSuccessful); }