Original:. NET Edition distributed cache memcached test instance

Source: Internet
Author: User
Tags foreach empty failover iis net string tostring win32
The following test distributed cache Memcached software, has been learning to pay attention to the large number of Web site cache is how to achieve, before looking at the memcached data, busy no time to really test, this article tests the distributed cache memcached environment is as follows: (two computers as a server)

First station:

Cpu:inter (R) Pentium (r) 4 CPU 2.8G

Memory: 1G

System: Windows 7

Iis:iis 7

ip:172.10.1.97

Environment: Local

Installation: Memcached 1.2.1 for Win32

Second Station:

Cpu:inter (R) Pentium (r) 4 CPU 3.0G

Memory: 2G

System: Windows Server 2003

Iis:iis 6

ip:172.10.1.236

Environment: Remote

Installation: Memcached 1.2.1 for Win32

The test program is deployed to the local environment (172.10.1.97) and the development tool VS2008. NET3.5

This article uses the memcached 1.2.1 for Win32 download address:

http://jehiah.cz/projects/memcached-win32/

More memcached version Encyclopedia please enter

Http://www.xueit.com/html/2009-11-12/32-1550931594781.html

OK, let's go through the steps to test the following:

First, to the download of the good memcached 1.2.1 to the C:\memcached directory, respectively, replicated to two servers.

Second, install the memcached service, at the command prompt input CD c:\memcached into the memcached directory, the following figure:

Then enter memcached-h carriage return, look at the help instructions, then enter memcached-d install carriage can automatically install memcached service, the following figure:

Install memcached Service Map

After installing the memcached service, enter memcached-d start carriage memcached Service, as shown in the following figure:

Start memcached service diagram

In 172.10.1.97 and 172.10.1.236 two PCs are installed to start memcached.

Third, download. NET Edition memcached client API components to write test programs.

This article uses memcacheddotnet to download the following addresses:

http://sourceforge.net/projects/memcacheddotnet/

After the download, put these files commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll to the bin directory (none), and then to the test project development Environment Reference me Mcached. ClientLibrary.dll, as shown below

Referencing Memcached.ClientLibrary.dll graphs

Test procedure:

The following are the referenced contents:
Using System;
Using System.Collections;
Using System.Text;

Must refer to memcached
Using Memcached.clientlibrary;

Namespace test
{
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
if (request["action"] = = "clear")
This.clear ();
Else
This.test ();
}
}

<summary>
Empty cache
</summary>
public void Clear ()
{
String[] Servers = {"172.10.1.97:11211", "172.10.1.236:11211"};

Initializing pool
Sockiopool pool = sockiopool.getinstance ();
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 ();
memcachedclient mc = new Memcached.ClientLibrary.MemcachedClient ();
Mc. EnableCompression = false;
Mc. Delete ("cache");
Mc. Delete ("Endcache");
Response.Write ("Empty cache succeeded");
}

<summary>
Test cache
</summary>
public void Test ()
{
Distributed MEMCACHEDF Service IP ports
String[] Servers = {"172.10.1.97:11211", "172.10.1.236:11211"};

Initializing pool
Sockiopool pool = sockiopool.getinstance ();
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 ();
Client instance
memcachedclient mc = new Memcached.ClientLibrary.MemcachedClient ();
Mc. EnableCompression = false;
StringBuilder sb = new StringBuilder ();
Write Cache
Sb. Appendline ("Write Cache test:");
Sb. Appendline ("<br>_______________________________________<br>");
if (MC. Keyexists ("cache"))
{
Sb. Appendline ("Cached cache already exists");
}
Else
{
Mc. Set ("Cache", "Write Cache Time:" DateTime.Now.ToString ());
Sb. Appendline ("Cache has been successfully written to cache");
}
Sb. Appendline ("<br>_______________________________________<br>");
Sb. Appendline ("read cache content as follows:<br>");
Sb. Appendline (MC. Get ("cache"). ToString ());

Test cache Expiration
Sb. Appendline ("<br>_______________________________________<br>");
if (MC. Keyexists ("Endcache"))
{
Sb. Appendline ("Cache Endcache already exists, expires as:" MC. Get ("Endcache"). ToString ());
}
Else
{
Mc. Set ("Endcache", DateTime.Now.AddMinutes (1). ToString (), DateTime.Now.AddMinutes (1));
Sb. Appendline (Cache updated write to Endcache, write Time: "DateTime.Now.ToString ()" Expires: "DateTime.Now.AddMinutes (1)." ToString ());
}

Profiling Cache State
Hashtable HT = MC. Stats ();
Sb. Appendline ("<br>_______________________________________<br>");
Sb. Appendline ("Memcached Stats:");
Sb. Appendline ("<br>_______________________________________<br>");
foreach (DictionaryEntry de in HT)
{
Hashtable info = (Hashtable) de. Value;
foreach (DictionaryEntry de2 in info)
{
Sb. Appendline (De2. Key.tostring () ":" De2. Value.tostring () "<br>");
}
}
Response.Write (sb.) ToString ());
}
}

Five, the operation looks at the effect:

Cache Effect Chart I run memcached-d in local 172.10.1.97

Stops to stop the memcached service, run the above program, as correct, indicating that the cache is also saved to the remote 172.10.1.236 the server. This is a simple way to implement distributed caching, and there is one more option to use the cache. NET with the application and cache, visit a large number of Web sites to achieve distributed caching has many benefits. If you have any questions, please correct me and give me another tutorial next time. Reprint please indicate the source of the article and the original link, thank you for your cooperation! Original: http://www.xueit.com/html/2009-11-12/21-932220455859.html



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.