Memcached1.4.13(32、64位windows)在.Net中使用.zip

创建自动启动windows服务命令:

sc create "Memcached Server" binpath= "E:\Memcached\memcached.exe -d runservice -l 127.0.0.1 -m 512 -c 2048 -p 11011" DisplayName= "Memcached Server" start= auto  


启动服务命令(先关闭再启动):

echo off
net stop "memcached Server"
net start "memcached Server"
echo 服务已启动!
pause



封装类代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;

namespace Tools.Common
{
    public class MemcachedHelper:IDisposable
    {
        private MemcachedClient _mc =null; 
        protected MemcachedClient mc
        {
            get
            {
                if(_mc==null) _mc=new MemcachedClient();//初始化一个客户端 
                return _mc;
            }
        }
        /// <summary>
        /// 如果默认不是本地服务,可以额外指定memcached服务器地址
        /// </summary>
        public static string[] serverList
        {
            get;
            set;
        }
        private static MemcachedHelper _instance = null;
        /// <summary>
        /// 单例实例对象,外部只能通过MemcachedHelper.instance使用memcached
        /// </summary>
        public static MemcachedHelper instance
        {
            get
            {
                if (_instance == null)
                {
                    if (serverList != null && serverList.Length > 0)
                        _instance = new MemcachedHelper(serverList);
                    else _instance = new MemcachedHelper();
                }
                
                return _instance;
            }
        }
        SockIOPool pool;
        private void start(params string[]servers)
        {
            string[] serverlist;
            if (servers == null || servers.Length < 1)
            {
                serverlist = new string[] { "127.0.0.1:11011" }; //服务器列表,可多个
            }
            else
            {
                serverlist=servers;
            }
            pool = SockIOPool.GetInstance();

            //根据实际情况修改下面参数
            pool.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      
        }
        private MemcachedHelper(string[] servers)
        {
            start(servers);
        }
        private MemcachedHelper()
        {
            start();
        }
        ~MemcachedHelper()
        {
            if (pool != null) pool.Shutdown();
        }

        public object Get(string key)
        {
            return mc.Get(key);
        }
        /// <summary>
        /// 存入什么类型,取出时指定正确的类型才不会出错
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            object data=mc.Get(key);
            if (data is T) return (T)data;
            else return default(T);
        }
        public bool Delete(string key)
        {
            return mc.Delete(key);
        }
        public bool Set(string key,object data)
        {
           return mc.Set(key, data);
        }
        //在应用程序退出之前,调用Dispose释放memcached客户端连接
        public void Dispose()
        {
            if (pool != null) pool.Shutdown();
        }
    }
}

调用示例:

            MemcachedHelper.instance.Set("t2","测试memcached 1.4.13");
            MessageBox.Show(MemcachedHelper.instance.Get<string>("t2"));



运行结果:

Memcached1.4.13(32、64位windows)在.Net中使用.zip_第1张图片



相关文件下载:http://download.csdn.net/detail/pukuimin1226/6869519


你可能感兴趣的:(Memcached1.4.13(32、64位windows)在.Net中使用.zip)