HOW TO USE MEMCACHE

aditya kumar Patel

Total Post:254

Points:1810
Posted by  aditya kumar Patel
Mvc 
 875  View(s)
Ratings:
Rate this:
how to use memcache
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to use memcache

    1. Install Memcached server in Windows

    The latest Memcached version is 1.4.5, but the application doesn't support "-d install" parameter to install itself as Windows service.

    So right now the 1.4.4 is still the option to go under Windows. Get a package from memcached 1.4.4 for Windows 32-bit from http://downloads.northscale.com/memcached-win32-1.4.4-14.zip.

    Unzipped to a folder, then run the following command to install as win services
    memcached.exe -d install

    Start the server from the Microsoft Management Console or by running one of the following commands: 
    memcached.exe -d start

    2. Check Memcached installation

    You can telnet to the default port 11211 to check the status:
    telnel localhost 11211

    Then use the command stats to check status
    stats
    flush_all





    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Newtonsoft.Json;
    using Enyim.Caching;
    using Enyim.Caching.Memcached;

    namespace MemCching
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                List<User> lst = new List<MemCching.User>();
                List<User> lst2 = new List<MemCching.User>();
                User ob = new MemCching.User();
                ob.UserID = "1";
                ob.UserName = "aditya";
                lst.Add(ob);

                JsonSerializerSettings s = new JsonSerializerSettings();
                s.NullValueHandling = NullValueHandling.Ignore;
                s.MissingMemberHandling = MissingMemberHandling.Ignore;
                s.DefaultValueHandling = DefaultValueHandling.Ignore;
               
                bool b = false;
                bool c = false;
                string varCache = "SpibayProduct";
                MemcachedClient Cache = new MemcachedClient();
                string stringList = JsonConvert.SerializeObject(lst, Formatting.None, s);
                // a = Cache.Remove(varCache)
                if (Cache.Get(varCache) == null == true)
                {
                    b = Cache.Store(StoreMode.Add, varCache, stringList, DateTime.Now.AddDays(30));
                }
                else
                {
                    c = Cache.Store(StoreMode.Replace, varCache, stringList, DateTime.Now.AddDays(30));
                }

               
      if (Cache.Get(varCache) == null == false)
                {
                    var str = Cache.Get(varCache);
                    lst2 = JsonConvert.DeserializeObject<List<User>>(str.ToString());
                }
                
            }

             
        }

        public class User
        {
            public string UserID {get;set;}
            public string UserName {get;set;}
        }
    }


    <configSections>
        <sectionGroup name="enyim.com">
          <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection,Enyim.Caching"/>
        </sectionGroup>
      </configSections>

     <enyim.com>
        <memcached protocol="Binary">
          <servers>
            <add address="127.0.0.1" port="11211"/>
          </servers>
          <keyTransformer type="Enyim.Caching.Memcached.TigerHashKeyTransformer, Enyim.Caching" />
        </memcached>
      </enyim.com>

      Modified On Oct-31-2015 01:04:39 PM
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to use memcache

    This is the best way to use memcache!!

      Modified On Dec-15-2015 09:50:25 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!