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.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.
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.
WCF Error in the ServiceModel client configuration section
How to plan CCSP Exam preparation
Develop a MongoDB pipeline to transform data into time buckets
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2025 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use