• 代码中实际运用memcached——mycode


    1、下载安装64位memcached.exe  下载地址:http://blog.couchbase.com/memcached-windows-64-bit-pre-release-available

    2、下载memcacheddotnet_clientlib,并将Commons.dll、ICSharpCode.SharpZipLib.dll、log4net.dll、Memcached.ClientLibrary.dll几个文件引用到项目中;

    3、完成配置文件,在<configuration>节点下添加节点<configSections>,在<configSection>外添加log4net配置,完整如下:

    <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="10"/>
    <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" />
    </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、实现代码:

    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("LogInfo");
    sock.SetServers(serverList);//添加服务器列表
    sock.InitConnections = 3;//设置连接池初始数目
    sock.MinConnections = 3;//设置最小连接数目
    sock.MaxConnections = 5;//设置最大连接数目
    sock.Failover = true; //如果为false,对所有创建的套接字关闭Nagle的算法。
    sock.SetWeights(new int[] { 1 });//各服务器之间负载
    sock.MaxIdle = 1000 * 60 * 60 * 6;//连接的最大空闲时间,下面设置为6个小时(单位ms),超过这个时间,连接会被释放掉
    sock.SocketTimeout = 1000 * 3;//通讯的超时时间3秒,.net版本没有实现
    sock.SocketConnectTimeout = 0;//socket连接的超时时间,下面设置表示连接不超时,即一直保持连接状态
    sock.Nagle = false;//是否对TCP/IP使用Nagle算法,.Net版本没有实现
    sock.MaintenanceSleep = 60;//维护线程的间隔激活时间,下面设置为60秒,设置为0表示不启用维护线程
    sock.MaxBusy = 1000 * 10;//socket单次任务的最大时间,超过这个时间socket会被强行中断掉(当前任务失败)

    sock.Initialize();

    mc = new MemcachedClient();
    mc.PoolName = "LogInfo";
    mc.EnableCompression = false;


    }
    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;
    }
    }
    }

  • 相关阅读:
    WEB
    Python
    Git
    JavaScript
    鸡汤
    面向对象
    Python
    Python
    MongoDB
    Oracle 11g 安装
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/4081861.html
Copyright © 2020-2023  润新知