Bug 18213 - System.Runtime.Caching.MemoryCache doesn't honor SlidingExpiration
Summary: System.Runtime.Caching.MemoryCache doesn't honor SlidingExpiration
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Web ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-03-06 07:44 UTC by Craig Minihan
Modified: 2014-04-28 17:27 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Craig Minihan 2014-03-06 07:44:16 UTC
MemoryCache items which are set to SlidingExpiration should not expire if accessed before their timeout. Calling Get(KEY) should update their expiry value, however this value is not updated and the items expire based on NOW + TIMESPAN when they were fist added. This is a test case which you are drop into MemoryCacheTest.cs:

[Test]
public void TestCacheSliding ()
{	
	var config = new NameValueCollection ();
	config["cacheMemoryLimitMegabytes"] = 0.ToString ();
	config["physicalMemoryLimitPercentage"] = 100.ToString ();
	config["__MonoEmulateOneCPU"] = true.ToString ();

	// it appears that pollingInterval does nothing, so we set the Mono timer as well
	config["pollingInterval"] = new TimeSpan (0, 0, 2).ToString ();
	config["__MonoTimerPeriod"] = 2.ToString ();
	
	using (var mc = new MemoryCache ("TestCacheSliding",  config)) {	
		Assert.AreEqual (0, mc.GetCount (), "#CSL1");

		var cip = new CacheItemPolicy();
		cip.SlidingExpiration = new TimeSpan(0, 0, 2);				
		mc.Add("slidingtest", "42", cip);

		for (int i = 0; i < 40; i++) {
			global::System.Threading.Thread.Sleep(250);
			
			var item = mc.Get("slidingtest");
			Assert.AreNotEqual (null, item, "#CSL2-" + i);
		}			
	}
}

The item should not expire in the loop, according to MS: "A span of time within which a cache entry must be accessed before the cache entry is evicted from the cache" - I can't see any code in Get() where sliding expired items are updated, so they always expire based on the initial expiresAt value.
Comment 1 Craig Minihan 2014-03-07 07:55:24 UTC
I'll attempt a fix this this one shortly
Comment 2 Craig Minihan 2014-03-26 20:44:28 UTC
Fix made in pull #971
Comment 3 Alexis Christoforides 2014-04-28 17:27:44 UTC
Pull #971 was merged on master (+ trivial test timer adjustments), commit 2d9b230c73adb68013e8757ddb09ddd20fc359ef.