var someValue = client.Get("somekey") asstring; //获取数据
var someValue = client.Get<string>("somekey"); //获取数据
以上是简单基本类型的使用,下面我们介绍一下复杂类型。先申明一个类
[Serializable] publicclass Beer { public Guid Id { get; set; } publicstring Name { get; set; } publicstring 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);
在CouchBase2.0正式版就开始支持json,这一点让人激动人心。
存储json数据
publicstaticbool 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); }
获取json数据
publicstatic 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; }
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);
检查和操作结果
官方的说明
For check and set operations, the return values are wrapped in a CasResult instance. The success of the operation is still determined by a Boolean and detailed failures still require logging.
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"); }
Each of these methods shares its 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 is 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); }