/// <summary> /// Flushes all of the items in the cache. /// </summary> /// <param name="values">The collection containing the values to flush.</param> void Flush(IEnumerable <KeyValuePair <CacheKey, int> > values) { foreach (var v in values) { // Only execute the query when the value is non-zero (it is possible to get a zero value when you let your // counters decrement) if (v.Value != 0) { var value = new ObjectEventAmount <TObjectID, TEventID>(v.Key.ObjectID, v.Key.EventID, v.Value); WriteValue(value); } } }
/// <summary> /// Writes a single value to the database. /// </summary> /// <param name="value">The <see cref="ObjectEventAmount{T,U}"/>.</param> void WriteValue(ObjectEventAmount <TObjectID, TEventID> value) { try { _query.Execute(value); } catch (Exception ex) { const string errmsg = "Failed to execute EventCounter query on `{0}` (object: {1}, event: {2})." + " Going to drop the value (value: {3}) instead of retrying. There is a good chance that the exception was caused from" + " trying to insert a record for a foreign key that no longer exists, and a much smaller chance that we tried" + " to insert a record for a foreign key that does not yet exist. Exception: {4}"; if (log.IsWarnEnabled) { log.WarnFormat(errmsg, this, value.ObjectID, value.EventID, value.Amount, ex); } } }
/// <summary> /// When overridden in the derived class, sets the database parameters values <paramref name="p"/> /// based on the values specified in the given <paramref name="item"/> parameter. /// </summary> /// <param name="p">Collection of database parameters to set the values for.</param> /// <param name="item">The value or object/struct containing the values used to execute the query.</param> protected override void SetParameters(DbParameterValues p, ObjectEventAmount <TObjectID, TEventID> item) { p[_objectParameterName] = item.ObjectID; p[_eventParameterName] = item.EventID; p[_counterParameterName] = item.Amount; }
/// <summary> /// Writes a single counter value to the database. /// </summary> /// <param name="source">The source object to increment the counter for.</param> /// <param name="e">The event to increment.</param> /// <param name="amount">The amount to increment.</param> void WriteValue(TObjectID source, TEventID e, int amount) { var value = new ObjectEventAmount <TObjectID, TEventID>(source, e, amount); WriteValue(value); }