public void ExecuteCommands(ICanBeUsedInMultiCommand[] commands) { List<ICanBeUsedInMultiCommand>[] commandBuckets = SortCommandsIntoBuckets(commands); for (int i = 0; i < commandBuckets.Length; i++) { List<ICanBeUsedInMultiCommand> commandBucket = commandBuckets[i]; if (commandBucket != null) { MemcachedInstance instance = instances.GetMemcachedInstance(i); using (Pooled<MemcachedSocket> pooledSocket = instance.SocketPool.Get()) { try { int maximumBatchSize = 30;//DAVE - 1 or 5 while (commandBucket.Count > maximumBatchSize) { List<ICanBeUsedInMultiCommand> batch = commandBucket.GetRange(0, maximumBatchSize); commandBucket.RemoveRange(0, maximumBatchSize); RunBatch(batch, pooledSocket); } RunBatch(commandBucket, pooledSocket); pooledSocket.ItemCanBeReturnedToPool = true; } catch (Exception ex) { throw ex; } } } } }
private List<ICanBeUsedInMultiCommand>[] SortCommandsIntoBuckets(ICanBeUsedInMultiCommand[] commands) { List<ICanBeUsedInMultiCommand>[] commandBuckets = new List<ICanBeUsedInMultiCommand>[instances.Count]; foreach (ICanBeUsedInMultiCommand command in commands) { int instanceIndex = instances.GetMemcachedInstanceIndex(command.Key); if (commandBuckets[instanceIndex] == null) { commandBuckets[instanceIndex] = new List<ICanBeUsedInMultiCommand>(); } commandBuckets[instanceIndex].Add(command); } return commandBuckets; }