First round playing with Memcached

Posted by Shaun on Geeks with Blogs See other posts from Geeks with Blogs or by Shaun
Published on Wed, 07 Apr 2010 02:55:02 GMT Indexed on 2010/04/07 4:03 UTC
Read the original article Hit count: 852

Filed under:

To be honest I have not been very interested in the caching before I’m going to a project which would be using the multi-site deployment and high connection and concurrency and very sensitive to the user experience. That means we must cache the output data for better performance.

After looked for the Internet I finally focused on the Memcached. What’s the Memcached? I think the description on its main site gives us a very good and simple explanation.

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

The original Memcached was built on *nix system are is being widely used in the PHP world. Although it’s not a problem to use the Memcached installed on *nix system there are some windows version available fortunately. Since we are WISC (Windows – IIS – SQL Server – C#, which on the opposite of LAMP) it would be much easier for us to use the Memcached on Windows rather than *nix.

I’m using the Memcached Win X64 version provided by NorthScale. There are also the x86 version and other operation system version.

 

Install Memcached

Unpack the Memcached file to a folder on the machine you want it to be installed, we can see that there are only 3 files and the main file should be the “memcached.exe”. Memcached would be run on the server as a service. To install the service just open a command windows and navigate to the folder which contains the “memcached.exe”, let’s say “C:\Memcached\”, and then type “memcached.exe -d install”.

If you are using Windows Vista and Windows 7 system please be execute the command through the administrator role. Right-click the command item in the start menu and use “Run as Administrator”, otherwise the Memcached would not be able to be installed successfully.

Once installed successful we can type “memcached.exe -d start” to launch the service. Now it’s ready to be used. The default port of Memcached is 11211 but you can change it through the command argument. You can find the help by typing “memcached -h”.

image

 

Using Memcached

Memcahed has many good and ready-to-use providers for vary program language. After compared and reviewed I chose the Memcached Providers. It’s built based on another 3rd party Memcached client named enyim.com Memcached Client. The Memcached Providers is very simple to set/get the cached objects through the Memcached servers and easy to be configured through the application configuration file (aka web.config and app.config).

Let’s create a console application for the demonstration and add the 3 DLL files from the package of the Memcached Providers to the project reference.

image

Then we need to add the configuration for the Memcached server. Create an App.config file and firstly add the section on top of it. Here we need three sections: the section for Memcached Providers, for enyim.com Memcached client and the log4net.

   1: <configSections>
   2:   <section name="cacheProvider" 
   3:            type="MemcachedProviders.Cache.CacheProviderSection,  MemcachedProviders" 
   4:            allowDefinition="MachineToApplication" 
   5:            restartOnExternalChanges="true"/>
   6:   <sectionGroup name="enyim.com">
   7:     <section name="memcached" 
   8:              type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
   9:     </sectionGroup>
  10:   <section name="log4net" 
  11:            type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  12: </configSections>

Then we will add the configuration for 3 of them in the App.config file. The Memcached server information would be defined under the enyim.com section since it will be responsible for connect to the Memcached server. Assuming I installed the Memcached on two servers with the default port, the configuration would be like this.

   1: <enyim.com>
   2:   <memcached>
   3:     <servers>
   4:       <!-- put your own server(s) here-->
   5:       <add address="192.168.0.149" port="11211"/>
   6:       <add address="10.10.20.67" port="11211"/>
   7:     </servers>
   8:     <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/>
   9:   </memcached>
  10: </enyim.com>

Memcached supports the multi-deployment which means you can install the Memcached on the servers as many as you need. The protocol of the Memcached responsible for routing the cached objects into the proper server. So it’s very easy to scale-out your system by Memcached.

And then define the Memcached Providers configuration. The defaultExpireTime indicates how long the objected cached in the Memcached would be expired, the default value is 2000 ms.

   1: <cacheProvider defaultProvider="MemcachedCacheProvider">
   2:   <providers>
   3:     <add name="MemcachedCacheProvider" 
   4:          type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders" 
   5:          keySuffix="_MySuffix_" 
   6:          defaultExpireTime="2000"/>
   7:   </providers>
   8: </cacheProvider>

The last configuration would be the log4net.

   1: <log4net>
   2:   <!-- Define some output appenders -->
   3:   <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
   4:     <layout type="log4net.Layout.PatternLayout">
   5:       <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
   6:     </layout>
   7:   </appender>
   8:   <!--<threshold value="OFF" />-->
   9:   <!-- Setup the root category, add the appenders and set the default priority -->
  10:   <root>
  11:     <priority value="WARN"/>
  12:     <appender-ref ref="ConsoleAppender">
  13:       <filter type="log4net.Filter.LevelRangeFilter">
  14:         <levelMin value="WARN"/>
  15:         <levelMax value="FATAL"/>
  16:       </filter>
  17:     </appender-ref>
  18:   </root>
  19: </log4net>

 

Get, Set and Remove the Cached Objects

Once we finished the configuration it would be very simple to consume the Memcached servers. The Memcached Providers gives us a static class named DistCache that can be used to operate the Memcached servers.

  • Get<T>: Retrieve the cached object from the Memcached servers. If failed it will return null or the default value.
  • Add: Add an object with a unique key into the Memcached servers.

Assuming that we have an operation that retrieve the email from the name which is time consuming. This is the operation that should be cached. The method would be like this. I utilized Thread.Sleep to simulate the long-time operation.

   1: static string GetEmailByNameSlowly(string name)
   2: {
   3:     Thread.Sleep(2000);
   4:     return name + "@ethos.com.cn";
   5: }

Then in the real retrieving method we will firstly check whether the name, email information had been searched previously and cached. If yes we will just return them from the Memcached, otherwise we will invoke the slowly method to retrieve it and then cached.

   1: static string GetEmailByName(string name)
   2: {
   3:     var email = DistCache.Get<string>(name);
   4:     if (string.IsNullOrEmpty(email))
   5:     {
   6:         Console.WriteLine("==> The name/email not be in memcached so need slow loading. (name = {0})==>", name);
   7:         email = GetEmailByNameSlowly(name);
   8:         DistCache.Add(name, email);
   9:     }
  10:     else
  11:     {
  12:         Console.WriteLine("==> The name/email had been in memcached. (name = {0})==>", name);
  13:     }
  14:     return email;
  15: }

Finally let’s finished the calling method and execute.

   1: static void Main(string[] args)
   2: {
   3:     var name = string.Empty;
   4:     while (name != "q")
   5:     {
   6:         Console.Write("==> Please enter the name to find the email: ");
   7:         name = Console.ReadLine();
   8:  
   9:         var email = GetEmailByName(name);
  10:         Console.WriteLine("==> The email of {0} is {1}.", name, email);
  11:     }
  12: }

The first time I entered “ziyanxu” it takes about 2 seconds to get the email since there’s nothing cached. But the next time I entered “ziyanxu” it returned very quickly from the Memcached.

image

 

Summary

In this post I explained a bit on why we need cache, what’s Memcached and how to use it through the C# application. The example is fairly simple but hopefully demonstrated on how to use it. Memcached is very easy and simple to be used since it gives you the full opportunity to consider what, when and how to cache the objects. And when using Memcached you don’t need to consider the cache servers. The Memcached would be like a huge object pool in front of you.

The next step I’m thinking now are:

  • What kind of data should be cached? And how to determined the key?
  • How to implement the cache as a layer on top of the business layer so that the application will not notice that the cache is there.
  • How to implement the cache by AOP so that the business logic no need to consider the cache.

I will investigate on them in the future and will share my thoughts and results.

 

Hope this helps,

Shaun

All documents and related graphics, codes are provided "AS IS" without warranty of any kind.
Copyright © Shaun Ziyan Xu. This work is licensed under the Creative Commons License.

© Geeks with Blogs or respective owner