• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Caching.CachePolicy类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中EntityFramework.Caching.CachePolicy的典型用法代码示例。如果您正苦于以下问题:C# CachePolicy类的具体用法?C# CachePolicy怎么用?C# CachePolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



CachePolicy类属于EntityFramework.Caching命名空间,在下文中一共展示了CachePolicy类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: 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();
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:61,代码来源:CacheManagerTest.cs


示例2: 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();
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:26,代码来源:MemoryCacheProviderTest.cs


示例3: 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);
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:35,代码来源:MemoryCacheProviderTest.cs


示例4: CachePolicyConstructorTest

 public void CachePolicyConstructorTest()
 {
     var cachePolicy = new CachePolicy();
     
     cachePolicy.Should().NotBeNull();
     cachePolicy.Mode.Should().Be(CacheExpirationMode.None);
     cachePolicy.AbsoluteExpiration.Should().Be(ObjectCache.InfiniteAbsoluteExpiration);
     cachePolicy.SlidingExpiration.Should().Be(ObjectCache.NoSlidingExpiration);
 }
开发者ID:dioptre,项目名称:nkd,代码行数:9,代码来源:CachePolicyTest.cs


示例5: 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;
        }
开发者ID:vas6ili,项目名称:EntityFramework.Extended,代码行数:18,代码来源:MemoryCacheProvider.cs


示例6: 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();
        }
开发者ID:khoale,项目名称:EntityFramework.Extended,代码行数:10,代码来源:CacheManagerTest.cs


示例7: 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);
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:16,代码来源:MemoryCacheProviderTest.cs


示例8: 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;
        }
开发者ID:vas6ili,项目名称:EntityFramework.Extended,代码行数:28,代码来源:MemoryCacheProvider.cs


示例9: 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;
        }
开发者ID:NetUtil,项目名称:Util,代码行数:31,代码来源:MemoryCacheProvider.cs


示例10: Set

 /// <summary>
 /// Inserts a cache entry into the cache overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 public void Set(string key, object value)
 {
     var policy = new CachePolicy();
     Set(key, value, policy);
 }
开发者ID:RobersonLuo,项目名称:EntityFramework.Extended,代码行数:10,代码来源:CacheManager.cs


示例11: GetOrAddAsync

        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned asynchronously by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The asynchronous function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object 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 dictionary, 
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public virtual Task<object> GetOrAddAsync(CacheKey cacheKey, Func<CacheKey, Task<object>> valueFactory, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();
            var item = provider.GetOrAddAsync(cacheKey, valueFactory, cachePolicy);

            return item;
        }
开发者ID:RobersonLuo,项目名称:EntityFramework.Extended,代码行数:17,代码来源:CacheManager.cs


示例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);
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:29,代码来源:MemoryCacheProviderTest.cs


示例13: 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);
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:69,代码来源:MemoryCacheProviderTest.cs


示例14: Add

 /// <summary>
 /// Inserts a cache entry into the cache without overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</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(string key, object value)
 {
     var cachePolicy = new CachePolicy();
     return Add(key, value, cachePolicy);
 }
开发者ID:RobersonLuo,项目名称:EntityFramework.Extended,代码行数:11,代码来源:CacheManager.cs


示例15: GetTest

        public void GetTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("GetTest" + DateTime.Now.Ticks);
            var value = "Get Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            var existing = cacheManager.Get(cacheKey.Key);
            existing.Should().NotBeNull();
            existing.Should().BeSameAs(value);

        }
开发者ID:khoale,项目名称:EntityFramework.Extended,代码行数:15,代码来源:CacheManagerTest.cs


示例16: 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);
        }
开发者ID:khoale,项目名称:EntityFramework.Extended,代码行数:20,代码来源:CacheManagerTest.cs


示例17: RemoveTest

        public void RemoveTest()
        {
            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();

            // look in underlying MemoryCache
            var cachedValue = cacheManager.Get(cacheKey.Key);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            var removed = cacheManager.Remove(cacheKey);
            removed.Should().NotBeNull();
            removed.Should().Be(value);

            // look in underlying MemoryCache
            var previous = cacheManager.Get(cacheKey.Key);
            previous.Should().BeNull();
        }
开发者ID:khoale,项目名称:EntityFramework.Extended,代码行数:23,代码来源:CacheManagerTest.cs


示例18: GetOrAddTest

        public void GetOrAddTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("AddTest" + 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 = cacheManager.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result.Should().Be(value);
            callCount.Should().Be(1);

            var cachedValue = cacheManager.Get(cacheKey.Key);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            callCount = 0;
            var result2 = cacheManager.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result2.Should().Be(value);
            callCount.Should().Be(0);

        }
开发者ID:khoale,项目名称:EntityFramework.Extended,代码行数:28,代码来源:CacheManagerTest.cs


示例19: GetOrAdd

 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
 /// or the new value if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(string key, object value)
 {
     var policy = new CachePolicy();
     return GetOrAdd(key, value, policy);
 }
开发者ID:RobersonLuo,项目名称:EntityFramework.Extended,代码行数:14,代码来源:CacheManager.cs


示例20: 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);
        }
开发者ID:jdomizio,项目名称:EntityFramework.Extended,代码行数:14,代码来源:MemoryCacheProviderTest.cs



注:本文中的EntityFramework.Caching.CachePolicy类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Interfaces.StandardProviderParameters类代码示例发布时间:2022-05-24
下一篇:
C# Caching.CacheKey类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap