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
- Download the Windows stable version of MEMCACHED-WIN64-1.4.4-14
- 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)
- Start memcached, its command: memcached.exe-d start (corresponds to close: memcached.exe-d stop)
- 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
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