Click here to Skip to main content
15,894,539 members
Articles / Desktop Programming / Windows Forms

Implementing Distributed Caching using Memcached

Rate me:
Please Sign up or sign in to vote.
4.65/5 (21 votes)
26 Jul 2010Apache5 min read 133.4K   3.4K   55  
This article describes the implementation of distributed caching using Memcached, Including Memcached server Installation on Linux and Windows and using it in .NET.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Memcached.ClientLibrary;
using System.Collections;

namespace UsingMemcached.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        
        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();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MemcachedClient mc = new MemcachedClient();
            mc.EnableCompression = false;

            mc.Set(txtkey.Text, txtValue.Text);

            SockIOPool.GetInstance().Shutdown();
        }

        protected void btnStats_Click(object sender, EventArgs e)
        {
            // Get the Statistics of memcahed server
            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";
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer Macmillan Publishing
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions