cachingmemcachedappfabricazure-appfabric

Expiration Date Reset/Overriden on App Fabric Cache Provider


I am using

Microsoft.ApplicationServer.Caching.DataCache for caching

The problem is when I first add an item to the cache it preserves the time out but if I replace the existing item the cahce overrides the timeout to the default value. Here is the code I am using.

DataCache cache= new MyDataCahce();
// time out 30 secs
cache.Add("key",10, TimeSpan.FromMilliseconds(30000));
var temp = _cache.GetCacheItem("key");
temp.Timeout(); // minutes =  30 seconds which is correct

// Second time replace object at key
cache.Put("key",20)
var temp = _cache.GetCacheItem("key");
temp.Timeout(); // timeout reset to default and equals 10 minutes which is the default

Solution

  • What I ended up doing is getting the timeout of the existing item before I add new item to the Cache.

     DataCache cache= new MyDataCahce();
        // time out 30 secs at first
        cache.Add("key",10, TimeSpan.FromMilliseconds(30000));
        var temp = _cache.GetCacheItem("key");
        temp.Timeout(); // minutes =  30 seconds which is correct
    
    
        // Second time replace object at key may be after 10 seconds
        var temp = _cache.GetCacheItem("key");
    if(temp!=null) // it is not expired yet
    {
        var timeOut = temp.Timeout();  // less than 30 seconds should be about 20 seconds
        cache.Put("key",20, timeOut )
        var temp = _cache.GetCacheItem("key");
        temp.Timeout(); // timeout less than 30 seconds 
    }