This article was taken from: http://blog.csdn.net/dyllove98/article/details/9115947
Memcached Installation:
============================================================================
1 Extracting files to c:\memcached
2 command line input ' c:\memcached\memcached.exe-d install '
3 command line input ' c:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211
Note:
If the following error occurs
"Failed to install service or service already installed"
Workaround:
Install the administrator, first find out the original file of Cmd.exe
Right-click to run as Administrator, then OK (user under Win7).
If you are downloading a binary version, you can run it directly, and you can add parameters to set it up.
Common settings:
-P <num> Monitored ports
-L <ip_addr> connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U <username> Run as <username> (only valid when run as root)
-M <num> maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-C <num> Maximum number of simultaneous connections, default is 1024
-F <factor> block size growth factor, default is 1.25
-N <bytes> Minimum allocated space, key+value+flags default is 48
-H Display Help
memcached. NET client Calls:
===============================================================================
One
2) NET memcached Client Library
: https://sourceforge.net/projects/memcacheddotnet
Memcached Client class Library
Where the client class library includes the following DLLs:
Memcached.ClientLibrary.dll
ICSharpCode.SharpZipLib.dll
Log4net.dll
Put this three DLL into the bin directory, referencing Memcached.ClientLibrary.dll in the project
Introduce namespaces using Memcached.clientlibrary when used
Second, memcached use log4net, so we first configure log4net (this link can not do)
Locate the configsections node in Web. config and add the following
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
In addition to the configsections node, add the following:
[CSharp]View Plaincopyprint?
- <log4net>
- <appender name="Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
- <param name="File" value="logfiles/"/>
- <param name="Appendtofile" value="true"/>
- <param name="maxsizerollbackups" value="ten"/>
- <param name="staticlogfilename" value="false"/>
- <param name="Datepattern" value="yyyy-mm-dd". txt""/>
- <param name="Rollingstyle" value="Date"/>
- <layout type="log4net. Layout.patternlayout ">
- <param name="Conversionpattern" value="%d{yyyy-mm-dd HH:mm:ss} [%thread]%-5level%logger%NDC-%message% NewLine "/>
- </layout>
- </appender>
- <appender name="Consoleappender" type= "log4net. Appender.consoleappender ">
- <layout type="log4net. Layout.patternlayout ">
- <param name="Conversionpattern" value="%d{yyyy-mm-dd HH:mm:ss} [%thread]%-5level%logger%NDC-%message% NewLine "/>
- </layout>
- </appender>
- <root>
- <level value="All"/>
- <appender-ref ref="Rollinglogfileappender"/>
- <appender-ref ref="Consoleappender"/>
- </root>
- <logger name="Memcached.clientlibrary" >
- <level value="WARN"/>
- </logger>
- </log4net>
Third, initialize the Sockiopool
Sockiopool is a socket connection pool provided by the memcached client, which, in layman's words, is the object that exchanges data with the memcached server side. Sockiopool is initialized once when the application is started.
(usually written in the static too structure is OK)
Server-side List
[CSharp]View Plaincopyprint?
- String[] ServerList = { "127.0.0.1:11211"};
- Initialize Pool
- Sockiopool sock = Sockiopool.getinstance ();
- Sock. Setservers (serverlist); //Add server list
- Sock. Initconnections = 3; //Set initial number of connection pools
- Sock. Minconnections = 3; //Set minimum number of connections
- Sock. MaxConnections = 5; //Set the maximum number of connections
- Sock. socketconnecttimeout = 1000; //Set the socket timeout for the connection.
- Sock. Sockettimeout = 3000; //Set socket timeout read
- Sock. Maintenancesleep = 30; //Set the sleep time for the maintenance thread to run. If set to 0, then the maintenance thread will not start;
- Gets or sets the fault flag for the pool.
- If this flag is set to true then the socket connection fails,
- An attempt is made to return a socket from another server if it exists.
- If set to false, a socket is obtained if one exists. Otherwise, NULL is returned if it cannot connect to the requested server.
- Sock. Failover = true; //If False, the Nagle algorithm is closed on all created sockets.
- Sock. Nagle = false;
- Sock. Initialize ();
Iv. Use of memcached
[CSharp]View Plaincopyprint?
- memcachedclient mc = New Memcachedclient ();
- Mc. EnableCompression = true; //Whether to enable compressed data
- Mc. Set (Key,val); //Set key value
- Mc. Keyexists (key) //key is stored
- Mc. Get (key) //Get a key value
- Mc. Delete (key); //Delete key value
- How far a simple example
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using Memcached.clientlibrary;
- Namespace Cachedapp
- {
- public class Mcache
- {
- memcachedclient mc = New Memcachedclient (); Initializing a client
- static Mcache ()
- {
- string[] serverlist = { "172.18.30.33:11211"}; //Server list, can be multiple
- Sockiopool pool = sockiopool.getinstance ();
- //Modify the following parameters according to the actual situation
- 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 (); //Initialize the pool for memcache servers
- }
- public Object get (string key)
- {
- return MC. Get (key);
- }
- public Object set (string key,string val)
- {
- return MC. Set (Key,val);
- }
- }
- }
The actual use of memcached ——. NET in the code