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


C# CacheStorage.GetFromCacheOrFetch方法代码示例

本文整理汇总了C#中CacheStorage.GetFromCacheOrFetch方法的典型用法代码示例。如果您正苦于以下问题:C# CacheStorage.GetFromCacheOrFetch方法的具体用法?C# CacheStorage.GetFromCacheOrFetch怎么用?C# CacheStorage.GetFromCacheOrFetch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CacheStorage的用法示例。


在下文中一共展示了CacheStorage.GetFromCacheOrFetch方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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);
                }
            }
开发者ID:JaysonJG,项目名称:Catel,代码行数:50,代码来源:CacheStorageFacts.cs

示例2: 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);
                }
            }
开发者ID:Catel,项目名称:Catel,代码行数:15,代码来源:CacheStorageFacts.cs

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

示例4: 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);
            }
开发者ID:Catel,项目名称:Catel,代码行数:11,代码来源:CacheStorageFacts.cs

示例5: ThrowsArgumentNullExceptionForNullFunction

            public void ThrowsArgumentNullExceptionForNullFunction()
            {
                var cache = new CacheStorage<string, int>();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.GetFromCacheOrFetch("1", null));
            }
开发者ID:Catel,项目名称:Catel,代码行数:6,代码来源:CacheStorageFacts.cs

示例6: 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);
                }
            }
开发者ID:paytonli2013,项目名称:Catel,代码行数:13,代码来源:CacheStorageFacts.cs


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