none
Can't make sliding expiration on MemoryCache work. Bug? RRS feed

  • Question

  • I have a test which adds an item to a MemoryCache with a sliding expiration of 1 second. The cache checks item expiration every 250 ms. The item is retrieved from the cache at approximately 500 ms intervals. This should result in the item not being evicted from the cache. However, after 1 second, the item is always removed from the cache. I don't think that should happen.

     

    private const string TestKey = "my key";
    private const string TestValue = "my test value";
    private const string TestCache = "testCache";
    
    [TestMethod]
    public void TestSlidingExpirationOnSystemDotRuntimeDotCachingDotMemoryCache()
    {
    	var memoryCache = new System.Runtime.Caching.MemoryCache(TestCache);
    
    	Stopwatch stopwatch = new Stopwatch();
    	stopwatch.Start();
          
    	memoryCache.Add(TestKey, TestValue, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromSeconds(1) });
    	Thread.Sleep(500);
    
    	Trace.WriteLine(stopwatch.Elapsed);
    	Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    	Thread.Sleep(500);
    
    	Trace.WriteLine(stopwatch.Elapsed);
    	// failure here, sliding expiration does not work, we're just over a second at this point
    	Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    	Thread.Sleep(500);
    
    	Trace.WriteLine(stopwatch.Elapsed);
    	Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    	Thread.Sleep(500);
    	
    	Trace.WriteLine(stopwatch.Elapsed);
    	Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    
    	Thread.Sleep(2000);
    	Trace.WriteLine(stopwatch.Elapsed);
    	Assert.IsNull(memoryCache.Get(TestKey));
    }
    

     

     

    This is in the app.config file:

     

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <system.runtime.caching>
      <memoryCache>
       <namedCaches>
        <add name="testCache"
           cacheMemoryLimitMegabytes="0"
           physicalMemoryLimitPercentage="0"
           pollingInterval="00:00:00.25" />
       </namedCaches>
      </memoryCache>
     </system.runtime.caching>
    </configuration>
    

     

     

    Am I missing something? Or is this a bug?

    Tuesday, February 15, 2011 9:30 AM

Answers

All replies

  • Hi Jochen Z,

    You can submit your suggestions to Microsoft Connect feedback portal  http://connect.microsoft.com, Microsoft engineers will evaluate them seriously,thanks.

     If this issue is urgent, please contact support at  http://support.microsoft.com.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Cookie Luo Friday, February 18, 2011 2:49 AM
    • Marked as answer by Cookie Luo Tuesday, February 22, 2011 2:29 AM
    Thursday, February 17, 2011 9:25 AM
  • It has been posted here.

    Please vote for it or let Microsoft know you can reproduce this (if you can of course and think that the test is valid).

    • Marked as answer by Cookie Luo Tuesday, February 22, 2011 2:29 AM
    Thursday, February 17, 2011 10:09 AM
  • I also reproduced the same bug. Seems that the sliding expiration must be greater than 1 seconds. If the same example is executed with sliding expiration and changing the other values according all work as expected.

    See this new test with 2 seconds.

    		private const string TestKey = "my key";
    		private const string TestValue = "my test value";
    		private const string TestCache = "testCache";
    
    		[TestMethod]
    		public void TestSlidingExpirationOnSystemDotRuntimeDotCachingDotMemoryCache()
    		{
    			var memoryCache = new MemoryCache(TestCache);
    
    			Stopwatch stopwatch = new Stopwatch();
    			stopwatch.Start();
    
    			memoryCache.Add(TestKey, TestValue, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromSeconds(2) });
    			Thread.Sleep(1000);
    
    			Trace.WriteLine(stopwatch.Elapsed);
    			Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    			Thread.Sleep(1000);
    
    			Trace.WriteLine(stopwatch.Elapsed);
    			// failure here, sliding expiration does not work, we're just over a second at this point
    			Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    			Thread.Sleep(1000);
    
    			Trace.WriteLine(stopwatch.Elapsed);
    			Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    			Thread.Sleep(1000);
    
    			Trace.WriteLine(stopwatch.Elapsed);
    			Assert.AreEqual(TestValue, memoryCache.Get(TestKey));
    
    			Thread.Sleep(3000);
    			Trace.WriteLine(stopwatch.Elapsed);
    			Assert.IsNull(memoryCache.Get(TestKey));
    		}

    Tuesday, August 21, 2012 8:55 AM