Using Memcachedclient in a Web application

Source: Internet
Author: User
Tags dot net log4net

This article from: http://www.cnblogs.com/yukaizhao/archive/2008/11/10/memcached_client_usage.html

I. Background:

In the development of web programs with large amounts of traffic, databases are often referred to as performance bottlenecks. In order to alleviate the pressure of the database, we frequently use the cache, and the ASP. NET comes with a strong, but there is a congenital shortage, it is in-process cache, when the site is load-balanced by multiple servers, when there is a slow data update, we can not simultaneously synchronize the updated data to two or more web On the server. Fortunately, the foreigner's Daniel developed the memcached distributed cache, its performance is extraordinary, memcached commonly used. NET Client class library has two respectively: Http://code.google.com/p/beitmemcached/,http ://sourceforge.net/projects/memcacheddotnet/, I personally recommend using the latter one.

For an introduction to memcached and a simple call in the console, refer to: memcached Introduction and practice of distributed cache system

Two. How to use

    1. To install the memcached server side, download the dot net version of the client, refer to the distributed Cache system memcached Introduction and Practice
    2. Using the memcached client to access the server in an ASP.
      Ideas:
      1) We start the site at the start of the client side of the memcached, stop when the site stops the client side, the release of the occupied port resources;
      This can only be done by placing the start and close on the Application_Start and application_end of global. We have declared a static instance in global to hold this client because we want to not regenerate instances of the Memcachedclient class during the entire site run. Use the properties Remotecache and isremotecacheavailable of a public to expose memcachedclient references and whether they are available.
      2) for ease of use we need to build a base class for the System.Web.UI.Page class Pagebase, which references memcached client Access class instances in this base class, and then the base class of all newly created ASPX pages inherits Pagebase.
      Here too, we just need to find global appinstance = HttpContext.Current.ApplicationInstance as global and then refer to the two properties exposed in global. It is important to note that referencing global in a program is done through HttpContext.Current.ApplicationInstance instead of HttpContext.Current.Applicatioin, which is just a collection of key-value pairs that are the httpa of the site. Examples of pplication

Please refer to the Global and Pagebase code:


Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.SessionState;

Using Memcached.clientlibrary;
Using Log4net;

Namespace Forum.ui
{
public class Global:System.Web.HttpApplication
{
protected static ILog Applog = Logmanager.getlogger (typeof (Global));

public override void Init ()
{
This. Error + = new EventHandler (Application_Error);
Base. Init ();
}


protected void Application_Error (object sender, EventArgs e)
{
#if! DEBUG
Exception ex = Server.GetLastError ();
Applog.error (ex. Message, ex);
Server.ClearError ();
Response.Redirect ("~/error/500.htm", true);
#endif
}

protected void Application_Start (object sender, EventArgs e)
{
Setupmemcachedclient ();
}

protected void Application_End (object sender, EventArgs e)
{
Shutdownmemecachedclient ();
}


#region Remotecache
private static Memcachedclient MC = null;
Private Const string memcached_instance_name = "Memcached-forum";


<summary>
Returns whether Memcachedclient is available
</summary>
public bool Isremotecacheavailable
{
Get
{
return MC! = NULL;
}
}


<summary>
External Access Portal
</summary>
Public Memcachedclient Remotecache
{
Get
{
return MC;
}
}

<summary>
To close a consumed TCP port resource
</summary>
private void Shutdownmemecachedclient ()
{
Sockiopool pool = sockiopool.getinstance (memcached_instance_name);
if (pool! = null) pool. Shutdown ();
}

<summary>
Start Memcachedclient, assign parameters to client
</summary>
private void Setupmemcachedclient ()
{
String memcachedservers = configurationmanager.appsettings["Memcachedservers"];
string[] Servers = Memcachedservers.split (';');
Sockiopool pool = sockiopool.getinstance (memcached_instance_name);
Pool. Setservers (servers);

Pool. Initconnections = 3;
Pool. Minconnections = 3;
Pool. MaxConnections = 5;

Pool. socketconnecttimeout = 1000;
Pool. Sockettimeout = 3000;

Pool. Maintenancesleep = 30;
Pool. Failover = true;

Pool. Nagle = false;
Pool. Initialize ();

MC = new Memcachedclient ();
Mc. poolname = Memcached_instance_name;
Mc. EnableCompression = false;
}
#endregion


}
}


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

}
}

Using Memcachedclient in a Web application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.