码迷,mamicode.com
首页 > Web开发 > 详细

在web应用程序中使用MemcachedClient

时间:2014-11-06 19:10:35      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   for   sp   

本文来自:http://www.cnblogs.com/yukaizhao/archive/2008/11/10/memcached_client_usage.html

 

一. 背景:

在大访问量的web程序开发中,数据库常常会称为性能的瓶颈。为了缓解数据库的压力,我们频繁的使用缓存,而asp.net自带的Cache很强大,但是有先天的不足,它是进程内的缓存,当站点由多台服务器负载均衡时,当缓存在有数据更新时,我们不能同时将更新后的数据同步到两台或多台web server上。所幸的是老外的大牛开发了memcached分布式缓存,它的性能非凡,memcached常用的.net的client类库有两个分别是:http://code.google.com/p/beitmemcached/http://sourceforge.net/projects/memcacheddotnet/,我个人推荐使用后一个。

有关memcached的介绍和在控制台中的简单调用请参考:分布式缓存系统Memcached简介与实践

二. 如何使用

  1. 安装memcached server端,下载dot net版本的客户端,请参考分布式缓存系统Memcached简介与实践
  2. 在asp.net项目中使用memcached客户端访问服务端
    思路:
    1) 我们在站点启动时即启动memcached的client端,在站点停止时停掉client端,释放占用的端口资源;
    这一点只要把启动和关闭放到Global的Application_Start和Application_End中执行,即可。由于整个网站的运行期间,我们都希望不需要再重新生成MemcachedClient类的实例,所以我们在Global中声明了一个静态的实例来存放此Client。并用一个public的属性RemoteCache和IsRemoteCacheAvailable来暴露MemcachedClient的引用和其是否可用。
    2) 为了方便使用我们需要建一个System.Web.UI.Page类的基类PageBase,在此基类中引用Memcached client的客户端访问类实例,然后新建的所有aspx页面的基类继承PageBase就可以了。
    这儿也很容已做到,我们只需找到 Global appInstance = HttpContext.Current.ApplicationInstance as Global;然后引用Global中暴露的两个属性即可。需要注意的是在程序中引用Global是通过HttpContext.Current.ApplicationInstance而不是HttpContext.Current.Applicatioin,后者只是一个键值对的集合,而前者是网站的HttpApplication的实例

请参考Global和PageBase的代码:

bubuko.com,布布扣
bubuko.com,布布扣using System;
bubuko.com,布布扣using System.Data;
bubuko.com,布布扣using System.Configuration;
bubuko.com,布布扣using System.Collections;
bubuko.com,布布扣using System.Web;
bubuko.com,布布扣using System.Web.Security;
bubuko.com,布布扣using System.Web.SessionState;
bubuko.com,布布扣
bubuko.com,布布扣using Memcached.ClientLibrary;
bubuko.com,布布扣using log4net;
bubuko.com,布布扣
bubuko.com,布布扣namespace Forum.UI
bubuko.com,布布扣{
bubuko.com,布布扣    public class Global : System.Web.HttpApplication
bubuko.com,布布扣    {
bubuko.com,布布扣        protected static ILog AppLog = LogManager.GetLogger(typeof(Global));
bubuko.com,布布扣
bubuko.com,布布扣        public override void Init()
bubuko.com,布布扣        {
bubuko.com,布布扣            this.Error += new EventHandler(Application_Error);
bubuko.com,布布扣            base.Init();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣        protected void Application_Error(object sender, EventArgs e)
bubuko.com,布布扣        {
bubuko.com,布布扣#if !DEBUG
bubuko.com,布布扣            Exception ex = Server.GetLastError();
bubuko.com,布布扣            AppLog.Error(ex.Message, ex);
bubuko.com,布布扣            Server.ClearError();
bubuko.com,布布扣            Response.Redirect("~/error/500.htm", true);
bubuko.com,布布扣#endif
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        protected void Application_Start(object sender, EventArgs e)
bubuko.com,布布扣        {
bubuko.com,布布扣            SetupMemcachedClient();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        protected void Application_End(object sender, EventArgs e)
bubuko.com,布布扣        {
bubuko.com,布布扣            ShutdownMemecachedClient();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣        #region RemoteCache
bubuko.com,布布扣        private static MemcachedClient mc = null;
bubuko.com,布布扣        private const string MEMCACHED_INSTANCE_NAME = "Memcached-Forum";
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 返回MemcachedClient是否可用
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public bool IsRemoteCacheAvailable
bubuko.com,布布扣        {
bubuko.com,布布扣            get
bubuko.com,布布扣            {
bubuko.com,布布扣                return mc != null;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 外部访问入口
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public MemcachedClient RemoteCache
bubuko.com,布布扣        {
bubuko.com,布布扣            get
bubuko.com,布布扣            {
bubuko.com,布布扣                return mc;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 关闭占用的tcp端口资源
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        private void ShutdownMemecachedClient()
bubuko.com,布布扣        {
bubuko.com,布布扣            SockIOPool pool = SockIOPool.GetInstance(MEMCACHED_INSTANCE_NAME);
bubuko.com,布布扣            if (pool != null) pool.Shutdown();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 启动memcachedClient,给client赋予指定参数
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        private void SetupMemcachedClient()
bubuko.com,布布扣        {
bubuko.com,布布扣            string memcachedServers = ConfigurationManager.AppSettings["memcachedServers"];
bubuko.com,布布扣            string[] servers = memcachedServers.Split(‘;‘);
bubuko.com,布布扣            SockIOPool pool = SockIOPool.GetInstance(MEMCACHED_INSTANCE_NAME);
bubuko.com,布布扣            pool.SetServers(servers);
bubuko.com,布布扣
bubuko.com,布布扣            pool.InitConnections = 3;
bubuko.com,布布扣            pool.MinConnections = 3;
bubuko.com,布布扣            pool.MaxConnections = 5;
bubuko.com,布布扣
bubuko.com,布布扣            pool.SocketConnectTimeout = 1000;
bubuko.com,布布扣            pool.SocketTimeout = 3000;
bubuko.com,布布扣
bubuko.com,布布扣            pool.MaintenanceSleep = 30;
bubuko.com,布布扣            pool.Failover = true;
bubuko.com,布布扣
bubuko.com,布布扣            pool.Nagle = false;
bubuko.com,布布扣            pool.Initialize();
bubuko.com,布布扣
bubuko.com,布布扣            mc = new MemcachedClient();
bubuko.com,布布扣            mc.PoolName = MEMCACHED_INSTANCE_NAME;
bubuko.com,布布扣            mc.EnableCompression = false;
bubuko.com,布布扣        }
bubuko.com,布布扣        #endregion
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣    }
bubuko.com,布布扣}

 

bubuko.com,布布扣
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using Memcached.ClientLibrary;
using System.Collections.Generic;

using log4net;
using Forum.Models;
using Forum.UI.Proxy;

namespace Forum.UI
{
    public class PageBase : Page
    {
        #region RemoteCache & IsRemoteCacheAvailable

        protected MemcachedClient RemoteCache
        {
            get
            {
                if (HttpContext.Current.ApplicationInstance == null) return null;
                Global appInstance = HttpContext.Current.ApplicationInstance as Global;
                if (appInstance == null) return null;

                return appInstance.RemoteCache;
            }
        }

        protected bool IsRemoteCacheAvailable
        {
            get
            {
                return RemoteCache != null;
            }
        }
        #endregion
        
    }
}

在web应用程序中使用MemcachedClient

标签:style   blog   http   io   color   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/zhouyunbaosujina/p/4079550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!