码迷,mamicode.com
首页 > 其他好文 > 详细

memcached使用说明

时间:2014-08-16 22:28:31      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   strong   

1、在服务器上注册服务

 bubuko.com,布布扣

 

 2、启动服务:services.msc

 

 bubuko.com,布布扣

 

 

3、客户端创建服务接口

   

 object Get(string key);

 

        List<string> GetKeys();

 

        List<object> GetValues();

 

        void Set(string key, object value);

 

        void Set(string key, object value, DateTime expiration);

 

        void Set(string key, object value, TimeSpan expiration);

 

        bool Exists(string key);

 

        void Remove(string key);

 

        void RemoveAll();

    }

 

 

4、客户端创建接口实现

引入Memcached.ClientLibrary,版本为1.0.0.0,还需要引入了log4net,版本为1.2.10.0,其它版本会报异常。

public class MemcachedCache : ICache

    {

        private static readonly SockIOPool pool = SockIOPool.GetInstance("Project");

        private static MemcachedClient client;

        private static ILog log = LogManager.GetLogger("memcached-log");

 

        static MemcachedCache()

        {

            var config = ConfigurationManager.AppSettings["memcached-server"];

            if (string.IsNullOrWhiteSpace(config))

            {

                log.Info("没有配置Memcached服务器IP!");

                return;

            }

 

            try

            {

                string[] serverList = config.Split(new char[] { ; }, StringSplitOptions.RemoveEmptyEntries);

                pool.SetServers(serverList);

                pool.Initialize();

 

                client = new MemcachedClient();

                client.PoolName = "Project";

                client.EnableCompression = false;

            }

            catch (Exception e)

            {

                log.Info(string.Format("Memcached初始化异常:{0}", e.Message));

                return;

            }

 

            log.Info("Memchached客户端初始化完成!");

        }

 

 

        public object Get(string key)

        {

            return client.Get(key);

        }

 

        public List<string> GetKeys()

        {

            var keys = new List<string>();

            //IDictionaryEnumerator de = client.ke();

            //while (de.MoveNext())

            //{

            //    keys.Add(de.Key.ToString());

            //}

 

            return keys;

        }

 

        public List<object> GetValues()

        {

            var values = new List<object>();

            //IDictionaryEnumerator de = client.GetEnumerator();

            //while (de.MoveNext())

            //{

            //    values.Add(de.Value);

            //}

 

            return values;

        }

 

        public void Set(string key, object value)

        {

            client.Set(key, value);

        }

 

        public void Set(string key, object value, DateTime expiration)

        {

            client.Set(key, value, expiration);

        }

 

        public void Set(string key, object value, TimeSpan expiration)

        {

            client.Set(key, value, DateTime.Now.Add(expiration));

        }

 

        public bool Exists(string key)

        {

            return client.KeyExists(key);

        }

 

        public void Remove(string key)

        {

            if (Exists(key))

            {

                client.Delete(key);

            }

        }

 

        public void RemoveAll()

        {

            foreach (var item in GetKeys())

            {

                Remove(item);

            }

        }

    }

 

 

5、客户端配置

 

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  </configSections>

 

 

  <appSettings>

   <!--服务器IP端口列表,这里的端口号是memcached默认监控的端口号-->

    <add key="memcached-server" value="192.0.0.1:11211;192.0.0.2:11211;"/>

  </appSettings>

 

   <log4net>

    <root>

      <level value="ALL" />

      <appender-ref ref="RollingLogFileAppender" />

    </root>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

      <param name="File" value="./log/aaa-" />

      <param name="AppendToFile" value="true" />

      <param name="MaxSizeRollBackups" value="10" />

      <param name="StaticLogFileName" value="false" />

      <param name="DatePattern" value="yyyyMMdd&quot;.log&quot;" />

      <param name="RollingStyle" value="Date" />

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="[%d] [%-5t] [%-5p] -- %m%n" />

      </layout>

    </appender>

  </log4net>

 

6、客户端写入获取缓存代码

  //Autofac获取实现

   IContainer container = ContainerInit.GetContainer();

 

            var cache = container.Resolve<ICache>();

            cache.Set("MyName", "James");

            cache.Set("MyAge", 30);

 

            var value = cache.Get("MyAge");

 

memcached使用说明,布布扣,bubuko.com

memcached使用说明

标签:style   blog   http   color   使用   os   io   strong   

原文地址:http://www.cnblogs.com/gossip/p/3892718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!