public IRoutedCacheClient PushClientWithRule(IRoutedCacheClientRule rule)
        {
            CheckNotDisposedOrThrow();

            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

            mRules.Push(rule);
            return(this);
        }
示例#2
0
        private IRoutedCacheClient CreateRoutedCacheClient(ICacheClient fallbackClient, ICacheClient sessionClient, params IRoutedCacheClientRule[] rules)
        {
            IRoutedCacheClientRule sessionCacheClientRule = new KeyStartsWithStringCacheClientRule(sessionClient,
                                                                                                   StringComparison.InvariantCultureIgnoreCase,
                                                                                                   SessionKeyPrefix);

            IRoutedCacheClientRule[] newRules = new IRoutedCacheClientRule[rules.Length + 1];

            newRules[0] = sessionCacheClientRule;
            for (int i = 0; i < rules.Length; i++)
            {
                newRules[i + 1] = rules[i];
            }

            return(CreateRoutedCacheClient(fallbackClient,
                                           newRules));
        }
        private List <string> GenerateTestKeys(int numKeys,
                                               List <IRoutedCacheClientRule> forRules,
                                               Dictionary <Guid, IList <string> > keysForClients)
        {
            List <string> testCacheKeys =
                new List <string>();

            for (int i = 0; i < numKeys; i++)
            {
                string key = Guid.NewGuid().ToString();
                testCacheKeys.Add(key);

                IRoutedCacheClientRule rule = forRules[i % forRules.Count];
                keysForClients[rule.Id].Add(key);
            }

            return(testCacheKeys);
        }
        protected void Dispose(bool disposing)
        {
            if (!mIsDisposed)
            {
                if (disposing)
                {
                    while (mRules.Count > 0)
                    {
                        IRoutedCacheClientRule rule = mRules.Pop();
                        if (rule.AutoDispose)
                        {
                            rule.Client.Dispose();
                        }
                    }
                }

                mIsDisposed = true;
            }
        }
示例#5
0
        /// <summary>
        /// Given a collection of keys and a function that returns a cache client rule for a given key
        ///		(that is a rule for which .Match() applied to that key returns true)
        ///		assigns a list of keys for each cache client rule.
        /// </summary>
        /// <param name="keys">The collection of keys</param>
        /// <param name="ruleSelector">Maps each key to a matching cache client rule</param>
        public void CollectAll(IEnumerable <string> keys, Func <string, IRoutedCacheClientRule> ruleSelector)
        {
            if (keys == null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            if (ruleSelector == null)
            {
                throw new ArgumentNullException(nameof(ruleSelector));
            }

            foreach (string key in keys)
            {
                IRoutedCacheClientRule rule = ruleSelector.Invoke(key);
                if (rule != null)
                {
                    Collect(key, rule);
                }
            }
        }
示例#6
0
        /// <summary>
        /// Assigns a key to the given rule.
        /// </summary>
        /// <param name="key">The key</param>
        /// <param name="rule">The rule</param>
        public void Collect(string key, IRoutedCacheClientRule rule)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

            IList <string> keysForClient;

            if (!mKeysForCacheClient.TryGetValue(rule.Id, out keysForClient))
            {
                keysForClient = new List <string>();
                mKeysForCacheClient[rule.Id] = keysForClient;
                mCacheClients[rule.Id]       = rule.Client;
            }

            keysForClient.Add(key);
        }
        public void Test_CanCollect_Single(int numKeys, int numClients)
        {
            PerKeyCacheClientRuleAggregator aggregator =
                new PerKeyCacheClientRuleAggregator();

            Dictionary <Guid, IList <string> > keysForCacheClients =
                new Dictionary <Guid, IList <string> >();

            //Init rules
            List <IRoutedCacheClientRule> cacheClientRules =
                CreateTestRules(numClients);

            foreach (IRoutedCacheClientRule r in cacheClientRules)
            {
                keysForCacheClients[r.Id] = new List <string>();
            }

            //Generate some keys and distribute
            // them per cache client rules
            List <string> testCacheKeys = GenerateTestKeys(numKeys,
                                                           cacheClientRules,
                                                           keysForCacheClients);

            foreach (KeyValuePair <Guid, IList <string> > keysForRules in keysForCacheClients)
            {
                IRoutedCacheClientRule rule = cacheClientRules.FirstOrDefault(r => r.Id == keysForRules.Key);
                foreach (string key in keysForRules.Value)
                {
                    aggregator.Collect(key, rule);
                }
            }

            Assert_KeysCollected(aggregator,
                                 keysForCacheClients,
                                 cacheClientRules,
                                 numKeys);
        }