Tools Used in this Article - Need to be Downloaded
- Memcached Manager - This application enables us to manage
MemCacheD
in our Windows server environment including remote service management. - Memcached .NET client Library - C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers.
- Environment. Visual Studio 2010, Ubuntu 9.10
Contents
- Introduction
- Installing and configuring Memcached on Linux
- Installing and configuring Memcached on Windows using Manager
- Configuring Memcached server with Command line
- Using Memcached in web app and in win form
A distributed cache as a concept and as a best practice is gaining more popularity. Only a few years ago, very few people in the .NET space knew about it, although the Java community has been ahead of .NET in this area. With the explosive growth in application transactions, databases are stressed beyond their limits, and distributed caching is now accepted as a vital part of any scalable application architecture.
There are a couple of In-Memory distributed cache engines such as Velocity, NCache, ScaleOut and Memcached. Each one of these has its advantages and disadvantages in term of scalability, security and licensing the details of each can be explored on the internet. In this article, we will discuss Memcached.
Mecached is open source easily deployable distributed in-memory, cross application and cross Platform Engine meaning the same server can be accessed from a variety of languages - PHP, Java and .NET and from Windows, Linux, etc.
In this article, we will be Installing Memcached server on Linux and Windows and after that, we will be accessing both servers from .NET applications (Web and desktop) using the Memcached client library.
In this article, we will be installing Memcached on Ubuntu 9.10, but you can install on any Linux platform.
Memcached uses Libevent, so you have to compile and install Libevent first. Login to Ubuntu and open terminal and run the following commands and run the following commands on Ubuntu terminal:
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar xzf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure && make && sudo make install
System might prompt for the password, so enter the password to continue. Now run the following commands to compile the Memcached server:
wget http://memcached.googlecode.com/files/memcached-1.4.1.tar.gz
tar xzf memcached-1.4.1.tar.gz
cd memcached-1.4.1
./configure && make && sudo make install
After installation completes, two folders “libevent-1.4.14b-stable” and “memcached-1.4.1” will be created as shown below:
Now you can run Memcached either by double clicking from its location or through terminal as shown below. While running from terminal, you can configure different options listening address and port:
Memcached –L 192.168.1.106 –P 56789
To make sure Memcached is running, type the following command to check the status:
sudo netstat –plntu
Checking with system monitor that Memcached is working:
Memcached is running and listening on port 56789.
Now we will be checking the IP address Ubuntu is running on so that we can use that in our client.
Click system -> Administration -> Networking Tools.
In this step, we will be installing and configuring the Memcached server on Windows. To do this, download the Memcached manager from the link given at the beginning of article. Run that tool as administrator.
Add server name “localhost
” and click add server. Refer to the below screen:
And click add instance button, configure the settings. Refer to the below screen:
Make sure that memcached is started and running, We can use resource monitor to see the port its running on:
Memcached is running and listening on port 12345.
Configuring Memcached with command line is the same as we did for Ubuntu, e.g.,
Memcached –L 192.168.1.101 –P 12345
But here is the list of some more commands that can be useful:
- -u <user> : run as user if started as root
- -m <num> : maximum <num> MB memory to use for items
- If more than available RAM - will use swap
- Don’t forget 4G limit on 32 bit machines
- -d : Run as a daemon
- -l <ip_addr> : Listen on <ip_addr>; default to INDRR_ANY
- -p <num> : port
- -s <file> : unix socket (disables network support)
- -c <num> : max simultaneous connections
- -v : verbose
- -vv : (2 v’s) very verbose
- -P <file> : PID file (used with -d)
- -t <threads> : number of threads to use to process incoming requests. Only valid if compiled with thread support enabled. Only useful up to number of CPUs
In this step, we will be accessing Memcached from both Linux and Windows. To do so, first we need to download the Memcached .NET client Library from the link given at the beginning of article.
Create a solution and add web and desktop projects as shown below:
Add reference to the Memcached.ClientLibrary.dll in both projects. This assembly can be located at the following path:
memcacheddotnet_clientlib-1.1.5\
memcacheddotnet\trunk\clientlib\src\clientlib\bin\2.0\Release
To test the functionality of Memcached, we will insert some value through web application and will be retrieving that from desktop application.
Web application contains text boxes for server, key, value and a button insert into Memcached as shown below:
Add the following code snippet in Page_Load()
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] serverlist = { txtServer.Text };
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);
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();
}
}
Add the following code into Button1_Click()
handler:
protected void Button1_Click(object sender, EventArgs e)
{
MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = false;
mc.Set(txtkey.Text, txtValue.Text);
}
Add the following code into btnStats_Click()
handler:
protected void btnStats_Click(object sender, EventArgs e)
{
MemcachedClient mc = new MemcachedClient();
IDictionary stats = mc.Stats();
foreach (string key1 in stats.Keys)
{
lblStats.Text += key1;
Hashtable values = (Hashtable)stats[key1];
foreach (string key2 in values.Keys)
{
lblStats.Text += key2 + ":" + values[key2] + "\r\n";
}
lblStats.Text += "\r\n";
}
}
Creating desktop application contains text boxes for server and Key and button “Get Value“:
Write the following code snippet into btnGetValue_Click()
event handler:
private void btnGetValue_Click(object sender, EventArgs e)
{
string[] serverlist = { txtServer.Text };
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);
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 MemcachedClient();
mc.EnableCompression = false;
string value = (string)mc.Get(txtKey.Text);
MessageBox.Show(value);
}
Now test the Memcached by changing servers and inserting different values.
Now change the server to Windows Memcached which is running on 192.168.1.102:12345 as shown below
And we can see in Memcached statistics in Manger, Item Count is 1.
Conclusion
We have successfully Installed memcached server on Windows and Linux environment and used those in our ASP.NET and C# projects.
I hope this project will be useful for developers who want to implement scalable solutions with distributed caching.
I tried my best to describe each step with a screen shot. I hope you've enjoyed this article. If you like this article, please let me know :). If you have any questions, please feel free to contact me at fayaziiui@gmail.com.
History
- 26th July, 2010: Initial post