Using Multiple Buckets with the CouchbaseClient
It is not possible to configure (in app|web.config) a single instance of a CouchbaseClient to work with multiple buckets. Though it is possible to programmatically reconstruct a client to work with multiple buckets, it is not recommended. The process of creating a client is expensive (relative to other Couchbase operations) and should ideally be done once per app domain.
It is possible however to set multiple config sections in app|web.config to allow for multiple client instances to be created, while still maintaining bucket affinity.
<?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="couchbase"> <section name="bucket-a" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/> <section name="bucket-b" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/> </sectionGroup> </configSections> <couchbase> <bucket-a> <servers bucket="default"> <add uri="http://127.0.0.1:8091/pools" /> </servers> </bucket-a> <bucket-b> <servers bucket="beernique" bucketPassword="b33rs"> <add uri="http://127.0.0.1:8091/pools" /> </servers> </bucket-b> </couchbase> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
After defining the config sections, bucket specific clients are created by reading the appropriate config sections and passing the config section reference to the constructor of the CouchbaseClient. Again, constructing the client should not be done per operation, but rather per app domain.
var bucketASection = (CouchbaseClientSection)ConfigurationManager.GetSection("couchbase/bucket-a"); var bucketBSection = (CouchbaseClientSection)ConfigurationManager.GetSection("couchbase/bucket-b"); var clientA = new CouchbaseClient(bucketASection); var clientB = new CouchbaseClient(bucketBSection); clientA.ExecuteStore(StoreMode.Set, "fooA", "barA"); var itemA = clientA.Get<string>("fooA"); Console.WriteLine(itemA); clientB.ExecuteStore(StoreMode.Set, "fooB", "barB"); var itemB = clientB.Get<string>("fooB"); Console.WriteLine(itemB);