The following is a test of the distributed cache memcached software. I have been learning how to implement the cache of a Large-traffic website. I have read the memcached documents and have no time to test it, this article tests the distributed cache memcached environment as follows: (two computers serve as servers)
First:
CPU: inter (r) Pentium (r) 4 CPU 2.8g
Memory: 1 GB
System: Windows 7
IIS: IIS 7
IP: 172.10.1.97
Environment: Local
Install: memcached 1.2.1 for Win32
Second:
CPU: inter (r) Pentium (r) 4 CPU 3.0g
Memory: 2 GB
Windows Server 2003
IIS: IIS 6
IP: 172.10.1.236
Environment: Remote
Install: memcached 1.2.1 for Win32
Test Program deployment to local environment (172.10.1.97), Development Tool vs2008. net3.5
This article uses memcached 1.2.1 for Win32:
Http://jehiah.cz/projects/memcached-win32/
For more memcached versions, go
Http://www.xueit.com/html/2009-11-12/32-1550931594781.html
Now, let's test it in steps:
First,
First, decompress the downloaded memcached 1.2.1 to the C: \ memcached directory and copy it to the two servers.
Second,
Install the memcached service and enter cd c: \ memcached at the command prompt to enter the memcached directory, for example:
Enter memcached-H and press enter to see help instructions. Then enter memcached-D install and press enter to automatically install the memcached service, for example:
Install memcached
After installing the memcached service, enter memcached-d start and press enter to start the memcached service, for example:
Start memcached service Diagram
Install and start memcached on both 172.10.1.97 and 172.10.1.236 computers.
Third,
Download the. Net version memcached client API component to write the test program.
This document uses memcacheddotnet as follows:
Http://sourceforge.net/projects/memcacheddotnet/
After downloading these files, write commons. DLL, icsharpcode. sharpziplib. DLL, log4net. DLL, memcached. clientlibrary. place the DLL in the bin directory (none of them work), and then reference memcached in the development environment of the test project. clientlibrary. DLL, such
Reference memcached. clientlibrary. dll
Fourth,
Test procedure:
View code
Using system;
Using system. collections;
Using system. text;
// Memcached must be referenced
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>
/// Clear the cache
/// </Summary>
Public void clear ()
{
String [] servers = {"172.10.1.97: 11211", "172.10.1.236: 11211 "};
// Initialize the pool
Sockiopool pool = sockiopool. getinstance ();
Pool. setservers (servers );
Pool. initconnections = 3;
Pool. minconnections = 3;
Pool. maxconnections = 5;
Pool. socketconnecttimeout = 1000;
Pool. Fig = 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 ("cache cleared successfully ");
}
/// <Summary>
/// Test Cache
/// </Summary>
Public void test ()
{
// Distribution of memcachedf service IP Ports
String [] servers = {"172.10.1.97: 11211", "172.10.1.236: 11211 "};
// Initialize the pool
Sockiopool pool = sockiopool. getinstance ();
Pool. setservers (servers );
Pool. initconnections = 3;
Pool. minconnections = 3;
Pool. maxconnections = 5;
Pool. socketconnecttimeout = 1000;
Pool. Fig = 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 ("cache already exists ");
}
Else
{
MC. Set ("cache", "Write cache time:" datetime. Now. tostring ());
SB. appendline ("the cache has been successfully written to the cache ");
}
SB. appendline ("<br> ___________________________________ <br> ");
SB. appendline ("read cache content: <br> ");
SB. appendline (MC. Get ("cache"). tostring ());
// Test cache expiration
SB. appendline ("<br> ___________________________________ <br> ");
If (MC. keyexists ("endcache "))
{
SB. appendline ("the cache endcache already exists, and the expiration time is:" MC. Get ("endcache"). tostring ());
}
Else
{
MC. Set ("endcache", datetime. Now. addminutes (1). tostring (), datetime. Now. addminutes (1 ));
SB. appendline ("cache updated and written to endcache, write time:" datetime. now. tostring () "expiration time:" datetime. now. addminutes (1 ). tostring ());
}
// Analyze the cache status
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 () ": & nbsp;" de2.value. tostring () "<br> ");
}
}
Response. Write (sb. tostring ());
}
}
}
Fifth,
Running results:
Cache
I run memcached-d stop on the local server 172.10.1.97 to stop the memcached service and run the above program. The same is true, indicating that the cache is also saved to the remote server 172.10.1.236.
In this way, the distributed cache can be implemented simply, and there is another option to use the cache, without the use of the. NET built-in application and cache. The distributed cache for websites with a large access volume has many advantages.
If you have any questions, please correct them and try other tutorials in the next period.
From: http://www.xueit.com/asp.net/show-4880-2.aspx