Home > Blockchain >  when will this PostEvictionCallback be called?
when will this PostEvictionCallback be called?

Time:11-11

This is my code:

public void IncrementValue(string key){

   int currentCount = _cache.Get<int>(key);

   var options = new MemoryCacheEntryOptions()
   {
       AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
   };

   options.RegisterPostEvictionCallback(UpdateDatabase);
   _cache.Set(key, currentCount   1, options);
}

And UpdateDatabase:

private void Updatedatabase(object key, object value, EvictionReason reason, object state)
{
    int currentCount = int.Parse(value.ToString());
    _repository.AddNew(key.ToString(), currentCount);
}

Imagine the (first)key is created at 13:35 PM, then I update the key's value a couple times until 13:54. When will the UpdateDatabase method be called ? at 14:45 or at 14:54 ?

Edit:

Current question: I can see that the expiration is overwritten each time and the AbsoluteExpirationRelativeToNow is being updated so it never expires, how can I fix this ?

CodePudding user response:

The UpdateDatabase is called every time the _cache.Set() function is called, this is because the value is replaced and this will trigger the PostEvictionCallback . The EvictionReason will have the value: Replaced.

  • Related