Caching: Sliding Expiration Policy

posted Jun 27, 2018, 11:13 AM by Travis Croxford   [ updated Jul 5, 2018, 1:18 PM by Eric Patrick ]
Potential breaking change; If you have a private plugin/project (not included in the qbo.Core solution), and it leverages caching, you will need to add a new reference to System.Runtime.Caching.

MemoryCache can now have a policy applied to it when an object is being added.

Code example:

CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromHours(2) };
qbo.Application.Caching.MemoryCache pacerCache = new qbo.Application.Caching.MemoryCache(new Application.Configuration.Cache());
pacerCache.Set(credentialKey, pacerCreds, policy);

This allows for a cached item to remain in cache for 2 hours, until it's accessed again then the 2 hour timer starts over. After 2 hours without being accessed the item will fall out of cache.

Note that qbo.Application.Tests/CacheManagerTests.cs has been added to qbo.Application.Tests, which validates the behavior of CacheManager_SlidingExpiration().