BB.Caching.Cache.LoadFromConfig C# (CSharp) Method

LoadFromConfig() public static method

Loads BB.Caching settings from you app config file, or from the section supplied.
public static LoadFromConfig ( ConfigurationSection section = null, bool establishConnections = true ) : List
section System.Configuration.ConfigurationSection /// An optional to load configuration data from. If this is left as null, /// the method will try to load your configuration settings from the current projects app config file. ///
establishConnections bool /// Talks with the redis instances defined in the connection strings to establish connections. Testing classes /// will set this to false. ///
return List
        public static List<ConnectionGroup> LoadFromConfig(
            ConfigurationSection section = null, bool establishConnections = true)
        {
            var connectionGroups = new List<ConnectionGroup>();

            // if the section is not supplied, look it up in the current configuration file
            if (section == null)
            {
                section = (ConfigurationSection)ConfigurationManager.GetSection("BB.Caching");
            }

            // try to cast the object to our configuration type
            var customSection = section as BB.Caching.Configuration;
            if (customSection == null)
            {
                return connectionGroups;
            }

            // create actual ConnectionGroup instances from the configuration data
            foreach (GroupElement group in customSection.ConnectionGroups)
            {
                string name = group.Name;
                var connectionGroup = new ConnectionGroup(name, group.IsAnalytics);

                connectionGroups.Add(connectionGroup);

                foreach (ConnectionElement connection in group.ConnectionCollections)
                {
                    string type = connection.Type;
                    string connectionString = connection.ConnectionString;

                    switch (type)
                    {
                        case "read":
                        {
                            connectionGroup.AddReadConnection(connectionString, establishConnections);
                            break;
                        }

                        case "write":
                        {
                            connectionGroup.AddWriteConnection(connectionString, establishConnections);
                            break;
                        }

                        default:
                        {
                            throw new Exception(string.Format("type {0} is not supported", type));
                        }
                    }
                }
            }

            // load each ConnectionGroup for use
            foreach (var group in connectionGroups)
            {
                if (group.IsAnalytics)
                {
                    SharedCache.Instance.SetAnalyticsConnectionGroup(group);
                }
                else
                {
                    SharedCache.Instance.AddRedisConnectionGroup(group);
                }
            }

            return connectionGroups;
        }
    }