private String[] serverlist = { "127.0.0.1:11211" }; //要改为配置文件读取

        #endregion Fields

        #region Constructors

        private MemcachedClientSatat(string poolName)
        {
            string memchached = ConfigurationSettings.AppSettings["memchached"];
            string msgnumstr = ConfigurationSettings.AppSettings["msgnum"];
            msgnum = msgnumstr == null ? 0 : int.Parse(msgnumstr);
            string keystr = ConfigurationSettings.AppSettings["analyzedkey"];
            if (keystr != null)
                analyzedkey = keystr;
            serverlist = memchached.Split(',');
            // initialize the pool for memcache servers
            try
            {
                pool = SockIOPool.GetInstance(poolName);
            }
            catch (Exception)
            {

                throw;
            }
            pool.SetServers(serverlist);
            pool.Initialize();
            mc = new MemcachedClient();
            mc.PoolName = poolName;
            mc.EnableCompression = false;
        }
Example #2
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">host to connect to</param>
        /// <param name="port">port to connect to</param>
        /// <param name="timeout">int ms to block on data for read</param>
        /// <param name="connectTimeout">timeout (in ms) for initial connection</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int port, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            if (connectTimeout > 0)
            {
                _socket = GetSocket(host, port, connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(host), port));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host + ":" + port;
        }
Example #3
0
        /// <summary>
        /// 这个客户端是跟怒网上例子自己整理的
        /// </summary>
        /// <param name="configPath"></param>
        public MemcacheClient2(string configPath)
        {
            Config config = new Config(configPath);
            _cfg = config;

            pool = SockIOPool.GetInstance(config.Id);
            pool.SetServers(config.Servers);
            pool.InitConnections = config.InitConnections;//初始化链接数
            pool.MinConnections = config.MinConnections;//最少链接数
            pool.MaxConnections = config.MaxConnections;//最大连接数
            pool.SocketConnectTimeout = config.SocketConnectTimeout;//Socket链接超时时间
            pool.SocketTimeout = config.SocketTimeout;// Socket超时时间
            pool.MaintenanceSleep = config.MaintenanceSleep;//维护线程休息时间
            pool.Failover = config.FailOver; //失效转移(一种备份操作模式)
            pool.Nagle = config.Nagle;//是否用nagle算法启动socket
            pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;
            pool.Initialize();
            if (config.Duration.TotalSeconds <= 0)
                this.duration = null;
            else
                this.duration = config.Duration;

            mc = new MemcachedClient();
            mc.PoolName = config.Id;
            mc.EnableCompression = config.EnableCompression;
        }
        /// <summary>
        /// Creates a wrapper for an existing <see cref="SockIOPool" />.
        /// </summary>
        /// <param name="pool">The existing pool</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="pool"/> is null</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the pool has been disposed</exception>
        public MemcachedCachePool(SockIOPool pool)
        {
            if (pool == null)
                throw new ArgumentNullException("pool");

            SetPool(pool, GetSockIOPoolName(pool));
        }
Example #5
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">hostname:port</param>
        /// <param name="timeout">read timeout value for connected socket</param>
        /// <param name="connectTimeout">timeout for initial connections</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            String[] ip = host.Split(':');

            // get socket: default is to use non-blocking connect
            if (connectTimeout > 0)
            {
                _socket = GetSocket(ip[0], int.Parse(ip[1], new System.Globalization.NumberFormatInfo()), connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(ip[0]), int.Parse(ip[1], new System.Globalization.NumberFormatInfo())));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host;
        }
Example #6
0
        public static SockIOPool GetInstance(String poolName)
        {
            if (Pools.ContainsKey(poolName))
            {
                return((SockIOPool)Pools[poolName]);
            }

            SockIOPool pool = new SockIOPool();

            Pools[poolName] = pool;

            return(pool);
        }
 private MemcachedClientSatat(String[] servers)
 {
     string msgnumstr = ConfigurationSettings.AppSettings["msgnum"];
     msgnum = msgnumstr == null ? 0 : int.Parse(msgnumstr);
     string keystr = ConfigurationSettings.AppSettings["analyzedkey"];
     if (keystr != null)
         analyzedkey = keystr;
     this.serverlist = servers;
     // initialize the pool for memcache servers
     pool = SockIOPool.GetInstance();
     pool.SetServers(serverlist);
     pool.Initialize();
     mc = new MemcachedClient();
     //mc.PoolName = poolName;
     mc.EnableCompression = false;
 }
Example #8
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">host to connect to</param>
        /// <param name="port">port to connect to</param>
        /// <param name="timeout">int ms to block on data for read</param>
        /// <param name="connectTimeout">timeout (in ms) for initial connection</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int port, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));

            _pool = pool;

            if (connectTimeout > 0)
            {
                _socket = GetSocket(host, port, connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(host), port));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host + ":" + port;
        }
        public CacheManager(params string[] hostring)
        {
            string poolName = Guid.NewGuid().ToString("N");
            mPool = SockIOPool.GetInstance(poolName);
            mPool.SetServers(hostring);

            mPool.InitConnections = 5;
            mPool.MinConnections = 5;
            mPool.MaxConnections = 10;

            mPool.SocketConnectTimeout = 1000;
            mPool.SocketTimeout = 3000;

            mPool.MaintenanceSleep = 30;
            mPool.Failover = true;

            mPool.Nagle = false;
            mPool.Initialize();
            mCacheClient = new MemcachedClient();
            mCacheClient.PoolName = poolName;
            mCacheClient.EnableCompression = false;
        }
        private void SetPool(SockIOPool pool, string poolName)
        {
            this.pool = pool;
            this.poolName = poolName;

            // Fixup our invariants to avoid mysterious failures.
            if (pool.Servers == null)
                pool.SetServers(EmptyArray<string>.Instance);
        }
        private static string GetSockIOPoolName(SockIOPool pool)
        {
            lock (SockIOPoolsLock)
            {
                foreach (DictionaryEntry entry in GetSockIOPools())
                {
                    if (entry.Value == pool)
                        return (string)entry.Key;
                }
            }

            throw new ObjectDisposedException("The pool has been disposed.");
        }
Example #12
0
 public MaintenanceThread(SockIOPool pool)
 {
     _thread = new Thread(new ThreadStart(Maintain));
     _pool   = pool;
 }
Example #13
0
        public static SockIOPool GetInstance(String poolName)
        {
            if(Pools.ContainsKey(poolName))
                return (SockIOPool)Pools[poolName];

            SockIOPool pool = new SockIOPool();
            Pools[poolName] = pool;

            return pool;
        }
Example #14
0
 public MaintenanceThread(SockIOPool pool)
 {
     _thread = new Thread(new ThreadStart(Maintain));
     _pool = pool;
 }
Example #15
0
        /// <summary>
        /// 关闭连接
        /// </summary>
        private void Shutdown()
        {
            if (_SockIOPool != null)
            {
                _SockIOPool.Shutdown();

                _SockIOPool = null;
            }
        }
 private static void DisposeSockIOPool(string poolName, SockIOPool pool)
 {
     lock (SockIOPoolsLock)
     {
         IDictionary pools = GetSockIOPools();
         if (pools[poolName] == pool)
             GetSockIOPools().Remove(poolName);
     }
 }
Example #17
0
        protected void Application_Start(object sender, EventArgs e)
        {

            if (_xameleonConfiguration.UseMemcached == "yes")
            {
                _useMemCached = true;
                _memcachedClient = new MemcachedClient();
                _pool = SockIOPool.GetInstance();
                List<string> serverList = new List<string>();
                foreach (MemcachedServer server in _memcachedConfiguration.MemcachedServerCollection)
                {
                    serverList.Add(server.IP + ":" + server.Port);
                }
                _pool.SetServers(serverList.ToArray());

                if (_memcachedConfiguration.UseCompression != null && _memcachedConfiguration.UseCompression == "yes")
                    _memcachedClient.EnableCompression = true;
                else
                    _memcachedClient.EnableCompression = false;

                MemcachedPoolConfig poolConfig = (MemcachedPoolConfig)_memcachedConfiguration.PoolConfig;
                _pool.InitConnections = (int)poolConfig.InitConnections;
                _pool.MinConnections = (int)poolConfig.MinConnections;
                _pool.MaxConnections = (int)poolConfig.MaxConnections;
                _pool.SocketConnectTimeout = (int)poolConfig.SocketConnectTimeout;
                _pool.SocketTimeout = (int)poolConfig.SocketConnect;
                _pool.MaintenanceSleep = (int)poolConfig.MaintenanceSleep;
                _pool.Failover = (bool)poolConfig.Failover;
                _pool.Nagle = (bool)poolConfig.Nagle;
                _pool.Initialize();
            }

            string baseUri = (string)_xameleonConfiguration.PreCompiledXslt.BaseUri;
            if (baseUri != String.Empty)
                baseUri = (string)_xameleonConfiguration.PreCompiledXslt.BaseUri;
            else
                baseUri = "~";

            _xsltTransformationManager = new XsltTransformationManager(_processor, _transform, _resolver, _serializer);
            _xsltTransformationManager.HashAlgorithm = _hashAlgorithm;
            _resolver.Credentials = CredentialCache.DefaultCredentials;
            _namedXsltHashtable = _xsltTransformationManager.NamedXsltHashtable;

            string hashkey = (string)_xameleonConfiguration.BaseSettings.ObjectHashKey;
            Application["hashkey"] = hashkey;

            foreach (PreCompiledXslt xslt in _xameleonConfiguration.PreCompiledXslt)
            {
                string localBaseUri = (string)_xameleonConfiguration.PreCompiledXslt.BaseUri;
                if (localBaseUri == String.Empty)
                    localBaseUri = baseUri;
                Uri xsltUri = new Uri(HttpContext.Current.Server.MapPath(localBaseUri + xslt.Uri));
                _xsltTransformationManager.Compiler.BaseUri = xsltUri;
                _xsltTransformationManager.AddTransformer(xslt.Name, xsltUri, _resolver, xslt.InitialMode, xslt.InitialTemplate);
                _namedXsltHashtable.Add(xslt.Name, xsltUri);
                if (xslt.UseAsBaseXslt == "yes")
                {
                    _baseXsltContext = new BaseXsltContext(xsltUri, XsltTransformationManager.GenerateNamedETagKey(xslt.Name, xsltUri), xslt.Name);
                }
            }

            _xsltTransformationManager.SetBaseXsltContext(_baseXsltContext);

            foreach (XsltParam xsltParam in _xameleonConfiguration.GlobalXsltParam)
            {
                _globalXsltParams[xsltParam.Name] = (string)xsltParam.Select;
            }

            if (_memcachedClient != null)
                Application["appStart_memcached"] = _memcachedClient;
            Application["appStart_usememcached"] = _useMemCached;
            Application["appStart_xslTransformationManager"] = _xsltTransformationManager;
            Application["appStart_namedXsltHashtable"] = _namedXsltHashtable;
            Application["appStart_globalXsltParams"] = _globalXsltParams;

        }
        /// <summary>
        /// Disposes of a pool.
        /// </summary>
        public void Dispose()
        {
            lock (this)
            {
                if (pool != null)
                {
                    pool.Shutdown();
                    DisposeSockIOPool(poolName, pool);

                    pool = null;
                }
            }
        }
Example #19
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">hostname:port</param>
        /// <param name="timeout">read timeout value for connected socket</param>
        /// <param name="connectTimeout">timeout for initial connections</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));

            _pool = pool;

            String[] ip = host.Split(':');

            // get socket: default is to use non-blocking connect
            if (connectTimeout > 0)
            {
                _socket = GetSocket(ip[0], int.Parse(ip[1], new System.Globalization.NumberFormatInfo()), connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(ip[0]), int.Parse(ip[1], new System.Globalization.NumberFormatInfo())));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host;
        }
Example #20
0
        /// <summary>
        /// 初始化memcache客户端
        /// </summary>
        private void InitMemCacheClient()
        {
            _SockIOPool = SockIOPool.GetInstance();
            _SockIOPool.SetServers(_Servers);

            //客户端连接数
            _SockIOPool.InitConnections = 10;
            //客户端最小连接数
            _SockIOPool.MinConnections = 30;
            //客户端最大充许连接数
            _SockIOPool.MaxConnections = 60;

            //SOCKET连接超时
            _SockIOPool.SocketConnectTimeout = 200;

            //SOCKET 接收数据超时
            _SockIOPool.SocketTimeout = 400;

            //检测时间间隔
            _SockIOPool.MaintenanceSleep = 10 * 1000;
            _SockIOPool.Failover = false;
            _SockIOPool.Nagle = false;
            _SockIOPool.Initialize();

            MemCacheClient = new MemcachedClient();
            MemCacheClient.EnableCompression = false;
        }