本文整理汇总了C#中EntityFramework.Caching.CacheKey类的典型用法代码示例。如果您正苦于以下问题:C# CacheKey类的具体用法?C# CacheKey怎么用?C# CacheKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CacheKey类属于EntityFramework.Caching命名空间,在下文中一共展示了CacheKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddWithExistingTagTest
public void AddWithExistingTagTest()
{
var provider = new MemoryCacheProvider();
string key = "AddWithExistingTagTest" + DateTime.Now.Ticks;
string[] tags = new[] { "a", "b" };
var cacheKey = new CacheKey(key, tags);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = provider.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
// make sure cache key is in underlying MemoryCache
var cacheTag = new CacheTag("a");
string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
tagKey.Should().NotBeNullOrEmpty();
var cachedTag = MemoryCache.Default.Get(tagKey);
cachedTag.Should().NotBeNull();
// add second value with same tag
string key2 = "AddWithExistingTagTest2" + DateTime.Now.Ticks;
string[] tags2 = new[] { "a", "c" };
var cacheKey2 = new CacheKey(key2, tags2);
var value2 = "Test Value 2 " + DateTime.Now;
var cachePolicy2 = new CachePolicy();
bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
result2.Should().BeTrue();
// tag 'a' should have same value
var cachedTag2 = MemoryCache.Default.Get(tagKey);
cachedTag2.Should().NotBeNull();
cachedTag2.Should().Be(cachedTag);
}
示例2: ExpireTest
//[Fact]
public void ExpireTest()
{
var cacheManager = new CacheManager();
string key = "ExpireTest" + DateTime.Now.Ticks;
var tags = new[] { "a", "b" };
var cacheKey = new CacheKey(key, tags);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = cacheManager.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
// add second value with same tag
string key2 = "ExpireTest2" + DateTime.Now.Ticks;
var tags2 = new[] { "a", "c" };
var cacheKey2 = new CacheKey(key2, tags2);
var value2 = "Test Value 2 " + DateTime.Now;
var cachePolicy2 = new CachePolicy();
bool result2 = cacheManager.Add(cacheKey2, value2, cachePolicy2);
result2.Should().BeTrue();
// add third value with same tag
string key3 = "ExpireTest3" + DateTime.Now.Ticks;
var tags3 = new[] { "b", "c" };
var cacheKey3 = new CacheKey(key3, tags3);
var value3 = "Test Value 3 " + DateTime.Now;
var cachePolicy3 = new CachePolicy();
bool result3 = cacheManager.Add(cacheKey3, value3, cachePolicy3);
result3.Should().BeTrue();
var cacheTag = new CacheTag("a");
string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
tagKey.Should().NotBeNullOrEmpty();
var cachedTag = cacheManager.Get<object>(tagKey);
cachedTag.Should().NotBeNull();
// expire actually just changes the value for tag key
cacheManager.Expire(cacheTag);
// allow flush
System.Threading.Thread.Sleep(500);
var expiredTag = cacheManager.Get<object>(tagKey);
expiredTag.Should().NotBeNull();
expiredTag.Should().NotBe(cachedTag);
// items should have been removed
var expiredValue = cacheManager.Get<string>(cacheKey.Key);
expiredValue.Should().BeNull();
var expiredValue2 = cacheManager.Get<string>(cacheKey2.Key);
expiredValue2.Should().BeNull();
var expiredValue3 = cacheManager.Get<string>(cacheKey3.Key);
expiredValue3.Should().NotBeNull();
}
示例3: AddWithTagsTest
public void AddWithTagsTest()
{
var provider = new MemoryCacheProvider();
string key = "AddWithTagsTest" + DateTime.Now.Ticks;
string[] tags = new[] { "a", "b" };
var cacheKey = new CacheKey(key, tags);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = provider.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
// look in underlying MemoryCache
string innerKey = MemoryCacheProvider.GetKey(cacheKey);
var cachedValue = MemoryCache.Default.Get(innerKey);
cachedValue.Should().NotBeNull();
cachedValue.Should().Be(value);
// make sure cache key is in underlying MemoryCache
var cacheTag = new CacheTag("a");
string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
tagKey.Should().NotBeNullOrEmpty();
var cachedTag = MemoryCache.Default.Get(tagKey);
cachedTag.Should().NotBeNull();
}
示例4: KeyTest
public void KeyTest()
{
string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
var target = new CacheKey(key);
target.Should().NotBeNull();
target.Key.Should().NotBeNull();
target.Key.Should().Be(key);
}
示例5: CacheKeyConstructorTest1
public void CacheKeyConstructorTest1()
{
string key = string.Empty;
var target = new CacheKey(key);
target.Should().NotBeNull();
target.Key.Should().NotBeNull();
target.Key.Should().Be(string.Empty);
}
示例6: Add
/// <summary>
/// Inserts a cache entry into the cache without overwriting any existing cache entry.
/// </summary>
/// <param name="cacheKey">A unique identifier for the cache entry.</param>
/// <param name="value">The object to insert.</param>
/// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
/// <returns>
/// <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
/// </returns>
public bool Add(CacheKey cacheKey, object value, CachePolicy cachePolicy)
{
string key = GetKey(cacheKey);
var item = new CacheItem(key, value);
var policy = CreatePolicy(cacheKey, cachePolicy);
var existing = MemoryCache.Default.AddOrGetExisting(item, policy);
return existing.Value == null;
}
示例7: AddTest
public void AddTest()
{
var cacheManager = new CacheManager();
var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = cacheManager.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
}
示例8: TagsTest
public void TagsTest()
{
string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
string[] tags = new[] { "a", "b" };
var target = new CacheKey(key, tags);
target.Should().NotBeNull();
target.Key.Should().NotBeNull();
target.Key.Should().Be(key);
target.Tags.Should().HaveCount(2);
}
示例9: AddTest
public void AddTest()
{
var provider = new MemoryCacheProvider();
var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = provider.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
// look in underlying MemoryCache
string innerKey = MemoryCacheProvider.GetKey(cacheKey);
var cachedValue = MemoryCache.Default.Get(innerKey);
cachedValue.Should().NotBeNull();
cachedValue.Should().Be(value);
}
示例10: GetOrAdd
/// <summary>
/// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
/// </summary>
/// <param name="cacheKey">A unique identifier for the cache entry.</param>
/// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
/// <param name="cachePolicy">A <see cref="CachePolicy"/> that contains eviction details for the cache entry.</param>
/// <returns>
/// The value for the key. This will be either the existing value for the key if the key is already in the cache,
/// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the cache.
/// </returns>
public object GetOrAdd(CacheKey cacheKey, Func<CacheKey, object> valueFactory, CachePolicy cachePolicy)
{
string key = GetKey(cacheKey);
if (MemoryCache.Default.Contains(key))
{
Debug.WriteLine("Cache Hit : " + key);
return MemoryCache.Default.Get(key);
}
Debug.WriteLine("Cache Miss: " + key);
// get value and add to cache
object value = valueFactory(cacheKey);
if (Add(cacheKey, value, cachePolicy))
return value;
// add failed
return null;
}
示例11: GetOrAdd
/// <summary>
/// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
/// </summary>
/// <param name="cacheKey">A unique identifier for the cache entry.</param>
/// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
/// <param name="cachePolicy">A <see cref="CachePolicy"/> that contains eviction details for the cache entry.</param>
/// <returns>
/// The value for the key. This will be either the existing value for the key if the key is already in the cache,
/// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the cache.
/// </returns>
public object GetOrAdd(CacheKey cacheKey, Func<CacheKey, object> valueFactory, CachePolicy cachePolicy)
{
var key = GetKey(cacheKey);
var cachedResult = MemoryCache.Default.Get(key);
if (cachedResult != null)
{
Debug.WriteLine("Cache Hit : " + key);
return cachedResult;
}
Debug.WriteLine("Cache Miss: " + key);
// get value and add to cache, not bothered
// if it succeeds or not just rerturn the value
var value = valueFactory(cacheKey);
this.Add(cacheKey, value, cachePolicy);
return value;
}
示例12: GetOrAddTest
public void GetOrAddTest()
{
var provider = new MemoryCacheProvider();
var cacheKey = new CacheKey("GetOrAddTest" + DateTime.Now.Ticks);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
int callCount = 0;
Func<CacheKey, object> valueFactory = k =>
{
callCount++;
return value;
};
var result = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
result.Should().Be(value);
callCount.Should().Be(1);
// look in underlying MemoryCache
string innerKey = MemoryCacheProvider.GetKey(cacheKey);
var cachedValue = MemoryCache.Default.Get(innerKey);
cachedValue.Should().NotBeNull();
cachedValue.Should().Be(value);
callCount = 0;
var result2 = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
result2.Should().Be(value);
callCount.Should().Be(0);
}
示例13: SetTest
public void SetTest()
{
var cacheManager = new CacheManager();
var cacheKey = new CacheKey("SetTest" + DateTime.Now.Ticks);
var value = "Set Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
cacheManager.Set(cacheKey, value, cachePolicy);
var cachedValue = cacheManager.Get(cacheKey.Key);
cachedValue.Should().NotBeNull();
cachedValue.Should().Be(value);
var value2 = "Set Value " + DateTime.Now;
cacheManager.Set(cacheKey, value2, cachePolicy);
var cachedValue2 = cacheManager.Get(cacheKey.Key);
cachedValue2.Should().NotBeNull();
cachedValue2.Should().Be(value2);
}
示例14: ExpireTest
public void ExpireTest()
{
var cache = MemoryCache.Default;
// purge all values
foreach (KeyValuePair<string, object> pair in cache)
cache.Remove(pair.Key);
var provider = new MemoryCacheProvider();
string key = "ExpireTest" + DateTime.Now.Ticks;
var tags = new[] { "a", "b" };
var cacheKey = new CacheKey(key, tags);
var value = "Test Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = provider.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
// add second value with same tag
string key2 = "ExpireTest2" + DateTime.Now.Ticks;
var tags2 = new[] { "a", "c" };
var cacheKey2 = new CacheKey(key2, tags2);
var value2 = "Test Value 2 " + DateTime.Now;
var cachePolicy2 = new CachePolicy();
bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
result2.Should().BeTrue();
// add third value with same tag
string key3 = "ExpireTest3" + DateTime.Now.Ticks;
var tags3 = new[] { "b", "c" };
var cacheKey3 = new CacheKey(key3, tags3);
var value3 = "Test Value 3 " + DateTime.Now;
var cachePolicy3 = new CachePolicy();
bool result3 = provider.Add(cacheKey3, value3, cachePolicy3);
result3.Should().BeTrue();
var cacheTag = new CacheTag("a");
string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
tagKey.Should().NotBeNullOrEmpty();
// underlying cache
cache.GetCount().Should().Be(6);
var cachedTag = cache.Get(tagKey);
cachedTag.Should().NotBeNull();
System.Threading.Thread.Sleep(500);
// expire actually just changes the value for tag key
provider.Expire(cacheTag);
var expiredTag = cache.Get(tagKey);
expiredTag.Should().NotBeNull();
expiredTag.Should().NotBe(cachedTag);
// items should have been removed
var expiredValue = provider.Get<string>(cacheKey);
expiredValue.Should().BeNull();
var expiredValue2 = provider.Get<string>(cacheKey2);
expiredValue2.Should().BeNull();
var expiredValue3 = provider.Get<string>(cacheKey3);
expiredValue3.Should().NotBeNull();
cache.GetCount().Should().Be(4);
}
示例15: GetTest
public void GetTest()
{
var provider = new MemoryCacheProvider();
var cacheKey = new CacheKey("GetTest" + DateTime.Now.Ticks);
var value = "Get Value " + DateTime.Now;
var cachePolicy = new CachePolicy();
bool result = provider.Add(cacheKey, value, cachePolicy);
result.Should().BeTrue();
var existing = provider.Get<string>(cacheKey);
existing.Should().NotBeNull();
existing.Should().BeSameAs(value);
}