在Asp.Net下使用couchbase实现分布式缓存

1、下载couchbase windows客户端;http://www.couchbase.com/download;

     默认一步一步安装就可以了;

    

2、编写一个 CacheManager  

    public interface ICacheManager
    {
        bool AddCache(string key, object obj);
        T GetCache<T>(string key) where T : class;
        bool ClearCache(string key);
        bool AddCache(string key, object obj, int minutes);
        void FlushAll();
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Couchbase;
using Enyim.Caching.Memcached;
using System.Runtime.Serialization.Json;
using System.IO;
using Newtonsoft.Json;


    public class CouchbaseManager : ICacheManager
    {
        private readonly static CouchbaseClient _instance;
        static CouchbaseManager()
        {
            _instance = new CouchbaseClient();
        }

        public static CouchbaseClient Instance { get { return _instance; } }

        public void FlushAll()
        {
            Instance.FlushAll();
        }

        public bool AddCache(string key, object obj)
        {
            //return Instance.Store(StoreMode.Add, key, obj);
            Instance.Remove(key);
            string output = JsonConvert.SerializeObject(obj);
            return Instance.Store(StoreMode.Add, key, output);
        }

        public T GetCache<T>(string key) where T : class
        {
            //return Instance.Get<T>(key);

            string output = Instance.Get<String>(key);
            if (string.IsNullOrEmpty(output))
                return null;
            return (T)JsonConvert.DeserializeObject(output, typeof(T));
        }

        public bool AddCache(string key, object obj, int minutes)
        {
            //return Instance.Store(StoreMode.Add, key, obj, System.DateTime.Now.AddMinutes(minutes));
            Instance.Remove(key);
            string output = JsonConvert.SerializeObject(obj);
            return Instance.Store(StoreMode.Add, key, output, System.DateTime.Now.AddMinutes(minutes));
        }

        public bool ClearCache(string key)
        {
            return Instance.Remove(key);
        }

        /// <summary>
        /// //
        /// </summary>
        private static CouchbaseManager _ShareInstance;
        public static CouchbaseManager ShareInstance()
        {
            if (_ShareInstance == null)
                _ShareInstance = new CouchbaseManager();
            return _ShareInstance;
        }
}

3、webconfig配置:

  <configSections>
    <section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
  </configSections>
<couchbase>
    <servers bucket="default" bucketPassword="">
      <add uri="http://localhost:8091/pools"/>
    </servers>
  </couchbase>

4、使用测试:

            string key = "test_vals";
            CacheManager.AddCache(key, "string1");
            Response.Write(CacheManager.GetCache<string>(key) + "<br/>");
            CacheManager.FlushAll();
            Response.Write(CacheManager.GetCache<string>(key) + "--after<br/>");

            CacheManager.AddCache(key, 2);
            Response.Write(CacheManager.GetCache<string>(key) + "<br/>");

            CacheManager.AddCache(key, System.DateTime.Now);

            Response.Write(CacheManager.GetCache<string>(key) + "<br/>");

            CacheManager.AddCache(key, true);
            Response.Write(CacheManager.GetCache<string>(key) + "<br/>");

            CacheManager.FlushAll();

            Photo u = _db.FindUnique<Photo>("1=1");

            Response.Write(u.Title);
            //Response.Flush();

            string k = "Photo_test";

            CacheManager.AddCache(k, u, 5);
            Photo val = CacheManager.GetCache<Photo>(k);

            if (val == null)
                WriteAjax("u is null");
            else
            {
                //Zi_User uu = val as Zi_User;
                WriteAjax("u is not null >> " + val.Title);
            }