Exemple #1
0
        public void SingleThreaded_AutoEvictKeys()
        {
            int             cacheSize = 5;
            ConcurrentCache cache     = new ConcurrentCache(cacheSize);

            for (int i = 0; i < 2 * cacheSize; i++)
            {
                cache.Get(i.ToString());
            }
            // [5, 6, 7, 8, 9]
            for (int i = 0; i < cacheSize; i++)
            {
                cache.Get(i.ToString());
            }
            // [1, 2, 3, 4, 5]
            Assert.AreEqual(0, cache.CacheHit);
            Assert.AreEqual(3 * cacheSize, cache.TotalRequest);

            for (int i = 0; i < cacheSize; i++)
            {
                cache.Get(i.ToString());
            }
            // [1, 2, 3, 4, 5]
            Assert.AreEqual(cacheSize, cache.CacheHit);
        }
Exemple #2
0
        public void SingleThreaded_CacheHits()
        {
            int             cacheSize = 1000;
            ConcurrentCache cache     = new ConcurrentCache(cacheSize);

            foreach (var i in Enumerable.Range(0, cacheSize / 2))
            {
                cache.Get(i.ToString());
            }
            Assert.AreEqual(0, cache.CacheHit);
            Assert.AreEqual(cacheSize / 2, cache.TotalRequest);

            foreach (var i in Enumerable.Range(0, cacheSize / 2))
            {
                cache.Get(i.ToString());
            }
            Assert.AreEqual(cacheSize / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize, cache.TotalRequest);

            foreach (var i in Enumerable.Range(cacheSize / 2, cacheSize / 2))
            {
                cache.Get(i.ToString());
            }
            Assert.AreEqual(cacheSize / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize * 3 / 2, cache.TotalRequest);

            foreach (var i in Enumerable.Range(0, cacheSize))
            {
                cache.Get(i.ToString());
            }
            Assert.AreEqual(cacheSize * 3 / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize * 5 / 2, cache.TotalRequest);
        }
Exemple #3
0
        public void SingleThreaded_RepeatedCallReturnsCachedValue()
        {
            ConcurrentCache cache = new ConcurrentCache(10);
            var             val   = cache.Get("1");
            var             val2  = cache.Get("1");

            Assert.AreEqual(val, val2);
            Assert.AreEqual(1, cache.CacheHit);
        }
Exemple #4
0
        public void SingleThreaded_EmptyCacheReturns()
        {
            ConcurrentCache cache = new ConcurrentCache(10);
            var             value = cache.Get("1");

            Assert.IsNotNull(value);
        }
Exemple #5
0
        /// <summary>
        /// Gets the factory which will create an instance of the type indicated by <see cref="ResolveContext.RequestedType"/>
        /// of the passed <paramref name="context"/>.
        /// </summary>
        /// <param name="context">The context containing the requested type and any scope which is currently in force.</param>
        /// <returns>Always returns a reference to a delegate - but note that if
        /// <see cref="CanResolve(Type)"/> returns false for the same context, then the target's
        /// delegate will likely throw an exception.</returns>
        public Func <ResolveContext, object> GetFactory(ResolveContext context)
        {
#if !ENABLE_IL_EMIT
            return(_cache.Get(context));
#else
            return(_dynCache.GetFactory(context.RequestedType));
#endif
        }
        public async Task CacheShouldReturnDefaultValueIfKeyNull()
        {
            var cache = new ConcurrentCache();

            var cached = await cache.Get <ProviderMetadata>(null, true);

            Assert.IsNull(cached);
        }
Exemple #7
0
        public void MultiThreaded_CacheHits()
        {
            int             cacheSize = 1000;
            ConcurrentCache cache     = new ConcurrentCache(cacheSize);

            Parallel.ForEach(Enumerable.Range(0, cacheSize / 2), (i) => cache.Get(i.ToString()));
            Assert.AreEqual(0, cache.CacheHit);
            Assert.AreEqual(cacheSize / 2, cache.TotalRequest);

            Parallel.ForEach(Enumerable.Range(0, cacheSize / 2), (i) => cache.Get(i.ToString()));
            Assert.AreEqual(cacheSize / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize, cache.TotalRequest);

            Parallel.ForEach(Enumerable.Range(cacheSize / 2, cacheSize / 2), (i) => cache.Get(i.ToString()));
            Assert.AreEqual(cacheSize / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize * 3 / 2, cache.TotalRequest);

            Parallel.ForEach(Enumerable.Range(0, cacheSize), (i) => cache.Get(i.ToString()));
            Assert.AreEqual(cacheSize * 3 / 2, cache.CacheHit);
            Assert.AreEqual(cacheSize * 5 / 2, cache.TotalRequest);
        }
        public async Task RemoveShouldRemoveStoredResponse()
        {
            var cache = new ConcurrentCache();
            var mcc   = "001";
            var mnc   = "01";

            await cache.Add(mcc, mnc, new DiscoveryResponse(_responses[0]));

            await cache.Remove(mcc, mnc);

            var actual = await cache.Get(mcc, mnc);

            Assert.IsNull(actual);
        }
        public async Task CacheShouldGetResponseWhenMultipleStored()
        {
            var cache    = new ConcurrentCache();
            var expected = new DiscoveryResponse(_responses[1]);
            var mcc      = "001";
            var mnc      = "01";

            await cache.Add(mcc, mnc, expected);

            await cache.Add("002", "02", new DiscoveryResponse(_responses[0]));

            var actual = await cache.Get(mcc, mnc);

            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Cached);
            Assert.IsNotNull(actual.ResponseData.response.apis);
        }
Exemple #10
0
        private async Task <IEnumerable <IAzureUserAccountSubscriptionContext> > GetSubscriptionsFromCacheAsync(AzureUserAccount user)
        {
            var result = Enumerable.Empty <IAzureUserAccountSubscriptionContext>();

            if (user != null)
            {
                result = _subscriptionCache.Get(user.UniqueId);
                if (result == null)
                {
                    result = await GetSubscriptionFromServiceAsync(user);

                    _subscriptionCache.UpdateCache(user.UniqueId, result);
                }
            }
            result = result ?? Enumerable.Empty <IAzureUserAccountSubscriptionContext>();
            return(result);
        }
        public object CallAction(ActionSerDes actionDes)
        {
            MethodInfo callMethodInfo = null;
            Type       actionType     = ConstractInterfaceCache.Get(actionDes.TargetTypeFullName);
            dynamic    serviceValue   = IocUnity.Get(actionType);
            string     actionKey      = actionDes.GetRouteAddress();

            callMethodInfo = ActionMethodInfoCache.GetOrAdd(actionKey, key =>
            {
                var sameNameMethodList = actionType.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(f => f.Name == actionDes.MethodName).ToList();
                MethodInfo methodInfo  = null;
                //只区分参数个数不区分类型
                if (sameNameMethodList.Count == 1)
                {
                    methodInfo = sameNameMethodList.FirstOrDefault();
                }
                else
                {
                    methodInfo = sameNameMethodList.FirstOrDefault(f => f.GetParameters().Length == actionDes.ParamterInfoArray.Length);
                }
                return(methodInfo);
            });
            if (callMethodInfo == null)
            {
                throw new KeyNotFoundException($"路由地址没有找到【{actionKey}】!");
            }

            var    actionParamters = actionDes.GetParamters(callMethodInfo);
            object rtnObj          = null;

            try
            {
                var dynamicDelegate = DynamicMethodTool.GetMethodInvoker(callMethodInfo);
                rtnObj = dynamicDelegate(serviceValue, actionParamters);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                throw;
            }

            return(rtnObj);
        }
        public async Task AddShouldStoreDiscoveryResponse()
        {
            var cache    = new ConcurrentCache();
            var response = new DiscoveryResponse(_responses[0]);
            var mcc      = "001";
            var mnc      = "01";

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(response, new Newtonsoft.Json.JsonSerializerSettings {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            });

            await cache.Add(mcc, mnc, response);

            var actual = await cache.Get(mcc, mnc);

            Assert.IsFalse(cache.IsEmpty);
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Cached);
            Assert.AreEqual(response.ResponseData.response, actual.ResponseData.response);
        }
Exemple #13
0
        public static Type Create(IDictionary <string, Type> properties)
        {
            TypeCacheEntryKey key = new TypeCacheEntryKey(properties);

            return(typeCache.Get(key, () => CreateRowType(properties)));
        }
Exemple #14
0
 /// <summary>
 /// There was a wired nullReferencedException was running the tasks parallel. It only got fixed when I put the getting from cache insed an async method
 /// </summary>
 private Task <ServiceResponse <DatabaseInstanceInfo> > GetFromCacheAsync(string key)
 {
     return(Task.Factory.StartNew(() => _cache.Get(key)));
 }
 /// <summary>
 ///     Return the latch associated to specified data loader configuration
 /// </summary>
 /// <param name="key"> Identifies the data loader configuration. </param>
 /// <returns> The configuration latch. </returns>
 public static DataLoaderConfigurationLatch GetLatch(
     DataLoaderConfigurationKey key)
 {
     return(store.Get(key, () => new DataLoaderConfigurationLatch()));
 }
        public async Task <CachedParameters> getData(string state)
        {
            var cachedParameters = cache.Get <CachedParameters>(state);

            return(await cachedParameters);
        }