本文整理汇总了C#中CacheStorage类的典型用法代码示例。如果您正苦于以下问题:C# CacheStorage类的具体用法?C# CacheStorage怎么用?C# CacheStorage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CacheStorage类属于命名空间,在下文中一共展示了CacheStorage类的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: ThrowsArgumentNullExceptionForNullKey
public void ThrowsArgumentNullExceptionForNullKey()
{
var cache = new CacheStorage<string, int>();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() =>
{
var value = cache.Get(null);
Assert.IsNull(value);
});
}
示例3: RunMultipleThreadsWithRandomAccessCalls
public void RunMultipleThreadsWithRandomAccessCalls()
{
var cacheStorage = new CacheStorage<Guid, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(1)));
var threads = new List<Thread>();
for (int i = 0; i < 25; i++)
{
var thread = new Thread(() =>
{
var random = new Random();
for (int j = 0; j < 10000; j++)
{
var randomGuid = _randomGuids[random.Next(0, 9)];
cacheStorage.GetFromCacheOrFetch(randomGuid, () =>
{
var threadId = Thread.CurrentThread.ManagedThreadId;
Log.Info("Key '{0}' is now controlled by thread '{1}'", randomGuid, threadId);
return threadId;
});
ThreadHelper.Sleep(1);
}
});
threads.Add(thread);
thread.Start();
}
while (true)
{
bool anyThreadAlive = false;
foreach (var thread in threads)
{
if (thread.IsAlive)
{
anyThreadAlive = true;
break;
}
}
if (!anyThreadAlive)
{
break;
}
ThreadHelper.Sleep(500);
}
}
示例4: RunMultipleThreadsWithRandomAccessCalls
private void RunMultipleThreadsWithRandomAccessCalls(Func<ICacheStorage<Guid, int>, Guid, int> retrievalFunc)
{
var cacheStorage = new CacheStorage<Guid, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(250)));
var threads = new List<Thread>();
for (var i = 0; i < 50; i++)
{
var thread = new Thread(() =>
{
var random = new Random();
for (var j = 0; j < 1000; j++)
{
var randomGuid = _randomGuids[random.Next(0, 9)];
retrievalFunc(cacheStorage, randomGuid);
ThreadHelper.Sleep(10);
}
});
threads.Add(thread);
thread.Start();
}
while (true)
{
var anyThreadAlive = false;
foreach (var thread in threads)
{
if (thread.IsAlive)
{
anyThreadAlive = true;
break;
}
}
if (!anyThreadAlive)
{
break;
}
ThreadHelper.Sleep(500);
}
}
示例5: LoadTexture
public static int LoadTexture(string filename, CacheStorage cache)
{
GL.ActiveTexture(TextureUnit.Texture0);
filename = filename.ToLower();
if (cache.materials.ContainsKey(filename))
{
// Console.WriteLine("[CACHE HIT] " + filename);
return cache.materials[filename];
}
//Console.WriteLine("[CACHE MISS] " + filename);
int textureId = GL.GenTexture();
var blp = new BLPReader();
blp.LoadBLP(filename);
if (blp.bmp == null)
{
throw new Exception("BMP is null!");
}
else
{
GL.BindTexture(TextureTarget.Texture2D, textureId);
cache.materials.Add(filename, textureId);
System.Drawing.Imaging.BitmapData bmp_data = blp.bmp.LockBits(new System.Drawing.Rectangle(0, 0, blp.bmp.Width, blp.bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
blp.bmp.UnlockBits(bmp_data);
}
// Console.WriteLine("[CACHE ADD] " + filename);
return textureId;
}
示例6: DisposesExpiredItemWhenDisposingNotEnabledButForcedByEventArgs
public void DisposesExpiredItemWhenDisposingNotEnabledButForcedByEventArgs()
{
var disposable = new CustomDisposable();
var cache = new CacheStorage<string, CustomDisposable>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
cache.Expired += (sender, e) =>
{
e.Dispose = true;
};
cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250));
ThreadHelper.Sleep(750);
Assert.IsTrue(disposable.IsDiposed);
}
示例7: 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);
}
示例8: 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));
}
示例9: AddsAndExpiresSeveralItems
public void AddsAndExpiresSeveralItems()
{
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
for (int i = 0; i < 5; i++)
{
ThreadHelper.Sleep(1000);
int innerI = i;
var value = cache.GetFromCacheOrFetch("key", () => innerI, expiration: TimeSpan.FromMilliseconds(250));
Assert.AreEqual(i, value);
}
}
示例10: 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"));
}
示例11: 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);
}
示例12: 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);
}
示例13: ThrowsArgumentNullExceptionForNullFunction
public void ThrowsArgumentNullExceptionForNullFunction()
{
var cache = new CacheStorage<string, int>();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.GetFromCacheOrFetch("1", null));
}
示例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: AddsAndExpiresSeveralItems
public void AddsAndExpiresSeveralItems()
{
var cache = new CacheStorage<string, int>();
for (int i = 0; i < 5; i++)
{
var value = cache.GetFromCacheOrFetch("key", () => i, expiration: new TimeSpan(0, 0, 1));
Assert.AreEqual(i, value);
ThreadHelper.Sleep(2000);
}
}