public static TOut GetLifeTimeCache <TOut>(string cacheName, string key) { // define auto refreshing cache var lifetimeCache = new AutoRefreshingCache <object>(cacheName); return(lifetimeCache.Get <TOut>(key)); }
public void DontCacheExceptions() { var i = 0; var cache = new AutoRefreshingCache <string>((k) => { i++; if (i == 1) { throw new Exception(); } return("test"); }, 1); try { var first = cache.Get("key"); } catch (Exception) { } var second = cache.Get("key"); Assert.IsTrue(second == "test"); }
public static void Test() { // define auto refreshing cache var refreshableCache = new AutoRefreshingCache <string>(expireAfter: 10, refreshAfter: 8, cacheName: "shortTimeCache"); // add key/value data to cache refreshableCache.Inject("key", "value"); // get count of expired cache by key elements int expiredCacheCount = refreshableCache.CountExpiredElements(new[] { "key1", "key2", "key3", "key4" }); // get or update a key in cache, the update operate when occurred that cache was expired, else get old value. var value = refreshableCache.Get("key", () => "new value"); // define lifetime cache var lifetimeCache = new AutoRefreshingCache <object>(cacheName: "lifetimeCache"); lifetimeCache.Inject("test", 123456); int testValue = lifetimeCache.Get <int>("test"); // define rate limiter for decide can call a method too more or not? var rateLimiter = new RateLimiter(maxTries: 100, inPeriod: 120, cacheName: "rateLimiterCache"); bool canProc = rateLimiter.CanProceed("method name or a key"); }
public void OnlyOneInParallel_LongGetOperation() { var i = 0; var cache = new AutoRefreshingCache <string>((k) => { i++; Thread.Sleep(1000); return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 3), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void OnlyOneInParallel() { var i = 0; var cache = new AutoRefreshingCache <string>((k) => { i++; return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 5), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void TestNoStaleNoSerialize() { // Set up the mocks string testKey = "testKey"; string refreshserResult = "testValue"; string testValue = null; var tracing = new Mock <ITrace>(); var cache = new Mock <ICache>(); // The cache.AddItem has to act like a cache and save the value // that was passed in. Since the refresher is in ServeStale=false // mode cache items will be saved with a TTL. cache.Setup(m => m.AddItem( It.IsAny <string>(), It.IsAny <TimeSpan>(), It.IsAny <object>())) .Callback <string, TimeSpan, object>((k, ttl, d) => { // set testValue to the passed in value testValue = d as string; }); // The value of testValue was set in the cache.addItem call cache.Setup(m => m.GetItem( It.IsAny <string>())) .Returns <string>((k) => { return(testValue); }); // Set up the AutoRefreshingCache Func <string, Task <object> > cacheRefreshser = (key) => Task.FromResult <object>(refreshserResult); var refreshTimeout = TimeSpan.FromMilliseconds(1000); var defaultTTL = TimeSpan.FromMilliseconds(500); var autoRefreshingCache = new AutoRefreshingCache( false, // don't serve stale false, // don't serialize refresher calls refreshTimeout, defaultTTL, cacheRefreshser, cache.Object, tracing.Object); var testResult = autoRefreshingCache.GetItem("testKey"); cache.Verify(m => m.AddItem(testKey, defaultTTL, testValue), "test item was added to the cache"); cache.Verify(m => m.GetItem(testKey), "testkey was used to access the test value in the cache"); Assert.AreEqual(testValue, testResult, "was able to retrieve the correct test value"); }
public void ExceptionInAsyinc() { var i = 0; var exceptionThrown = false; var cache = new AutoRefreshingCache <string>((k) => { i++; if (i == 2) { exceptionThrown = true; throw new Exception(); } return("test"); }, 1.0 / 60); var first = cache.Get("key"); Thread.Sleep(1500); var second = cache.Get("key"); Assert.IsTrue(exceptionThrown); Assert.IsTrue(second == "test"); }
public void CacheIsAutoRefreshing() { var i = 0; var cache = new AutoRefreshingCache <string>((k) => { i++; if (i == 2) { Thread.Sleep(250); } return("test"); }, 1.0 / 60); var first = cache.Get("key"); while (i < 2) { var duration = Duration(() => { var second = cache.Get("key"); }); Assert.IsTrue(duration.TotalMilliseconds < 200); } }
public static string GetCache(string cacheName, string key) { var arc = new AutoRefreshingCache <string>(cacheName, ExpireAfter, RefreshAfter); return(arc.Get(key, () => key + _expireCounter++)); }