The actual use of Memcached--mycode in code

Source: Internet
Author: User
Tags set socket static class log4net

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&quot;. txt&quot; " />
<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

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.