Application of high performance cache system memcached in ASP.

Source: Internet
Author: User

First download the memcached under Windows platform and install it. After the installation is started memcached service, you can use the DOS command under CMD input, you can also be in the Computer Management, service->memcached-> start. To start the service.

This is followed by the introduction of the associated DLL in the project:
Commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll
Introduction of Memcached.ClientLibrary.dll in project references

Then you write the program, where you create an MVC program:
Create a class in the Models folder:

12345678910111213 [Serializable]publicclassVIP{public stringUserName { getset; }publicint? Vip { getset; }publicDateTime? VipEndDate { getset; }publicstringMail { getset; }publicstringQQ { getset; }}

If the label is not serializable, then the subsequent run program will error.

A Memcachedhelper class is then created to aid programming.

123456789101112131415161718192021222324252627 publicclassMemcachedHelper{publicstaticMemcachedClient mclient;staticMemcachedHelper(){string[] serverlist = newstring[] { "127.0.0.1:11211"};SockIOPool pool = SockIOPool.GetInstance("First");pool.SetServers(serverlist);pool.Initialize();mclient = new MemcachedClient();mclient.PoolName = "First";mclient.EnableCompression = false;}publicstaticboolset(stringkey, objectvalue, DateTime expiry){returnmclient.Set(key, value, expiry);}publicstaticobjectGet(stringkey){return mclient.Get(key);}}

The final is the controller inside the specific implementation:

123456789101112131415161718192021222324252627282930313233343536373839404142434445 publicclassEntityMemcachedController : Controller    {        //        // GET: /EntityMemcached/        /// <summary>        /// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力....        /// </summary>        /// <returns></returns>        public ActionResult Index()        {            varvipInfo = newList<VIP>{                newVIP{ UserName="张三", Vip=1, QQ="3123456", Mail="3123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(1) },                newVIP{ UserName="李四", Vip=1, QQ="4123456", Mail="4123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(2) },                newVIP{ UserName="王五", Vip=1, QQ="5123456", Mail="5123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(3) },                newVIP{ UserName="赵六", Vip=1, QQ="6123456", Mail="6123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(4) },                newVIP{ UserName="刘七", Vip=1, QQ="7123456", Mail="7123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(5) }            };                        if(Request.Cookies["_EntityMemcached"] == null)            {                stringsessionId = Guid.NewGuid().ToString();                Response.Cookies["_EntityMemcached"].Value = sessionId;                Response.Cookies["_EntityMemcached"].Expires = DateTime.Now.AddMinutes(1);//设置cookie过期时间                MemcachedHelper.set(sessionId, vipInfo, DateTime.Now.AddMinutes(1));//设置缓存过期时间                returnContent("Memcached分布式缓存设置成功!!!");            }            else            {                stringkey = Request.Cookies["_EntityMemcached"].Value.ToString();                 objectobj = MemcachedHelper.Get(key);                List<VIP> info = obj asList<VIP>;                if(info != null)                {                    returnView(info);                }            }             returnContent("若显示则有‘bug‘");        }

See the implementation:

Then exit and re-click "Implement Memcached Cache"

Application of high performance cache system memcached in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.