February 2011, Couchone and Memebase merged, renamed Couchbase, official address (www.couchbase.com). Membase the last version is 1.7.2, which can be downloaded from Couchbase's website (http://www.couchbase.com/downloads-all).
The installation of Couchbase is not described here, only the. NET Client Librarye is used.
- There are two ways to get the SDK for the couchbase for net
- by Nuget,install-package Couchbasenetclient
- Download Http://www.couchbase.com/communities/net via official website
- Start the Couchbase Tour
- To create the project, it is to be reminded that, in vs2010, when we created the class console application, the project uses. NET Framework Client profile by default, and we need to manually switch to the full. NET Framework.
- Configure Couchbase,couchbase in the program to provide encoding configuration and configuration file configuration, of course, using App. config is the most flexible, here is our first choice, add the following information to your configuration file,
<?xml version= "1.0" ? <configuration> <configsections> <section name= "couchbase" type= "Couchbase.Configuration.CouchbaseClientSection, Couchbase "/> </configsections> <couchbase> <servers bucket= "Default" bucketpassword= "" > <add uri= "Http://192.168.0.2:8091/pools" /> <add uri= "Http://192.168.0.3:8091/pools" /> </servers> </couchbase> </configuration> |
The URL list for the cluster configuration is used here, of course, you need only one address for local debugging, and the default couchbase will create a cache bucket (bucket) with no password at the time of installation, and you can freely modify this piece of information. (If you're not sure about buckets, Google yourself).
- Add Reference
Using Couchbase; Using Couchbase.configuration; Using Couchbase.extensions; Using Enyim.caching; Using Enyim.Caching.Configuration; Using Enyim.Caching.Memcached; |
Add references based on actual references
- Create an instance and use
var client = new Couchbaseclient (); Create an instance Client. Store (Storemode.add, "Somekey", "somevalue"); Storing data var somevalue = client. Get ("Somekey") as string; Get Data var somevalue = client. get<string> ("Somekey"); Get Data |
The above is a simple basic type of use, let's describe the complex type below. First of all, declare a class
[Serializable] Public class Beer { Public Guid Id {get; set;} Public string Name {get; set;} Public string brewery {get; set;} } |
var key = Guid.NewGuid (); var beer = new Beer { Id = key, Name = "Old Yankee Ale", Brewery = "Cottrell Brewing Company" }; Client. Store (Storemode.add, "beer_" + key, beer); var beer = client. Get<beer> ("Beer_" + key); |
It's exciting to start supporting JSON in the official version of CouchBase2.0.
Storing JSON data
Public Static BOOL Storejson<t> ( This couchbaseclient client, Storemode storemode, string key, T value) where T: class { var ms = new MemoryStream (); var serializer = new DataContractJsonSerializer (typeof (T)); Serializer. WriteObject (MS, value); var json = Encoding. Default. GetString (Ms. ToArray ()); Ms. Dispose (); return client. Store (Storemode, Key, JSON); } |
Get JSON data
Public Static T getjson<t> ( This couchbaseclient client, string key) where T: class { var json = client. get<string> (key); var ms = new MemoryStream (Encoding. Default. GetBytes (JSON)); var serializer = new DataContractJsonSerializer (typeof (T)); var obj = serializer. ReadObject (ms) as T; Ms. Dispose (); return obj; } |
How to use the client
var key = Guid.NewGuid (); var beer = new Beer { Id = key, Name = "American Ale", Brewery = "Thomas Hooker Brewing Company", Type = "Beer" }; Client. Storejson<beer> (Storemode.add, "beer_" + key, Beer); var beer = client. Getjson<beer> ("Beer_" + key); |
- Check and Operation results
Official notes
For check and set operations, the return values is wrapped in a casresult instance. The success of the operation is still determined by a Boolean and detailed failures still require.
var result = client. Getwithcas ("foo"); var bar = "Bar"; var result = client. Cas (Storemode.set, "foo", bar, result.) Cas); if (result. Result) { Console.WriteLine ("CAS operation was successful"); } |
- Get detailed operation results
If you need to get run-time details, you can use the Ioperationresult API method, which is the description of the official API properties.
Each of these methods shares their name with a method from the Single-value return API, but prefixed with "Execute." For example, Get () becomes executeget () and Store () becomes Executestore ().
Property |
Interface |
Description |
Success |
Ioperationresult |
Whether the operation succeeded |
Message |
Ioperationresult |
Error, warning or informational message |
StatusCode |
Ioperationresult |
Nullable Status code from server |
Innerresult |
Ioperationresult |
Nested result. Populated by low-level I/O failures. |
Value |
Inullableoperationresult |
Extended by Igetoperationresult, where Value was item for given key. |
HasValue |
Inullableoperationresult |
Shortcut for null Value check. |
Cas |
Icasoperationresult |
Extended by Igetoperationresult, Imutateoperationresult, Iconcatoperationresult and Istoreoperationresult. Contains possible CAS value for operations. |
var getresult = client. Executeget<beer> ("Beer_heady_topper"); if (Getresult.success && Getresult.hasvalue) {
var beer = Getresult.value; Beer. Brewery = "The Alchemist"; var casresult = client. Executecas (Storemode.set, "Beer_heady_topper", beer, Getresult.cas);
if (casresult.success) { Console.WriteLine ("CAS operation was successful"); } } Else { Console.WriteLine ("Get operation failed with message {0} and exception {1}", Getresult.message, getresult.exception); } |
- Configuration Log
Couchbase supports log4net and Nlog, and you can get it yourself now or from the SDK provided by Couchbase.
Log4net configuration Reference.
<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <configSections> <sectiongroup name= "enyim.com" > <section name= "Log" type= "Enyim.Caching.Configuration.LoggerSection, enyim.caching"/> </sectionGroup> <section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <enyim.com> <log factory= "Enyim.Caching.Log4NetFactory, Enyim.Caching.Log4NetAdapter"/> </enyim.com> <log4net debug= "false" > <appender name= "Logfileappender" type= "log4net. Appender.fileappender,log4net "> <param name= "File" value= "C:\\temp\\error-log.txt"/> <param name= "Appendtofile" value= "true"/> <layout type= "log4net. Layout.patternlayout,log4net "> <param name= "Conversionpattern" value= "%d [%t]%-5p%c [%x] <%X{auth}> -%m%n "/> </layout> </appender> <root> <priority value= "All"/> <level value= "DEBUG"/> <appender-ref ref= "Logfileappender"/> </root> </log4net> </configuration> |
For more log4net configurations, refer to: http://logging.apache.org/log4net/release/manual/configuration.html.
Nlog Configuration Reference
<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <configSections> <sectiongroup name= "enyim.com" > <section name= "Log" type= "Enyim.Caching.Configuration.LoggerSection, enyim.caching"/> </sectionGroup> <section name= "Nlog" type= "NLog.Config.ConfigSectionHandler, Nlog"/> </configSections> <enyim.com> <log factory= "Enyim.Caching.NLogFactory, Enyim.Caching.NLogAdapter"/> </enyim.com> <nlog> <targets> <target name= "logfile" type= "File" filename= "C:\temp\error-log.txt"/> </targets> <rules> <logger name= "*" minlevel= "Info" writeto= "logfile"/> </rules> </nlog> <startup> <supportedruntime version= "v4.0" sku= ". netframework,version=v4.0 "/> </startup> </configuration> |
For more Nlog configuration, refer to: Http://nlog-project.org/wiki/Configuration_file
Summary: The above information source official getting Started, and attached a copy of their own demo. (There are some variants of document formats published through Office Word)
Demo Source
. NET Using Couchbase Foundation