How to install and use MemCacheD / Distributed caching implimentation


Introduction: Caching is very important for performance improvement of applications. By caching data applications can reduce roundtrip to data storage.

There is lots of open source Cache Management application. MemCacheD is one of the widely used distributed caching system. Giants like Youtube, FaceBook are using MemCached for there cache management. In the following sections I will try to show how to install and use MemCacheD with ASP.Net application.

MemCacheD Installation: Download MemCacheD by click here. After completion of download follow the steps below to install MemCacheD:

  1. Installation is very simple just click on the downloaded msi.
  2. After that clicking on the couple of Next buttons will finish the installation.

Configure MemCacheD: From start menu–>MemCacheD Manager–>MemCacheD Manager. Pops up MemCacheD Manager Window.

Choose MemCacheD Manager from start menu.

Click on the Add Server button to add MemCacheD server (May be installed on local machine or on some other location) to MemCacheD manager. So MemCacheD manager can maintain multiple MemCacheD server.

Now provide the server information to the right side boxes. In the server name text box provide IP of the MemCacheD server. For our case as just installed in the local machine we will add the IP of local machine (127.0.0.1). Click on the apply button. And the server will be added.

Now add instance to the server by right clicking on the server left side tree node and choose Add New Instance from the context menu, it’s possible to create multiple instance of the same server and the cache is maintained instance wise separately by MemCacheD. To create instance provide the necessary information in the right side input boxes and click on the Apply button. as the snap below :

To add instance in a Server

The added server as well as the instances can be deleted or start/stop by just right clicking on the corresponding tree nodes. The snap above shows the servers as well as the instances as start state.

To see the caching status of the instances click on the Status tab, it shows the no of cached items, no of times items read/write and other information.

Monitor caching status of different Instances

So the MemCacheD is installed and configured and ready for use.

Using MemCacheD : Create new web application with name MemcachedWebClient from visual studio. Add a new page GetSetCacheData.aspx on the web application with the following html content.

<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Site.Master” AutoEventWireup=”true” CodeBehind=”GetSetCacheData.aspx.cs” Inherits=”MemcachedWebClient.GetSetCacheData” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”HeadContent” runat=”server”>

</asp:Content>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>

<div>

<div style=”float:left;padding-right:20px”>

<div>Cached Key</div>

<div> <asp:TextBox runat=”server” id=”txtCacheKey”>

</asp:TextBox>

</div>

</div>

<div>

<div>CacheValue</div>

<div> <asp:TextBox runat=”server” id=”txtCacheValue” Width=”301px”></asp:TextBox>

</div>

</div>

<div style=”padding-top:20px;padding-bottom:20px”>

<asp:Button ID=”btnSetCacheValue” runat=”server” Text=”SetCacheValue” />

</div>

</div>

<div style=”float:none”>

<div style=”float:left;padding-right:20px”>

<div>KeyToRetrieveCache</div>

<div> <asp:TextBox runat=”server” id=”txtKeyToRetrieveCache”>

</asp:TextBox>

</div>

</div>

<div>

<div>CacheValue</div>

<div> <asp:TextBox runat=”server” id=”txtRetrievedValue” Width=”306px”></asp:TextBox>

</div>

</div>

<div style=”padding-top:20px;padding-bottom:20px”>

<asp:Button ID=”btnGetCacheValue” runat=”server” Text=”GetCacheValue” />

</div>

</div>

</asp:Content>

Viewing the page on browse looks like below:

Added page to test caching

The page is intended to set cache by providing cache key/value and by clicking on the SetCacheValue button.

And retrieves the cache value by providing the cache key on the KeyToRetrieveCache and clicking on the GetCacheValue button.

To implement these functionality add the button click events for GetCacheValue and SetCacheValue. Change the provided html code by the following to add the button events.

For GetCacheValue :

<asp:Button ID=”btnGetCacheValue” runat=”server” Text=”GetCacheValue”

onclick=”btnGetCacheValue_Click” />

For SetCacheValue :

<asp:Button ID=”btnSetCacheValue” runat=”server” Text=”SetCacheValue”

onclick=”btnSetCacheValue_Click” />

Add the following code in the GetSetCacheData.aspx.cs :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Memcached.ClientLibrary;

using System.Collections;

namespace MemcachedWebClient

{

public partial class GetSetCacheData : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

/* The list of the servers to which cached have to add*/

string[] serverlist = { “127.0.0.1:11212” };

SockIOPool oSockIOPool = SockIOPool.GetInstance();

oSockIOPool.SetServers(serverlist);

oSockIOPool.InitConnections = 3;

oSockIOPool.MinConnections = 3;

oSockIOPool.MaxConnections = 5;

oSockIOPool.SocketConnectTimeout = 1000;

oSockIOPool.SocketTimeout = 3000;

oSockIOPool.MaintenanceSleep = 30;

oSockIOPool.Failover = true;

oSockIOPool.Nagle = false;

oSockIOPool.Initialize();

}

}

protected void btnSetCacheValue_Click(object sender, EventArgs e)

{

MemcachedClient oMemcachedClient = new MemcachedClient();

oMemcachedClient.EnableCompression = false;

oMemcachedClient.Set(txtCacheKey.Text, txtCacheValue.Text);

}

protected void btnGetCacheValue_Click(object sender, EventArgs e)

{

MemcachedClient oMemcachedClient = new MemcachedClient();

oMemcachedClient.EnableCompression = false;

object retrievedvalue = oMemcachedClient.Get(txtKeyToRetrieveCache.Text);

txtRetrievedValue.Text = retrievedvalue != null ? retrievedvalue.ToString() : string.Empty;

}

}

}

Now view the added page in browser and set cache/ Get cache and follow the Status tab from the MemCacheD Manger, it reflects the no of cache added and no of times it read.

Server status after doing some caching

Advertisements

3 Responses to How to install and use MemCacheD / Distributed caching implimentation

  1. muyassir says:

    where is located the “MamCached.ClientLibrary”;

    • Faruk says:

      Sorry I missed that in the description, By the way the dll’s are available in http://sourceforge.net/projects/memcacheddotnet/

      The specific path of the dll is : \memcacheddotnet_clientlib-1.1.5\memcacheddotnet\trunk\clientlib\src\clientlib\bin\2.0\Release from the downloaded folder structure.

  2. Pingback: Memcached for Windows | Making Things Easier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: