Memcached Distributed Cache First Experience

Source: Internet
Author: User

1 memcached profile/download/install

Memcached is a high-performance, non-memory object caching system for dynamic Web applications to offload database load. Memcached is based on a hashmap that stores key/value pairs. Its clients can be written in any language and communicate through memcached protocol

    1. Download the Windows stable version of MEMCACHED-WIN64-1.4.4-14
    2. cmd command (win+r), switch to the specified directory of the unpacked package, and enter the command: memcached.exe-d install (corresponding to memcached.exe-d uninstall)
    3. Start memcached, its command: memcached.exe-d start (corresponds to close: memcached.exe-d stop)
    4. The memcached server is already installed and started (command help can be typed: Memcached.exe helper)

Memcached The default port is 11211

2 Create a new test project and use the Memcached client to implement the cache

    • Create a new MVC project: Memcachedtest, and NuGet package introduces enyimmemcached (open source, direct door: https://github.com/enyim/EnyimMemcached)
    • configuring in Web. config
<sectiongroupname= "Enyim.com">            < Sectionname= "Log"type= "Enyim.Caching.Configuration.LoggerSection, enyim.caching" />            < Sectionname= "memcached"type= "Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching" />        </sectiongroup><enyim.com><LogFactory= "Enyim.Caching.Log4NetFactory, Enyim.Caching.Log4NetAdapter" /><memcached><Servers><AddAddress= "192.168.14.172"Port= "11211" /><AddAddress= "192.168.14.251"Port= "11211" /></Servers></memcached></enyim.com>
View Code
    • Cache Test Code: (Note: The cache of the entity for the class must have the [Serializable] attribute added to the class )
Student luge =NewStudent () {UserId =1, UserName ="Luge" }; varLugebycache = client. Get<student> ("Luge"); if(Lugebycache = =NULL) {client. Store (Storemode.add,"Luge", luge); } Student Pigeon=NewStudent () {UserId =2, UserName ="Pigeon" }; varPigeonbycache = client. Get<student> ("Pigeon"); if(Pigeonbycache = =NULL) {client. Store (Storemode.add,"Pigeon", pigeon); }
View Code
    • Results

You can type stats to view the current memcached version and configuration information by Telnet 192.168.14.17 11211 (default port)/telnet 192.168.14.251 11211

For Telnet is not enabled, you can check the Telnet client in the Control Panel---Programs and functions--open and close window function

Listen for the cached values in each of the two servers separately, typing: get luge; Get pigeon; Displays the value of the respective cache

The example of such a distributed cache has been completed.

3 Synchronizing Session Information

The author uses memcachedproviders (which is to encapsulate the enyimmemcached to provide session synchronization in the ASP. The principle is to use enyimmemcached to rewrite the sessionstatestoreproviderbase, using only the system.web tag with a custom session configuration item, you can complete the replacement of the original session content

<sessionstatecookieless= "false"regenerateExpiredSessionId= "true"Mode= "Custom"Timeout= "1"CustomProvider= "Memcachedsessionprovider">            <providers>                <Addname= "Memcachedsessionprovider"type= "Memcachedproviders.session.sessionstateprovider,memcachedproviders"connectionStringName= "ApplicationServices"DbType= "None"writeExceptionsToEventLog= "false"   />            </providers>        </sessionstate>
View Code

After that, use the same Session as asp: session["UserName"]=luge; The test results are as follows:

4 references

Http://www.cnblogs.com/luminji/archive/2011/08/17/2143371.html

Http://www.cnblogs.com/edisonchou/p/3855517.html

Memcached Distributed Cache First Experience

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.