1, download the installation number Memcached.exe
2, download Memcacheddotnet_clientlib, and will Commons.dll, ICSharpCode.SharpZipLib.dll, Log4net.dll, Memcached.ClientLibrary.dll several file references to the project;
3. Complete the configuration file, add the node <configsections> under the <configuration> node, and add the log4net configuration outside the <configSection>, complete as follows:
<configuration>
<!--<appSettings>
<add key= "ServerIP" value= "127.0.0.1"/>
<add key= "ServerPort" value= "11011"/>
</appSettings>-->
<!--<startup>
<supportedruntime version= "v4.0" sku= ". netframework,version=v4.5 "/>
</startup>-->
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
</configSections>
<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"/& Gt
</layout>
</appender>
<root>
<level value= "All"/>
<appender-ref ref= "Rollinglogfileappender"/>
<appender-ref ref= "Consoleappender"/>
</root>
<logger name= "Memcached.clientlibrary" >
<level value= "WARN"/>
</logger>
</log4net>
</configuration>
4. Implementation code:
Using Memcached.clientlibrary;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Memcached
{
public static Class Memcachedhelper
{
private static Memcachedclient MC;
Static Memcachedhelper ()
{
Try
{
string[] serverlist = new string[] {"127.0.0.1:11211"};
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 maximum number of connections
Sock. Socketconnecttimeout = 1000;//Sets the socket timeout for the connection.
Sock. Sockettimeout = 3000;//Set Socket timeout read
Sock. Maintenancesleep = 30;//Sets the sleep time that the maintenance thread runs. If set to 0, then the maintenance thread will not start;
Sock. Failover = true; If False, the Nagle algorithm is closed for all created sockets.
Sock. Nagle = false;
Sock. Initialize ();
MC = new Memcachedclient ();
}
catch (Exception ex)
{
var exception = ex. Message + ex. StackTrace;
}
}
public static bool SetValue (string key, object value, DateTime expiry)
{
BOOL Issuccess=false;
Try
{
Issuccess = MC. Set (key, value, expiry);
}
catch (Exception ex)
{
var excepion = ex. Message + ex. StackTrace;
}
return issuccess;
}
public static object GetValue (String key)
{
var result = Mc. Get (key);
return result;
}
}
}
The actual use of Memcached--mycode in code