Ejemplo n.º 1
0
        public void Can_use_batch_operations()
        {
            cache.SetAll(new Dictionary <string, string>
            {
                { "Car", "Audi" },
                { "Phone", "MotoX" }
            });

            var response = cache.GetAll <string>(new List <string> {
                "Car", "Phone"
            });

            Assert.That(response["Car"], Is.EqualTo("Audi"));
            Assert.That(response["Phone"], Is.EqualTo("MotoX"));

            var singleResponse = cache.Get <string>("Phone");

            Assert.That(singleResponse, Is.EqualTo("MotoX"));

            cache.RemoveAll(new List <string> {
                "Car", "Phone"
            });

            response = cache.GetAll <string>(new List <string> {
                "Car", "Phone"
            });
            Assert.That(response["Car"], Is.EqualTo(default(string)));
            Assert.That(response["Phone"], Is.EqualTo(default(string)));
        }
        public void Does_SetAll_and_GetAll()
        {
            var map = 3.Times(i => new Item {
                Id = i, Name = "Name" + i
            })
                      .ToSafeDictionary(x => x.ToUrn());

            Cache.SetAll(map);

            var cacheMap = Cache.GetAll <Item>(map.Keys);

            Assert.That(cacheMap, Is.EquivalentTo(map));
        }
Ejemplo n.º 3
0
        public ICollection <ICacheEntry <int, IBinaryObject> > GetAllBinary()
        {
            var first = Random.Next(0, Params.Instance.Value.TotalObjects - Params.Instance.Value.BatchSize);
            var keys  = Enumerable.Range(first, Params.Instance.Value.BatchSize);

            return(_binaryCache.GetAll(keys));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Cache GetAll.
        /// </summary>
        private void GetAll(BenchmarkState state)
        {
            var idx  = BenchmarkUtils.GetRandomInt(Dataset - DatasetBatchSize);
            var keys = Enumerable.Range(idx, DatasetBatchSize);

            Cache.GetAll(keys);
        }
        public IDictionary <string, T> GetAll <T> (IEnumerable <string> keys)
        {
            CheckNotDisposedOrThrow();

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

            IDictionary <string, T> values =
                new Dictionary <string, T>();

            PerKeyCacheClientRuleAggregator aggregator
                = new PerKeyCacheClientRuleAggregator();

            try
            {
                //The naive approach would be to find the cache client for each key
                // then issue a Get() call for that key and value,
                // but that won't use any optimisation a cache client might have for bulk operations.
                //Issuing a GetAll() for each client with the given set of values, doesn't work either,
                // since that would bypass any routing.
                //So, we need to see first which key by which client can be resolved,
                // and then issue a GetAll() on each client using only the subset of keys
                // it can resolve

                aggregator.CollectAll(keys, FindRule);

                foreach (KeyValuePair <Guid, IList <string> > keysForClient in aggregator.KeysForCacheClient)
                {
                    ICacheClient client = aggregator
                                          .CacheClients[keysForClient.Key];

                    IDictionary <string, T> clientValues =
                        client.GetAll <T>(keysForClient.Value);

                    if (clientValues != null)
                    {
                        foreach (KeyValuePair <string, T> valuePair in clientValues)
                        {
                            values.Add(valuePair);
                        }
                    }
                }
            }
            finally
            {
                aggregator.Clear();
            }

            return(values);
        }
Ejemplo n.º 6
0
        public IEnumerable <T> GetList <T>()
        {
            if (!IsCachingServer)
            {
                return(null);
            }
            try
            {
                List <Tuple <string, T> > objList = new List <Tuple <string, T> >();

                var pattern = Helper.GetClassName <T>() + ":*";
                IEnumerable <string> keys = Sut.SearchKeys(pattern);

                var data = Sut.GetAll <T>(keys);
                return(data.Values.AsEnumerable());
            }
            catch (Exception ex)
            {
                LogManager.Logger.LogError(ex);
                return(null);
            }
        }
Ejemplo n.º 7
0
        public static IDictionary <string, T> GetFromCache <T>(this ICacheClient cacheClient,
                                                               IEnumerable <string> cacheKeys,
                                                               string pattern,
                                                               Func <object, T> factoryFn)
        {
            using (IRedisClient redisClient = (cacheClient as  IRedisClientsManager).GetClient()){
                List <string> res = redisClient.SearchKeys(pattern);

                if (cacheKeys != null && cacheKeys.ToList().Count > 0)
                {
                    var s = (from r in res
                             from c in cacheKeys
                             where r == c
                             select r).ToList();

                    var cache = (s != null && s.Count > 0)? cacheClient.GetAll <T>(s):  new Dictionary <string, T>();

                    foreach (string key in cacheKeys)
                    {
                        if (!s.Contains(key))
                        {
                            T t = factoryFn(UrnId.GetStringId(key));
                            if (t != null)
                            {
                                cacheClient.Set <T>(key, t);
                                cache.Add(key, t);
                            }
                        }
                    }
                    return(cache);
                }
                else
                {
                    return((res != null && res.Count > 0)? cacheClient.GetAll <T>(res): new Dictionary <string, T>());;
                }
            }
        }
        public IActionResult GetAll()
        {
            ICacheClient client          = getCacheClient();
            var          contentKeysGrab = client.SearchKeys("widget_*");
            var          result          = client.GetAll <Widget>(contentKeysGrab).OrderBy(c => c.Key);

            if (result == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(result));
            }
        }
Ejemplo n.º 9
0
        public IEnumerable <KeyValuePair <string, T> > Get(IEnumerable <string> keys)
        {
            if (true)
            {
                return(keys.Select(k => new KeyValuePair <string, T>(k, client.Get <T>(keyPrefix + k))).ToArray());
            }
            else
            {
                // https://github.com/StackExchange/StackExchange.Redis/issues/448
                // this can throw an ExecutionEngineException on the StringGet portion of the method below, hence the above workaround
                var prefixedKeys = keys.Select(key => keyPrefix + key);
                var results      = client.GetAll <T>(prefixedKeys);

                return(results.Select(kvp => new KeyValuePair <string, T>(kvp.Key.Split(':')[1], kvp.Value)));
            }
        }
        protected static void AssertCacheClientMissingModelValuesAsKeysWithNullValues(
			ICacheClient cacheClient)
        {
            var allKeys = new[] { "test:modelkey1", "test:modelkey2", "test:modelkey3" };
            var expectedValues = new[] { ModelWithIdAndName.Create(1), null, ModelWithIdAndName.Create(1) };
            cacheClient.Set(allKeys[0], expectedValues[0]);
            cacheClient.Set(allKeys[2], expectedValues[2]);

            var keyValues = cacheClient.GetAll<ModelWithIdAndName>(allKeys);
            Assert.That(keyValues, Has.Count.EqualTo(expectedValues.Length));

            for (var keyIndex = 0; keyIndex < expectedValues.Length; keyIndex++)
            {
                var key = allKeys[keyIndex];
                var keyValue = keyValues[key];

                ModelWithIdAndName.AssertIsEqual(keyValue, expectedValues[keyIndex]);
            }
        }
        protected static void AssertCacheClientIntModelValuesAsKeysWithNullValues(
			ICacheClient cacheClient)
        {
            var allKeys = new[] { "test:intkey1", "test:intkey2", "test:intkey3" };
            var expectedValues = new[] { 1, default(int), 3 };
            cacheClient.RemoveAll(allKeys);

            cacheClient.Set(allKeys[0], expectedValues[0]);
            cacheClient.Set(allKeys[2], expectedValues[2]);

            var keyValues = cacheClient.GetAll<int>(allKeys);
            Assert.That(keyValues, Has.Count.EqualTo(expectedValues.Length));

            for (var keyIndex = 0; keyIndex < expectedValues.Length; keyIndex++)
            {
                var key = allKeys[keyIndex];
                var keyValue = keyValues[key];
                Assert.That(keyValue, Is.EqualTo(expectedValues[keyIndex]));
            }
        }
Ejemplo n.º 12
0
        public void CacheGet_NewNodeEnteredTopology_RequestIsRoutedToDefaultNode()
        {
            // Warm-up.
            Assert.AreEqual(1, _cache.Get(1));

            // Before topology change.
            Assert.AreEqual(12, _cache.Get(12));
            Assert.AreEqual(1, GetClientRequestGridIndex());

            Assert.AreEqual(14, _cache.Get(14));
            Assert.AreEqual(2, GetClientRequestGridIndex());

            // After topology change.
            var cfg = GetIgniteConfiguration();

            cfg.AutoGenerateIgniteInstanceName = true;

            using (var ignite = Ignition.Start(cfg))
            {
                var affinityChangedTop = new AffinityTopologyVersion(ignite.GetCluster().TopologyVersion, 1);

                Assert.True(ignite.WaitTopology(affinityChangedTop, _cache.Name),
                            "Failed to wait topology " + affinityChangedTop);

                // Warm-up.
                Assert.AreEqual(1, _cache.Get(1));

                // Get default node index by performing non-partition-aware operation.
                _cache.GetAll(Enumerable.Range(1, 10));
                var defaultNodeIdx = GetClientRequestGridIndex("GetAll");
                Assert.Greater(defaultNodeIdx, -1);

                // Assert: keys 12 and 14 belong to a new node now, but we don't have the new node in the server list.
                // Requests are routed to default node.
                Assert.AreEqual(12, _cache.Get(12));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());

                Assert.AreEqual(14, _cache.Get(14));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());
            }
        }
Ejemplo n.º 13
0
        public void CacheGet_NewNodeEnteredTopology_RequestIsRoutedToDefaultNode()
        {
            // Warm-up.
            Assert.AreEqual(1, _cache.Get(1));

            // Before topology change.
            Assert.AreEqual(12, _cache.Get(12));
            Assert.AreEqual(1, GetClientRequestGridIndex());

            Assert.AreEqual(14, _cache.Get(14));
            Assert.AreEqual(2, GetClientRequestGridIndex());

            // After topology change.
            var cfg = GetIgniteConfiguration();

            cfg.AutoGenerateIgniteInstanceName = true;

            using (var ignite = Ignition.Start(cfg))
            {
                // Wait for rebalance.
                var events = ignite.GetEvents();
                events.EnableLocal(EventType.CacheRebalanceStopped);
                events.WaitForLocal(EventType.CacheRebalanceStopped);

                // Warm-up.
                Assert.AreEqual(1, _cache.Get(1));

                // Get default node index by performing non-partition-aware operation.
                _cache.GetAll(Enumerable.Range(1, 10));
                var defaultNodeIdx = GetClientRequestGridIndex("GetAll");
                Assert.Greater(defaultNodeIdx, -1);

                // Assert: keys 12 and 14 belong to a new node now, but we don't have the new node in the server list.
                // Requests are routed to default node.
                Assert.AreEqual(12, _cache.Get(12));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());

                Assert.AreEqual(14, _cache.Get(14));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());
            }
        }
        protected static void AssertCacheClientMissingModelValuesAsKeysWithNullValues(
            ICacheClient cacheClient)
        {
            var allKeys        = new[] { "test:modelkey1", "test:modelkey2", "test:modelkey3" };
            var expectedValues = new[] { ModelWithIdAndName.Create(1), null, ModelWithIdAndName.Create(1) };

            cacheClient.Set(allKeys[0], expectedValues[0]);
            cacheClient.Set(allKeys[2], expectedValues[2]);

            var keyValues = cacheClient.GetAll <ModelWithIdAndName>(allKeys);

            Assert.That(keyValues, Has.Count.EqualTo(expectedValues.Length));

            for (var keyIndex = 0; keyIndex < expectedValues.Length; keyIndex++)
            {
                var key      = allKeys[keyIndex];
                var keyValue = keyValues[key];

                ModelWithIdAndName.AssertIsEqual(keyValue, expectedValues[keyIndex]);
            }
        }
        protected static void AssertCacheClientIntModelValuesAsKeysWithNullValues(
            ICacheClient cacheClient)
        {
            var allKeys        = new[] { "test:intkey1", "test:intkey2", "test:intkey3" };
            var expectedValues = new[] { 1, default(int), 3 };

            cacheClient.RemoveAll(allKeys);

            cacheClient.Set(allKeys[0], expectedValues[0]);
            cacheClient.Set(allKeys[2], expectedValues[2]);

            var keyValues = cacheClient.GetAll <int>(allKeys);

            Assert.That(keyValues, Has.Count.EqualTo(expectedValues.Length));

            for (var keyIndex = 0; keyIndex < expectedValues.Length; keyIndex++)
            {
                var key      = allKeys[keyIndex];
                var keyValue = keyValues[key];
                Assert.That(keyValue, Is.EqualTo(expectedValues[keyIndex]));
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Execute bulk Put and Get operations.
        /// </summary>
        /// <param name="ignite">Ignite instance.</param>
        private static void PutAllGetAll(IIgniteClient ignite)
        {
            ICacheClient <int, Organization> cache = ignite.GetCache <int, Organization>(CacheName);

            // Create new Organizations to store in cache.
            Organization org1 = new Organization(
                "Microsoft",
                new Address("1096 Eddy Street, San Francisco, CA", 94109),
                OrganizationType.Private,
                DateTime.Now
                );

            Organization org2 = new Organization(
                "Red Cross",
                new Address("184 Fidler Drive, San Antonio, TX", 78205),
                OrganizationType.NonProfit,
                DateTime.Now
                );

            var map = new Dictionary <int, Organization> {
                { 1, org1 }, { 2, org2 }
            };

            // Put created data entries to cache.
            cache.PutAll(map);

            // Get recently created organizations as a strongly-typed fully de-serialized instances.
            ICollection <ICacheEntry <int, Organization> > mapFromCache = cache.GetAll(new List <int> {
                1, 2
            });

            Console.WriteLine();
            Console.WriteLine(">>> Retrieved organization instances from cache:");

            foreach (ICacheEntry <int, Organization> org in mapFromCache)
            {
                Console.WriteLine(">>>     " + org.Value);
            }
        }
Ejemplo n.º 17
0
 public IDictionary <string, T> GetAll <T>(IEnumerable <object> keys)
 {
     return(_clientExt.GetAll <T>(RedisUtils.KeysBuilder <T>(keys)));
 }
Ejemplo n.º 18
0
 public IDictionary <string, T> GetAll <T>(IEnumerable <string> keys)
 {
     return(cache.GetAll <T>(keys.Select(x => prefix + x)));
 }