本文整理汇总了C#中Cache.Get方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.Get方法的具体用法?C# Cache.Get怎么用?C# Cache.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPackages
public static IList<DataServicePackage> GetPackages(Cache cache)
{
// Try to load if from the cache
var packages = (IList<DataServicePackage>)cache.Get("packages");
// Double check lock
if (packages == null) {
lock (_lockObject) {
packages = (IList<DataServicePackage>)cache.Get("packages");
if (packages == null) {
// If we still don't have anything cached then get the package list and store it.
packages = _repository.GetPackages().AsEnumerable().Cast<DataServicePackage>().ToList();
cache.Insert("packages",
packages,
null,
DateTime.Now + TimeSpan.FromSeconds(20),
Cache.NoSlidingExpiration);
}
}
}
return packages;
}
示例2: AddGet_DateTime_ShouldAddAndExpire_Test
public void AddGet_DateTime_ShouldAddAndExpire_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, DateTime.Now + new TimeSpan(0, 0, 1));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
Thread.Sleep(2000);
Assert.IsNull(cache.Get<string, string>(TestKey));
}
示例3: Add_ShouldUpdateIfPresent_Test
public void Add_ShouldUpdateIfPresent_Test()
{
Cache cache = new Cache();
var otherValue = "otherValue";
cache.Add(TestKey, TestValue, new TimeSpan(0, 0, 1));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
cache.Add(TestKey, otherValue, new TimeSpan(0, 0, 1));
Assert.AreEqual(otherValue, cache.Get<string, string>(TestKey));
}
示例4: TestCache
public void TestCache()
{
var types = new[] { typeof(A1), typeof(A1) };
var infos = types.Select( t => new CallInfo( t, null, Flags.StaticInstanceAnyVisibility, MemberTypes.Property, "P1", Type.EmptyTypes, null, true ) ).ToList();
var cache = new Cache<CallInfo, object>();
infos.ForEach( ci => cache.Insert( ci, ci ) );
Assert.AreEqual( 1, cache.Count );
Assert.IsNotNull( cache.Get( infos[ 0 ] ) );
Assert.IsNotNull( cache.Get( infos[ 1 ] ) );
Assert.AreEqual( infos[0], cache.Get( infos[ 0 ] ) );
}
示例5: Cache_DataAddedAndReceived_ShouldPass
public void Cache_DataAddedAndReceived_ShouldPass()
{
var storage = new AsteriskStorage();
var datetime = new ChangeableTime();
var cache = new Cache<int, string>(16,1000,10000,storage,datetime);
cache.Get(1);
cache.Get(2);
Assert.AreEqual("1",cache[1]);
Assert.AreEqual("2",cache.Get(2));
}
示例6: ShouldReturnResultValueFromDictionaryWhenTypeIsInCache
public void ShouldReturnResultValueFromDictionaryWhenTypeIsInCache()
{
// Given
var cache = new Cache<Type, Exception>();
var initialValue = cache.Get(typeof(ArgumentNullException), () => new ArgumentNullException());
// When
var value = cache.Get(typeof(ArgumentNullException), () => new ArgumentNullException());
// Then
value.Should().BeSameAs(initialValue);
}
示例7: Cache_DataAddedAndReceived_ShouldPass
public void Cache_DataAddedAndReceived_ShouldPass()
{
var storage = new AsteriskStorage();
//datetime -> dateTime
var datetime = new ChangeableTime(); //this magic number to const
var cache = new Cache<int, string>(16,1000,10000,storage,datetime);// needs more space
cache.Get(1);
cache.Get(2);
Assert.AreEqual("1",cache[1]);
Assert.AreEqual("2",cache.Get(2));
}
示例8: should_keep_cache_if_versions_same
public async Task should_keep_cache_if_versions_same()
{
var cacheConfiguration = new CacheConfiguration(1024, 5, 1024, 5);
var cacheContainer = InitializeCacheContainer();
var storage = (TestStorage)cacheContainer.Resolve<IStorage>();
cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version(1, 1));
using (var cache = new Cache(cacheContainer, cacheConfiguration))
{
await cache.Initialize();
//when at least one value set cache is written
await cache.Set("some_entry", 42);
}
//cache should not be cleanued up if versions in storage and executing assembly differ
cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version(1, 1));
using (var cache = new Cache(cacheContainer, cacheConfiguration))
{
await cache.Initialize();
storage.KeyToStreams.Should().NotBeEmpty();
cache.Get<Int32>("some_entry").Result.Value.Should().Be(42);
}
}
示例9: AddGet_DateTime_ShouldAdd_Test
public void AddGet_DateTime_ShouldAdd_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, DateTime.Now + new TimeSpan(0, 5, 0));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
}
示例10: AddGet_TimeSpan_ShouldAddToTheCache_Test
public void AddGet_TimeSpan_ShouldAddToTheCache_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, new TimeSpan(0, 5, 0));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
}
示例11: Cache_Get_Item_Key_Should_Return_Item
public void Cache_Get_Item_Key_Should_Return_Item()
{
Cache<string, int> cache = new Cache<string, int>();
int EXPECTED = 1;
cache.Add("1", 1);
Assert.AreEqual(EXPECTED, cache.Get("1"));
}
示例12: Cache_AddedInFull_ShouldPass
public void Cache_AddedInFull_ShouldPass()
{
var storage = new AsteriskStorage();
var datetime = new ChangeableTime();
var cache = new Cache<int, string>(2,1000,10000,storage,datetime);
cache.Get(1);
datetime.AddTime(300);
cache.Get(2);
cache.Get(3);
Assert.AreEqual("2", cache.Get(2));
Assert.AreEqual("3", cache[3]);
Assert.AreEqual("1*", cache.Get(1));
}
示例13: Cache_AddedInFull_ShouldPass
public void Cache_AddedInFull_ShouldPass()
{
var storage = new AsteriskStorage();
//datetime -> dateTime
var datetime = new ChangeableTime();//this magic number to const
var cache = new Cache<int, string>(2,1000,10000,storage,datetime);// needs more space
cache.Get(1);
datetime.AddTime(300);
cache.Get(2);
cache.Get(3);
Assert.AreEqual("2", cache.Get(2));
Assert.AreEqual("3", cache[3]);
Assert.AreEqual("1*", cache.Get(1));
}
示例14: Main
public static void Main()
{
var _cache = new Cache<string, DateTime>();
while (true)
{
_cache.Get("Time", MethodThatShouldNotBeCalledVeryFrequently, TimeSpan.FromSeconds(2));
}
}
示例15: 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()));
}