Install memcached:
========================================================== ==========================================
Note:
If the following error occurs:
"Failed to install service or service already installed"
Solution:
Administrator ID installation, first find the original file of cmd.exe
Right-click to run as administrator, and then OK (user under win7 ).
If the download is a binary version, you can directly run it. You can add parameters to set it.
Common settings:
-P <num> listening port
-L <ip_addr> connected IP address. The default value is local.
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached Service
-D install the memcached Service
-D uninstall memcached Service
-U <username> runs as <username> (valid only when running as root)
-M <num> maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C <num> maximum number of simultaneous connections. The default value is 1024.
-F <factor> block size growth factor. The default value is 1.25.
-N <bytes> minimum allocated space. The default value of key + value + flags is 48.
-H Show Help
Memcached. net client call:
========================================================== ========================================================
I,
Memcached client class library
The client class library includes the following DLL:
Memcached. ClientLibrary. dll
ICSharpCode. SharpZipLib. dll
Log4net. dll
Put the three DLL files in the Bin directory and reference Memcached. ClientLibrary. dll in the project.
Introduce the namespace using Memcached. ClientLibrary
Ii. memcached uses log4net, so we should configure log4net first (this step can not be done)
Find the configSections node in web. config and add the following content:
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
Add the following content to the configSections node:
<log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="LogFiles/"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <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>
Iii. initialize SockIOPool
SockIOPool is a socket connection pool provided by the Memcached client. Generally speaking, it is an object for exchanging data with the Memcached server. SockIOPool can be initialized once when the application is started.
(It can be written in the static structure)
// Server list
String [] serverlist = {"127.0.0.1: 11211"}; // initialize the pool SockIOPool sock = SockIOPool. getInstance (); sock. setServers (serverlist); // Add the Server LIST sock. initConnections = 3; // set the initial number of the Connection Pool sock. minConnections = 3; // set the minimum number of connections to sock. maxConnections = 5; // set the maximum number of connections sock. socketConnectTimeout = 1000; // set the connection socket timeout. Sock. SocketTimeout = 3000; // set socket timeout to read sock. MaintenanceSleep = 30; // set the sleep time of the maintenance thread. If it is set to 0, the maintenance thread will not start; // gets or sets the fault mark of the pool. // If this flag is set to true, the socket connection fails. // It will attempt to return a socket from another server if it exists. // If it is set to false, a socket is obtained if it exists. Otherwise, NULL is returned if it cannot connect to the requested server. Sock. Failover = true; // if it is false, disable the Nagle Algorithm for all created sockets. Sock. Nagle = false; sock. Initialize ();
4. Use Memcached
MemcachedClient mc = new MemcachedClient (); mc. enableCompression = true; // whether to enable mc for compressed data. set (key, val); // Set the key value mc. keyExists (key) // whether the key is stored in mc. get (key) // obtain a key value mc. delete (key); // Delete key value 5. A simple example of using System; using System. collections. generic; using System. linq; using System. web; using Memcached. clientLibrary; namespace CachedApp {public class MCache {MemcachedClient mc = new MemcachedClient (); // initialize a client static MCache () {string [] serverlist = {"172.18.30.33: 11211 "}; // server list, with multiple SockIOPool = SockIOPool. getInstance (); // modify the following parameter pool according to the actual situation. 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 );}}}