花了2天的时间配置和使用了Couchbase,本来打算用memcached+Enyim,遇到种种问题最后放弃了,以下简要的做下笔记.

什么是couchbase?

Couchbase:非集群,轻量级版本的Couchbase旨为移动开发者提供便利,个人理解即内存数据库。

这两种技术完美结合。CouchOne和CouchDB数据库以开发人员为中心,为部署广泛的开源文档解决方案提供便利。而Membase则是高性能数据存储的领导者,被应用在全球众多最繁忙的网络中,例如Zynga。因为它具有简单、快速并具有弹性架构

官方网站:http://www.couchbase.com/

使用指南(包括web.config配置): http://www.couchbase.com/develop/net/current

产品介绍文档: 查看, 查看pdf

软件下载

Couchbase Server Win64位: down

.Net 客户端下载:上文"使用指南"内有下载地址.

简单的基类编写(自己发挥)

Couchbase 功能基类(CouchbaseProvider.cs)

View Code
 1     //Couchbase 功能基类
 2     internal class CouchbaseProvider
 3     {
 4         #region Singleton
 5 
 6         private static readonly CouchbaseClient _client = new CouchbaseClient();
 7 
 8         // 声明静态构造函数就是为了删除IL里的BeforeFieldInit标记
 9         // 静态自动在使用之前被初始化
10         static CouchbaseProvider()
11         {
12         }
13 
14         private CouchbaseProvider()
15         {
16         }
17 
18         public static CouchbaseClient Client
19         {
20             get { return _client; }
21         }
22 
23         #endregion
24 
25         #region CouchbaseProvider Members
26 
27         internal static bool Add<T>
28             (string key, T value)
29         {
30             if (_client != null)
31             {
32                 return _client.StoreJson(StoreMode.Set, key, value);
33             }
34             return false;
35         }
36 
37         internal static bool Add<T>(string key, T value, int cacheSecond)
38         {
39             if (_client != null)
40             {
41                 string serializeStr = JsonConvert.SerializeObject(value);//转码
42                 return _client.Store(StoreMode.Set, key, serializeStr, new TimeSpan(0, 0, cacheSecond));
43             }
44             return false;
45         }
46 
47         internal static T GetData<T>(string key) where T : class
48         {
49             if (_client == null)
50             {
51                 return default(T);
52             }
53             return _client.GetJson<T>(key);
54         }
55 
56         internal static void Remove(string key)
57         {
58             if (_client != null)
59             {
60                 _client.Remove(key);
61             }
62         }
63 
64         #endregion
65     }

 

列表数据的缓存 抽象父类(MemcacheList_abstract.cs)

View Code
 1     /// <summary>
 2     /// 列表数据的缓存 抽象父类
 3     /// </summary>
 4     /// <typeparam name="T"></typeparam>
 5     public abstract class MemcacheList_abstract<T>
 6     {
 7         private static readonly object LockHelper = new object();
 8 
 9         public MemcacheList_abstract(string memCacheKey, int cacheSecond)
10         {
11             MemCacheKey = memCacheKey;
12             CacheSecond = cacheSecond;
13         }
14 
15         public int CacheSecond { get; private set; }
16 
17         public string MemCacheKey { get; private set; }
18 
19         /// <summary>
20         /// 初始化
21         /// </summary>
22         protected abstract List<T> InitData();
23 
24         /// <summary>
25         /// 获取数据
26         /// </summary>
27         public List<T> GetData()
28         {
29             var data = CouchbaseProvider.GetData<List<T>>(MemCacheKey);
30             if (data == null)
31             {
32                 if (Caches.Get(MemCacheKey) != null)//启用本地缓存
33                 {
34                     return (List<T>)Caches.Get(MemCacheKey);
35                 }
36                 lock (LockHelper)
37                 {
38                     data = CouchbaseProvider.GetData<List<T>>(MemCacheKey);
39                     if (data == null)
40                     {
41                         data = InitData();
42                         bool success;
43                         if (CacheSecond == 0)
44                             success = CouchbaseProvider.Add(MemCacheKey, data);
45                         else
46                             success = CouchbaseProvider.Add(MemCacheKey, data, CacheSecond);
47                         if (success == false)//缓存写入失败,启用本地缓存
48                         {
49                             Caches.Set(MemCacheKey, data, DateTime.Now.AddSeconds(CacheSecond));
50                         }
51                     }
52                 }
53             }
54             return data;
55         }
56 
57         /// <summary>
58         /// 移除
59         /// </summary>
60         public void Remove()
61         {
62             CouchbaseProvider.Remove(MemCacheKey);
63         }
64 
65         /// <summary>
66         /// 刷新
67         /// </summary>
68         public void Refresh()
69         {
70             Remove();//删除
71             GetData();//重新载入缓存
72         }
73 
74     }

 

基类的调用

定义一个自己的缓存类:城市类(MemCityClass.cs)

View Code
 1     /// <summary>
 2     /// 城市操作类
 3     /// </summary>
 4     public class MemCityClass : MemcacheList_abstract<MemCity>
 5     {
 6         public MemCityClass()
 7             : base("MemCity", 0)
 8         { }
 9 
10         protected override List<MemCity> InitData()
11         {
12             using (lDataContext db = lDataContext .GetDataContext())
13             {
14                 var data = (from p in db.city
15                             select new MemCity
16                             {
17                                 id = p.id,
18                                 name = p.name,
19                                 ename = p.ename,
20                                 letters = p.letters,
21                             }).ToList();
22                 return data;
23             }
24         }
25     }
26 
27     /// <summary>
28     /// 城市类
29     /// </summary>
30     public class MemCity
31     {
32         [JsonProperty("id")]
33         public int id { get; set; }
34 
35         [JsonProperty("name")]
36         public string name { get; set; }
37 
38         [JsonProperty("ename")]
39         public string ename { get; set; }
40 
41         [JsonProperty("letters")]
42         public string letters { get; set; }
43     }

 

类的使用

    //获取存储的城市
    var data = new MemCityClass().GetData();
    //修改后,更新缓存
    new MemCityClass().Refresh();

 

发展

  • 1.Couchbase 挺像内存数据库,我还没使用到它的查询功能.
  • 2.貌似支持Log4net,待发现.
  • 3.其他问题,有待发现

小弟初用博客园,欢迎探讨.

 posted on 2013-02-27 16:52  一只要偷懒的猫  阅读(1566)  评论(3编辑  收藏  举报