I have code to add item to cache:
public static void AddTask(string name, string value,int seconds)
{
_onCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, value, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, _onCacheRemove);
}
For example, seconds equals 120. After 20 seconds I want to set expiration time again 120 seconds. How can I do this?
PS. A can’t delete this item and add again.
When you add the item to the cache – you can provide a callback that is to be invoked when the runtime is about to remove the item from the cache – rather than your current one which gets called when it’s already been removed.
In that callback, you can cancel the remove process and extend the entry’s lifetime.
See this overload of insert and this documentation about the CacheItemUpdateCallback delegate
Although you’re already handling the removed event – this ‘updated’ event is basically the same thing – it just gives you the ability to cancel the expiration if need be.