C # distributed Cache Couchbase use
Introduction ofCouchbase
At present, the most used Cache system in C # industry is mainly memcached and Redis. These two Cache system can be said to be a more mature solution, but also many systems of course choice. Memcache is basically the standard of development, but for the memcache cluster, many online deployments are still very thin. Several existing problems: not robust, data insecurity, configuration changes may cause access to exceptions, backing data consistency, Memcached does not support persistence.
In view of the above problems, Memcache's development team developed Membase, supporting multiple server clusters, slicing and replicating data, effectively improving service stability and data security, and supporting persistent data storage. Code stability is not good enough, I once used in some online business membase, and later because of a few inexplicable access to the operation of slow, can only be withdrawn back to Memcache.
After that, the Membase team merged with the COUCHDB team to launch the two-in-oneness product: Couchbase.
The biggest features of Couchbase:
1, complete inheritance memcache, native support all memcache operation;
2, inherits the Membase characteristic, supports the cluster and the data persistence;
3, inherits the COUCHDB document Nature, supports the operation through the view to the data. The view features, which provide the convenience that the previous memcache did not have, but are simple and primitive compared with MongoDB, and the feeling is not a function of universal demand.
Today, also learn the simple use of couchbase.
Second, Couchbase service-side installation
Server-Side Download address: Http://www.couchbase.com/download Choose the appropriate system version of the download installation. Currently the latest 4.0.0.
Couchbase is managed using the Web, after installation, if successful, then in the browser can be seen. If you do not then need to manually access http://localhost:8091/index.html. This machine installs can use localhost, can use IP or hostname.
Click Setup on the open page, default settings, then click Next, and then go to the following
Enter the administrator password. Continue next and finalize the configuration to enter the home page. In the inside can see a lot of things, need to study slowly.
After installation, you can also find out in the service:
Second, the client calls
Create a console application to test and then install the DLL for the Couchbase client via NuGet. My project is based on. NETFramework4.0, so choose Couchbasenetclient 1.3.12.
The reference is complete, the default connection address is: Http://127.0.0.1:8091/pools/default, code code to make a simple call:
Static void main (String[] args) { //Configuration Server CouchbaseClientConfiguration cbcc = new
Couchbaseclientconfiguration (); //set various timeout times            CBCC.
Socketpool.receivetimeout = new timespan (0, 0, 2);             CBCC.
Socketpool.connectiontimeout = new timespan (0, 0, 4);             CBCC.
Socketpool.deadtimeout = new timespan (0, 0, 10); //Use the default numberAccording to the library             CBCC.
Urls.add (New uri ("Http://127.0.0.1:8091/pools/default")); //establishes a client, loads the client configuration couchbaseclient client
= new couchbaseclient (CBCC); //add a piece of data casresult<bool> casresult = client. Cas (storemode.add, "Test", "hello world!"); //Get the data you just added console.writeline ( Client.
Get ("Test")); &nbsP Console.WriteLine ("Done!")
");
console.readline (); }
The difference between Storemode.add, Storemode.replace and Storemode.set
Add: Indicates that a new key is added;
Replace: Indicates that an existing key has been updated;
Set: Indicates that if the key does not exist, it is added, there is an update.
There are additions (Cas), remove (remove), and other methods under couchbaseclient for subsequent study.
The first run run gets the cached values of the settings:
The following comments on the set value are dropped: casresult casresult = client. Cas (Storemode.add, "Test", "Hello world!");
Run the value again or get the:
Other test results:
The web background can also see the added data and has been encrypted:
Memcache Upgrade: Couchbase installation configuration and use instructions
Memcache is basically the standard of development, but for the memcache cluster, many online deployments are still very thin.
several existing problems: not robust, data insecurity, configuration changes may lead to access to exceptions, backup data consistency
In view of the above problems, Memcache's development team developed Membase, supporting multiple server clusters, slicing and replicating data, effectively improving service stability and data security, and supporting persistent data storage. Code stability is not good enough, I once used in some online business membase, and later because of a few inexplicable access to the operation of slow, can only be withdrawn back to Memcache.
After that, the Membase team merged with the COUCHDB team to launch the two-in-oneness product: Couchbase.
The biggest features of Couchbase:
1, complete inheritance memcache, native support all memcache operation;
2, inherits the Membase characteristic, supports the cluster and the data persistence;
3, inherits the COUCHDB document Nature, supports the operation through the view to the data. The view features, which provide the convenience that the previous memcache did not have, but are simple and primitive compared with MongoDB, and the feeling is not a function of universal demand.
First, from the installation:
Install Server:
Go to the official website to download the corresponding version of the installation package http://www.couchbase.com currently the latest version is 2.1.1
Starting with Membase, it is recommended that you manage the Web (after you finish installing files, http://localhost:8091 port access in the browser), and the default is Port 8091.
Another piece of information is about the port. 11211 is the Memcache access port, 8091 is the management and Operation Port, and several others are used for managing and trunking communications.
Open 8091 for initialization settings:
The initialization process is user-friendly, and the first step is to select the physical storage location and set the new creation or join an existing cluster.
If you choose a new creation, you need to specify the amount of memory used (note that in a cluster, the MEM memory of all clusters must be greater than this value). Next, you can also choose to automatically pour some of the test data, so that you can directly experience the effects of some view.
The next step is to create the default bucket setting, which is important:
The meaning of Bucket is to separate the data, for example: Any view is based on a Bucket, only the data within the Bucket processing. There can be multiple bucket on one server, and each bucket storage type, content footprint, data replication quantity, and so on, each needs to be specified separately. In this sense, each bucket is equivalent to a separate instance. In the cluster state, we need to set up the server cluster, bucket only focus on the storage of data.
To create a bucket, you need to indicate the drive type: memcached as the name implies, pure memory, no replication set and other functions; The Couchbase type is with persistent storage and can have a replica set.
For replication sets, you can set: Disable the replica set, the number of replica sets (if set to 2, the data will be saved on two nodes, any one node is still in the presence of data), and whether the index is replicated. Flush, is whether to support the operation of emptying all data.
The recommendation is to use the Couchbase type, set the memory usage, others by default. About memory settings, try not to allocate the memory on the server all out, leave some idle, the new bucket test is very convenient. Of course, the amount of memory consumed by bucket can be adjusted dynamically after it is established.
Follow the prompts below, set up the administrator password, the service can be used. In the first page of the management interface, you can see the overview of the operation:
In the Servernodes frame, you can view the server cluster, in view, you can manage the view in each bucket, in the databuckets, buckets management. For example, adjust memory footprint:
There is only one server node, replica is meaningless, let's build a second server node. The installation process is similar, here choose to join the cluster, fill in the IP and password:
Click Next and verify that you can join the cluster. Any server in the cluster, the status is equal, with the browser to connect up can be managed.
As you can see, the newly joined server is in a Pending state, meaning that the data has not been synchronized. This node can provide services only after data has been synchronized. Click the rebalance button to synchronize the data. And the progress bar shows that it's really thoughtful.