public void CacheLockNeverExpiresTest() { Assert.AreEqual(TimeSpan.FromMilliseconds(-1), CacheSettings.DefaultLockTimeout); var cache = new InstanceCache <string>(); var startTime = DateTime.Now; var value = cache.Get(CacheDuration, () => { var result = "Initial"; var thread = new Thread(new ThreadStart(() => { try { result = cache.Get(CacheDuration, () => { return("Hello"); }); } catch (ThreadAbortException) { Thread.ResetAbort(); } })); thread.Start(); thread.Join(TwoSeconds); thread.Abort(); return(result); }); var endTime = DateTime.Now; var diff = endTime.Subtract(startTime); Assert.AreEqual("Initial", value); Assert.IsTrue(diff.TotalSeconds >= 1.8 && diff.TotalSeconds < 3); }
public void GetCachedValueTest() { var cache = new InstanceCache <string>(); var firstValue = "Hello"; var value = cache.Get(CacheDuration, () => firstValue); value = cache.Get(CacheDuration, () => "World"); Assert.AreEqual("Hello", value); }
public void GetValueTest() { var cache = new InstanceCache <string>(); var value = cache.Get(CacheDuration, () => { return("Hello"); }); Assert.AreEqual("Hello", value); }
public void TestCache() { var cache = new InstanceCache(); cache.Clear(); var summary = new HtmlSummary(HttpStatusCode.OK, "http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4", RawHtml, "text/html"); cache.Set("first", summary); cache.Set("second", summary); var first = cache.Get<HtmlSummary>("first"); var second = cache.Get<HtmlSummary>("second"); first.Should().NotBeNull(); second.Should().NotBeNull(); first.CreatedAt.Should().Be(second.CreatedAt); cache.Unset("first"); var none = cache.Get<HtmlSummary>("first"); none.Should().BeNull(); }
public void ClearCacheTest() { // set up cache with an initial value var cache = new InstanceCache <string>(); var cachedValue = "Initial value"; // get value from cache and check it matches initial var value = cache.Get(CacheDuration, () => cachedValue); Assert.AreEqual("Initial value", value); // clear the cache cache.Clear(); // change the cached value cachedValue = "Changed value"; // check that it returns the changed value value = cache.Get(CacheDuration, () => cachedValue); Assert.AreEqual("Changed value", value); }
public void CacheLockExpiresOnTimeTest() { var cache = new InstanceCache <string>(TwoSeconds); var startTime = DateTime.Now; var value = cache.Get(CacheDuration, () => { var result = default(string); var thread = new Thread(new ThreadStart(() => { result = cache.Get(CacheDuration, () => { return("Hello"); }); })); thread.Start(); thread.Join(); return(result); }); var endTime = DateTime.Now; var diff = endTime.Subtract(startTime); Assert.IsNull(value); Assert.IsTrue(diff.TotalSeconds >= 1.8 && diff.TotalSeconds < 3); }
/// <summary> /// Gets an instance for a bindings type and name. /// </summary> /// <param name="type">The type to get an instance for.</param> /// <param name="name">The name to get an instance for.</param> /// <returns>An <c>object</c>, or <c>null</c>.</returns> internal object GetInstance(Type type, string name) { return(name != null? // InstanceCache.Get(type, name) : // InstanceCache.Get(type)); }
private void Start() { gamePresenter = InstanceCache.Get <GamePresenter>(); }