当前位置: 首页>>代码示例>>C#>>正文


C# Caching.CachePolicy类代码示例

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


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


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