AddWriteConnection() public method

Adds a write connection.
public AddWriteConnection ( string connection, bool establishConnection = true ) : void
connection string /// The connection. ///
establishConnection bool /// Talks with the redis instance defined in the connection string to establish a connection. ///
return void
            public void WriteOnly()
            {
                var connectionGroup = new ConnectionGroup("node-0");
                var connectionString = string.Format("{0}:{1},allowAdmin=True", "12.34.56.78", "1234");

                connectionGroup.AddWriteConnection(connectionString, false);

                Assert.Equal("node-0", connectionGroup.Name);
                Assert.Equal(0, connectionGroup.ReadConnections.Count);

                Assert.Equal(connectionString, connectionGroup.WriteConnection);
            }
示例#2
0
        public DefaultTestFixture()
        {
            var connectionGroup1 = new ConnectionGroup("node-0");

            connectionGroup1.AddWriteConnection(string.Format("{0}:{1},allowAdmin=True", this.TestIp, this.TestPort1));

            SharedCache.Instance.AddRedisConnectionGroup(connectionGroup1);
            SharedCache.Instance.SetAnalyticsConnectionGroup(connectionGroup1);

            if (0 != this.TestPort2)
            {
                var connectionGroup2 = new ConnectionGroup("node-1");

                connectionGroup2.AddWriteConnection(string.Format("{0}:{1}", this.TestIp, this.TestPort1));

                SharedCache.Instance.AddRedisConnectionGroup(connectionGroup2);
            }

            PubSub.Configure(ConnectionMultiplexer.Connect(string.Format("{0}:{1}", this.TestIp, this.TestPort1)));
            Cache.Prepare();
        }
示例#3
0
        /// <summary>
        /// Loads BB.Caching settings from you app config file, or from the <paramref name="section"/> supplied.
        /// </summary>
        /// <param name="section">
        /// An optional <see cref="ConfigurationSection"/> 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.
        /// </param>
        /// <param name="establishConnections">
        /// Talks with the redis instances defined in the connection strings to establish connections. Testing classes
        /// will set this to false.
        /// </param>
        /// <returns>
        /// The <see cref="ConnectionGroup"/> objects that were loaded. This is returned for testing purposes.
        /// </returns>
        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;
        }