Configure Memcached with Custom ASP.Net Configuration Section

Download Sample Projects (222.32 kb)

In previous post Memcached in ASP.Net, I discussed at high level how you can use Memcached as a state server to store cache for application(s). If you look at the sample code for that post, you will notice that all the configuration for Memcached were hard coded including list of servers. But in real application you would want to have ability to configure Memcached from configuration file. In this post I will discuss writing a custom configuration section for your web.config file where you can provide all the settings for Memcached service.

Custom Configuration Section

To develop a custom configuration section for Memcached settings, you will need to create a class in a library assembly that derives from ConfigurationSection. Following code snippet shows that in my implementation I created a class MemcacheConfiguration.

public class MemcachedConfiguration : ConfigurationSection
{....}

Now we need to add properties for all the configuration settings that you would allow an admin to configure from the configuration file. Following code shows sample of few of the properties that I added to this class.

 [StringValidator(MinLength=1)]
 public string Servers
 {
    get
    {
       return this["servers"] as string;
    }
    set
    {
       this["servers"] = value;
    }
 }
 
 public bool Failover
 {
    get
    {
      return (bool)this["failover"];
    }
    set
    {
      this["failover"] = value;
    }
 }

Here is how the entry for this custom section looks like in web.config file.

<configuration>
  <configSections>
    <section name="MemcachedConfiguration" 
    type="ByteBlocks.MemcachedProvider.MemcachedConfiguration,ByteBlocks.MemcachedProvider" 
    allowDefinition="Everywhere"/>
  </configSections>
  
  <MemcachedConfiguration 
    servers="127.0.0.1:11211" 
    failover="true" 
    initConnections="2" 
    minConnections="2" 
    maxConnections="4" 
    socketTimeout="30">
   </MemcachedConfiguration>
</configuration>

You can get complete implementation of this custom configuration section from the attached sample. I will discuss all Memcached settings that are included in my custom configuration section that allows you to configure Memcached state server for optimal performance.

  • servers: This is list of comma separated server IP addresses along with the IP address on which Memcached service is configured to listen. Yes, you can have as many servers as you want in this list.
  • failover: This property is used to configure Memcached service to connect to alternate server if connection to current target fails. Set this property to false if you do not want to failover to other servers that you configured using servers setting. Leave it to true if you have a strong reason not to failover.
  • initConnections: This property is used to configure number of initial connections that memcached client should establish with service servers.
  • minConnections: This property is used to configure number of minimum connections to be maintained in the pool.
  • maxConnections: This property is used to set number of maximum connections to open in socket connection pool.
  • socketConnectionTimeout: This property is time in milliseconds for timeout for establishing connection with server that you have configured. This is just for establishing connection with server.
  • socketTimeout: This property is used to set timeout in milliseconds for connection to read data from the cache. This time is different from maxSocketConnectionTimeout.
  • maintenanceSleep: This property is used to set time in milliseconds for maintenance thread to run for socket pool clean up and clean up of dead objects etc. If you set this property value to 0 then clean up thread will not start and run.

Based on this custom configuration implementation, my cache provider code looks like as below.

private bool InitializeFromConfigurationFile()
{
   var config = ConfigurationManager.GetSection("MemcachedConfiguration") 
     as MemcachedConfiguration;
   if (null == config)
   {
      return false;
   }
   Servers = config.Servers.Split(",".ToCharArray());
   _pool.SetServers(Servers);
   _pool.InitConnections = config.InitConnections;
   _pool.MinConnections = config.MinConnections;
   _pool.MaxConnections = config.MaxConnections;

   _pool.SocketConnectTimeout = config.SocketConnectTimeout;
   _pool.SocketTimeout = config.SocketTimeout;
   _pool.MaintenanceSleep = config.MaintenanceSleep;
   _pool.Failover = config.Failover;

   _pool.Nagle = config.Nagle;
   return true;
}

You can download attached sample to see all this in action. If you have any question please drop me a line and I will try to get back to you as soon as I can.

comments powered by Disqus

Blog Tags