public void MutipleCacheTestHelperTest()
        {
            //BaseTest.RegisterServiceStart();//×Ô¶¯×¢²áRedis£¬Ò²¿ÉÒÔÊÖ¶¯×¢²á
            //BaseTest.RegisterServiceCollection();

            var exCache      = TestExtensionCacheStrategy.Instance;      //Íê³ÉÁìÓò»º´æ×¢²á
            var exRedisCache = TestExtensionRedisCacheStrategy.Instance; //Íê³ÉRedisÁìÓò»º´æ×¢²á

            MutipleCacheTestHelper.RunMutipleCache(() =>
            {
                try
                {
                    var currentCache = CacheStrategyFactory.GetObjectCacheStrategyInstance();
                    Console.WriteLine("µ±Ç°»º´æ²ßÂÔ£º" + currentCache.GetType());

                    var testExCache = CacheStrategyFactory.GetExtensionCacheStrategyInstance(new TestCacheDomain());
                    var baseCache   = testExCache.BaseCacheStrategy();

                    Console.WriteLine("µ±Ç°À©Õ¹»º´æ²ßÂÔ£º" + baseCache.GetType());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);//LocalÒѾ­×¢²á£¬Redisδע²á
                }
            }, CacheType.Local, CacheType.Redis);
        }
Esempio n. 2
0
        public void ContainerTest()
        {
            MutipleCacheTestHelper.RunMutipleCache(() =>
            {
                //获取Token完整结果(包括当前过期秒数)
                DateTime dt1    = DateTime.Now;
                var tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
                DateTime dt2    = DateTime.Now;

                Assert.IsNotNull(tokenResult);
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

                if (base._useRedis)
                {
                    Thread.Sleep(2500);//等待缓存更新
                }

                //只获取Token字符串
                dt1       = DateTime.Now;
                var token = AccessTokenContainer.GetAccessToken(base._appId);
                dt2       = DateTime.Now;
                Assert.AreEqual(tokenResult.access_token, token);
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

                //getNewToken
                {
                    dt1   = DateTime.Now;
                    token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, false);
                    dt2   = DateTime.Now;
                    Console.WriteLine(token);
                    Assert.AreEqual(tokenResult.access_token, token);

                    Console.WriteLine("强制重新获取AccessToken");
                    dt1   = DateTime.Now;
                    token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, true);
                    dt2   = DateTime.Now;
                    Console.WriteLine(token);
                    Assert.AreNotEqual(tokenResult.access_token, token);//如果微信服务器缓存,此处会相同
                    Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
                }

                {
                    tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
                    if (base._useRedis)
                    {
                        Thread.Sleep(2500);//等待缓存更新
                    }
                    Console.WriteLine("HashCode:{0}", tokenResult.GetHashCode());
                    dt1          = DateTime.Now;
                    var allItems = AccessTokenContainer.GetAllItems();
                    dt2          = DateTime.Now;
                    Assert.IsTrue(allItems.Count > 0);

                    //序列化
                    var d1 = StackExchangeRedisExtensions.Serialize(tokenResult);
                    var d2 = StackExchangeRedisExtensions.Serialize(allItems[0].AccessTokenResult);

                    Assert.AreEqual(String.Concat(d1), String.Concat(d2));//证明缓存成功
                    Console.WriteLine("All Items:{0}", allItems.Count);
                    Console.WriteLine("HashCode:{0}", allItems[0].AccessTokenResult.GetHashCode());
                    Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
                }
            }, CacheType.Local, CacheType.Redis);
        }