本文整理汇总了C#中Cache.Put方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.Put方法的具体用法?C# Cache.Put怎么用?C# Cache.Put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.Put方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCache
/// <exception cref="System.Exception"></exception>
public virtual void TestCache()
{
int retainCount = 1;
Cache cache = new Cache<string, Document>(retainCount);
IDictionary<string, object> props = new Dictionary<string, object>();
props.Put("foo", "bar");
Document doc1 = CreateDocumentWithProperties(database, props);
cache.Put(doc1.GetId(), doc1);
IDictionary<string, object> props2 = new Dictionary<string, object>();
props2.Put("foo2", "bar2");
Document doc2 = CreateDocumentWithProperties(database, props2);
cache.Put(doc2.GetId(), doc2);
NUnit.Framework.Assert.IsNotNull(cache.Get(doc1.GetId()));
NUnit.Framework.Assert.IsNotNull(cache.Get(doc2.GetId()));
cache.Remove(doc1.GetId());
NUnit.Framework.Assert.IsNull(cache.Get(doc1.GetId()));
cache.Clear();
NUnit.Framework.Assert.IsNull(cache.Get(doc2.GetId()));
}
示例2: GetById_ItemInCache_DataStoreIsntAccessed
public void GetById_ItemInCache_DataStoreIsntAccessed()
{
//Arrange
var cache = new Cache();
cache.Put(123, "This is my data is from the cache");
var storeMock = new Mock<DataStore>() { CallBase = true};
storeMock.Object.Add(123, "This is my data is from the data store");
var repository = new Repository(storeMock.Object, cache);
//Act
var item = repository.GetById<string>(123);
//Assert
Assert.AreEqual("This is my data is from the cache", item);
storeMock.Verify(x => x.GetById<string>(It.IsAny<int>()), Times.Never);
}
示例3: GetById_ItemExpiredFromCache_DataStoreIsAccessed
public void GetById_ItemExpiredFromCache_DataStoreIsAccessed()
{
//Arrange
var cache = new Cache();
cache.Put(123, "This is my data is from the cache");
cache.ItemExperationInSeconds = 10;
var store = new DataStore();
store.Add(123, "This is my data is from the data store");
var repository = new Repository(store, cache);
//Act
Thread.Sleep(10); //wait 10 seconds for cache to expire
var item = repository.GetById<string>(123);
//Assert
Assert.AreEqual("This is my data is from the cache", item);
}