本文整理汇总了C#中CacheStorage.Add方法的典型用法代码示例。如果您正苦于以下问题:C# CacheStorage.Add方法的具体用法?C# CacheStorage.Add怎么用?C# CacheStorage.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CacheStorage
的用法示例。
在下文中一共展示了CacheStorage.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReturnsRightValueForExistingKey
public void ReturnsRightValueForExistingKey()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
cache.Add("2", 2);
Assert.AreEqual(2, cache.Get("2"));
}
示例2: RaisesExpiringEventWithCorrectEventArgsWhenItemExpires
public void RaisesExpiringEventWithCorrectEventArgsWhenItemExpires()
{
var key = "1";
var expirationPolicy = new SlidingExpirationPolicy(TimeSpan.FromMilliseconds(250));
var value = 1;
var evKey = (string)null;
var evExpirationPolicy = default(ExpirationPolicy);
var evValue = 0;
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Expiring += (sender, e) =>
{
evKey = e.Key;
evExpirationPolicy = e.ExpirationPolicy;
evValue = e.Value;
};
cache.Add(key, value, expirationPolicy);
ThreadHelper.Sleep(750);
Assert.AreEqual(key, evKey);
Assert.AreEqual(expirationPolicy, evExpirationPolicy);
Assert.AreEqual(value, evValue);
}
示例3: AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode
public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode()
{
var cache = new CacheStorage<string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(250)));
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Add("1", 1);
Assert.IsTrue(cache.Contains("1"));
ThreadHelper.Sleep(750);
Assert.IsFalse(cache.Contains("1"));
}
示例4: AutomaticallyRemovesExpiredItems
public void AutomaticallyRemovesExpiredItems()
{
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));
Assert.IsTrue(cache.Contains("1"));
ThreadHelper.Sleep(750);
Assert.IsFalse(cache.Contains("1"));
}
示例5: IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy
public void IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy()
{
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));
Assert.IsTrue(cache.Contains("1"));
ThreadHelper.Sleep(750);
Assert.IsFalse(cache.Contains("1"));
}
示例6: DisposesItemOnRemoveWhenDisposingEnabled
public void DisposesItemOnRemoveWhenDisposingEnabled()
{
var disposable = new CustomDisposable();
var cache = new CacheStorage<string, CustomDisposable>();
cache.DisposeValuesOnRemoval = true;
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250));
Assert.IsFalse(disposable.IsDiposed);
cache.Remove("disposable");
Assert.IsTrue(disposable.IsDiposed);
}
示例7: DoesNotDisposeItemsOnClearWhenDisposingNotEnabled
public void DoesNotDisposeItemsOnClearWhenDisposingNotEnabled()
{
var disposable = new CustomDisposable();
var cache = new CacheStorage<string, CustomDisposable>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250));
Assert.IsFalse(disposable.IsDiposed);
cache.Clear();
Assert.IsFalse(disposable.IsDiposed);
}
示例8: AddsNonExistingValue
public void AddsNonExistingValue()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
Assert.AreEqual(1, cache["1"]);
}
示例9: AddsNonExistingValueForTrueOverride
public void AddsNonExistingValueForTrueOverride()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
cache.Add("1", 2, true);
Assert.AreEqual(2, cache["1"]);
}
示例10: ThrowsArgumentNullExceptionForNullKey
public void ThrowsArgumentNullExceptionForNullKey()
{
var cache = new CacheStorage<string, int>();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.Add(null, 1));
}
示例11: ThrowsArgumentNullExceptionForNullValueIfNotAllowNullValues
public void ThrowsArgumentNullExceptionForNullValueIfNotAllowNullValues()
{
var cache = new CacheStorage<string, object>();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.Add(null, null));
}
示例12: AddsItemToCacheWithOverrideAndReturnsIt
public void AddsItemToCacheWithOverrideAndReturnsIt()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
var value = cache.GetFromCacheOrFetch("1", () => 2, true);
Assert.IsTrue(cache.Contains("1"));
Assert.AreEqual(2, cache["1"]);
Assert.AreEqual(2, value);
}
示例13: ReturnsCachedItem
public void ReturnsCachedItem()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
var value = cache.GetFromCacheOrFetch("1", () => 2);
Assert.IsTrue(cache.Contains("1"));
Assert.AreEqual(1, cache["1"]);
Assert.AreEqual(1, value);
}
示例14: ReturnsTrueForExistingKey
public void ReturnsTrueForExistingKey()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
cache.Add("2", 2);
Assert.IsTrue(cache.Contains("2"));
}
示例15: ItemStaysInCacheWhenExpiringEventIsCanceled
public void ItemStaysInCacheWhenExpiringEventIsCanceled()
{
var key = "1";
var value = 1;
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Expiring += (sender, e) =>
{
e.Cancel = true;
};
cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250));
ThreadHelper.Sleep(750);
Assert.IsTrue(cache.Contains(key));
}