设为首页 收藏本站
查看: 1355|回复: 0

[经验分享] Memcached windows 下安装与测试

[复制链接]

尚未签到

发表于 2015-9-1 10:40:40 | 显示全部楼层 |阅读模式
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。但是它并不提供冗余(例如,复制其hashmap条目);当某个服务器S停止运行或崩溃了,所有存放在S上的键/值对都将丢失。   Memcached官方:http://danga.com/memcached/
  关于Memcached的介绍请参考:Memcached深度分析
  下载Windows的Server端
  下载地址:http://code.jellycan.com/memcached/
  安装Memcache Server(也可以不安装直接启动)
  1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在CMD下输入 "c:\memcached\memcached.exe -d install" 安装.
3. 再输入:"c:\memcached\memcached.exe -d start" 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
  如果下载的是二进制的版本,直接运行就可以了,可以加上参数来加以设置。
  
常用设置:
-p <num>          监听的端口
-l <ip_addr>      连接的IP地址, 默认是本机
-d start          启动memcached服务
-d restart        重起memcached服务
-d stop|shutdown  关闭正在运行的memcached服务
-d install        安装memcached服务
-d uninstall      卸载memcached服务
-u <username>     以<username>的身份运行 (仅在以root运行的时候有效)
-m <num>          最大内存使用,单位MB。默认64MB
-M                内存耗尽时返回错误,而不是删除项
-c <num>          最大同时连接数,默认是1024
-f <factor>       块大小增长因子,默认是1.25
-n <bytes>        最小分配空间,key+value+flags默认是48
-h                显示帮助
  然后就可以用.net 的memcached客户端来试一下了。
  C# 下可用的API(每个客户端API中都有详细的说明和注释)
  https://sourceforge.net/projects/memcacheddotnet/
http://www.codeplex.com/EnyimMemcached/ - Client developed in .NET 2.0 keeping performance and extensibility in
  mind. (Supports consistent hashing.)
http://code.google.com/p/beitmemcached/ - Client developed by BeIT with many new features
  转载出处: http://www.yaosansi.com/
  ----------------------------------------------------------------------------------------
  Client调用:
  下载示例代码网址: http://sourceforge.net/projects/memcacheddotnet/
  C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers. Ported from the Java memcached library located athttp://www.whalin.com/memcached/.
  e.g.:




show sourceview sourceprint?

001/**

002 * MemcachedBench.cs

003 *

004 * Copyright (c) 2005

005 * Tim Gebhardt <tim@gebhardtcomputing.com>

006 *  

007 * Based off of code written by

008 * Greg Whalin <greg@meetup.com>

009 * for his Java Memcached client:

010 * http://www.whalin.com/memcached/

011 *  

012 *

013 * See the memcached website:

014 * http://www.danga.com/memcached/

015 *

016 * This module is Copyright (c) 2005 Tim Gebhardt.

017 * All rights reserved.

018 *

019 * This library is free software; you can redistribute it and/or

020 * modify it under the terms of the GNU Lesser General Public

021 * License as published by the Free Software Foundation; either

022 * version 2.1 of the License, or (at your option) any later

023 * version.

024 *

025 * This library is distributed in the hope that it will be

026 * useful, but WITHOUT ANY WARRANTY; without even the implied

027 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR

028 * PURPOSE.  See the GNU Lesser General Public License for more

029 * details.

030 *

031 * You should have received a copy of the GNU Lesser General Public

032 * License along with this library; if not, write to the Free Software

033 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

034 *

035 * @author Tim Gebhardt<tim@gebhardtcomputing.com>  

036 * @version 1.0

037 */

038namespace Memcached.MemcachedBench

039{

040    using System;

041    using System.Collections;

042  

043    using Memcached.ClientLibrary;

044  

045    public class MemcachedBench  

046    {

047        /// <summary>

048        /// Arguments:  

049        ///     arg[0] = the number of runs to do

050        ///     arg[1] = the run at which to start benchmarking

051        /// </summary>

052        /// <param name="args"></param>

053        [STAThread]

054        public static void Main(String[] args)  

055        {

056            int runs = 100;

057            int start = 200;

058            if(args.Length > 1)

059            {

060                runs = int.Parse(args[0]);

061                start = int.Parse(args[1]);

062            }

063  

064            //可以设置多个服务器列表

065            //string[] serverlist = { "127.0.0.1:11211" , "140.192.34.73:11211" };

066            string[] serverlist = { "127.0.0.1:11211" };    //, "140.192.34.73:11211" };

067  

068            // initialize the pool for memcache servers

069            SockIOPool pool = SockIOPool.GetInstance();

070            pool.SetServers(serverlist);

071  

072            pool.InitConnections = 3;

073            pool.MinConnections = 3;

074            pool.MaxConnections = 5;

075  

076            pool.SocketConnectTimeout = 1000;

077            pool.SocketTimeout = 3000;

078  

079            pool.MaintenanceSleep = 30;

080            pool.Failover = true;

081  

082            pool.Nagle = false;

083            pool.Initialize();

084  

085            // initialize the pool for memcache servers

086//          SockIOPool pool = SockIOPool.Instance;

087//          pool.Servers = serverlist;

088//

089//          pool.InitConn = 5;

090//          pool.MinConn = 5;

091//          pool.MaxConn = 50;

092//          pool.MaintSleep = 30;

093//          pool.SocketTO = 1000;

094//

095//          pool.Nagle = false;

096//          pool.Initialize();

097  

098//

099//          // get client instance

100            MemcachedClient mc = new MemcachedClient();

101            mc.EnableCompression = false;

102  

103//          MemcachedClient mc = new MemcachedClient();

104//          mc.CompressEnable = false;

105//          mc.CompressThreshold = 0;

106//          mc.Serialize = true;

107  

108            string keyBase = "testKey";

109            string obj = "这是我的字符串This is a test of an object blah blah es, serialization does not seem to slow things down so much.  The gzip compression is horrible horrible performance, so we only use it for very large objects.  I have not done any heavy benchmarking recently";

110  

111            long begin = DateTime.Now.Ticks;

112            for(int i = start; i < start+runs; i++)  

113            {

114                mc.Set(keyBase + i, obj);

115            }

116            long end = DateTime.Now.Ticks;

117            long time = end - begin;

118  

119            Console.WriteLine(runs + " 设置花费的时间-sets: " + new TimeSpan(time).ToString() + "ms");

120  

121            begin = DateTime.Now.Ticks;

122            int hits = 0;

123            int misses = 0;

124            for(int i = start; i < start+runs; i++)  

125            {

126                string str = (string) mc.Get(keyBase + i);

127                Console.WriteLine("key={0},value={1}",keyBase+i,str);

128                if(str != null)

129                    ++hits;

130                else

131                    ++misses;

132            }

133            end = DateTime.Now.Ticks;

134            time = end - begin;

135  

136            Console.WriteLine(runs + "读取花费的时间- gets: " + new TimeSpan(time).ToString() + "ms");

137            Console.WriteLine("Cache hits,成功: " + hits.ToString());

138            Console.WriteLine("Cache misses,失败: " + misses.ToString());

139  

140            IDictionary stats = mc.Stats();

141            foreach(string key1 in stats.Keys)

142            {

143                Console.WriteLine(key1);

144                Hashtable values = (Hashtable)stats[key1];

145                foreach(string key2 in values.Keys)

146                {

147                    Console.WriteLine(key2 + ":" + values[key2]);

148                }

149                Console.WriteLine();

150            }

151  

152            SockIOPool.GetInstance().Shutdown();

153            Console.ReadKey();

154        }

155    }

156}
  
  服务器端: http://files.cnblogs.com/wucg/memcached-1.2.6-win32-bin.zip
  下载Client库文件及示例,vs2008,.netframework 1.0,2.0 http://files.cnblogs.com/wucg/clientlib.zip
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-108283-1-1.html 上篇帖子: 【转】命令行查看Memcached运行状态 下篇帖子: 【转载】遍历memcached缓存对象(C#)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表