zoukankan      html  css  js  c++  java
  • 在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);
                }
    作者:Peter Zhan
    博客园blog地址:http://www.cnblogs.com/zhanxp/
    本文版权归作者和博客园所有,欢迎转载,转载请注明出处
  • 相关阅读:
    Aurora 数据库支持多达五个跨区域只读副本
    Amazon RDS 的 Oracle 只读副本
    Amazon EC2 密钥对
    DynamoDB 读取请求单位和写入请求单位
    使用 EBS 优化的实例或 10 Gb 网络实例
    启动 LAMP 堆栈 Web 应用程序
    AWS 中的错误重试和指数退避 Error Retries and Exponential Backoff in AWS
    使用 Amazon S3 阻止公有访问
    路由表 Router Table
    使用MySQLAdmin工具查看QPS
  • 原文地址:https://www.cnblogs.com/zhanxp/p/2874250.html
Copyright © 2011-2022 走看看