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

[经验分享] Windows上memcached的使用

[复制链接]

尚未签到

发表于 2015-8-31 12:43:43 | 显示全部楼层 |阅读模式
  Memcached是什么?
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
  Memcached能缓存什么?
通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。
Memcached快么?
非常快。Memcached使用了libevent(如果可以的话,在linux下使用epoll)来均衡任何数量的打开链接,使用非阻塞的网络I/O,对内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表, 因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O(1).。
Memcached的特点?
Memcached的缓存是一种分布式的,可以让不同主机上的多个用户同时访问, 因此解决了共享内存只能单机应用的局限,更不会出现使用数据库做类似事情的时候,磁盘开销和阻塞的发生。
  Memcached的使用需要服务端和客户端同时合作完成。
  服务端一般用Danga Interactive开发的:
  Memcached官方站点:http://www.danga.com/memcached/
  Memcached Win32 1.2.6下载:http://code.jellycan.com/memcached/ 或者 http://code.google.com/p/beitmemcached/(windows)
  1.解压Memcached_1.2.5.zip ,它是memcached的服务器端。
2.把Memcached_1.2.5复制到你指定的做为缓存服务器的电脑上,比如叫做192.168.0.1。
3.cmd下运行类似命令 'd:/memcached/memcached.exe -d install' 安装服务器端,这时候它应该会出现在windows服务中
4.cmd下运行类似命令 'd:/memcached/memcached.exe -d start'启动服务,看服务器进程中是否有memcached进程。
5.确认服务器端口11211是否开放(防火墙设置中),否则其他机器无法访问
6.服务器端这时已经安装完毕、在其他机器上测试一下,cmd输入telnet 192.168.0.1 11211看能否登录
  telnet添加数据到memcached:
  add key值 0 时间(秒)  长度(存放多长时间由你指定, 键名不能重复,但是值可以重复
  )例:add name 0 5 5不可以在add name 0 77 44
  获取数据
  get key值  【key-val   key不能重复,但是val可以重复】
  修改数据
  replace key值 0 60 5 【如果key值不存在,则失败】
  set key值 0 60 5  【如果key值不存在,相当于添加,如果存在,则相当于修改.】
  删除数据
  delete 键值
  例:
DSC0000.jpg
  至此服务端配置完成
  客户端的版本比较多,并且不能互用,因为采用了压缩机制,日志等功能,所以在选择客户端时要注意这些。
  Memcached .NET客户端:
  1).NET memcached client library
  下载地址:https://sourceforge.net/projects/memcacheddotnet
  2)enyim.com Memcached Client
  下载地址:http://www.codeplex.com/EnyimMemcached/
  3)Memcached Providers
  下载地址:http://www.codeplex.com/memcachedproviders
  4) BeIT Memcached
  下载地址:http://code.google.com/p/beitmemcached/
  我这里用BeIT,BeITMemcached_source_2008_05_31.zip压缩包下栽下来,里面有Sample



class Example {
public static void Main(string[] args) {
//---------------------
// Setting up a client.
//---------------------
Console.Out.WriteLine("Setting up Memcached Client.");
MemcachedClient.Setup("MyCache", new string[] { "localhost" });
//It is possible to have several clients with different configurations:
//If it is impossible to resolve the hosts, this method will throw an exception.
try {
MemcachedClient.Setup("MyOtherCache", new string[]{ "server1.example.com:12345", "server2.example.com:12345"});
} catch (Exception e) {
Console.WriteLine(e.Message);
}
//Get the instance we just set up so we can use it. You can either store this reference yourself in
//some field, or fetch it every time you need it, it doesn't really matter.
MemcachedClient cache = MemcachedClient.GetInstance("MyCache");
//It is also possible to set up clients in the standard config file. Check the section "beitmemcached"
//in the App.config file in this project and you will see that a client called "MyConfigFileCache" is defined.
MemcachedClient configFileCache = MemcachedClient.GetInstance("MyConfigFileCache");
//Change client settings to values other than the default like this:
cache.SendReceiveTimeout = 5000;
cache.ConnectTimeout = 5000;
cache.MinPoolSize = 1;
cache.MaxPoolSize = 5;
//----------------
// Using a client.
//----------------
//Set some items
Console.Out.WriteLine("Storing some items.");
cache.Set("mystring", "The quick brown fox jumped over the lazy dog.");
cache.Set("myarray", new string[]{"This is the first string.", "This is the second string."});
cache.Set("myinteger", 4711);
cache.Set("mydate", new DateTime(2008, 02, 23));
//Use custom hash
cache.Set("secondstring", "Flygande b鋍kasiner s鰇a hwila p?mjuka tufvor", 4711);
//Get a string
string str = cache.Get("mystring") as string;
if (str != null) {
Console.Out.WriteLine("Fetched item with key: mystring, value: " + str);
}
//Get an object
string[] array = cache.Get("myarray") as string[];
if (array != null) {
Console.Out.WriteLine("Fetched items with key: myarray, value 1: " + array[0] + ", value 2: " + array[1]);
}
//Get several values at once
object[] result = cache.Get(new string[]{"myinteger", "mydate"});
if (result[0] != null && result[0] is int) {
Console.Out.WriteLine("Fetched item with key: myinteger, value: " + (int)result[0]);
}
if (result[1] != null && result[1] is DateTime) {
Console.Out.WriteLine("Fetched item with key: mydate, value: " + (DateTime)result[1]);
}
str = cache.Get("secondstring", 4711) as string;
if (str != null) {
Console.Out.WriteLine("Fetched item with key and custom hash: secondstring, value: " + str);
}
//Set a counter
Console.Out.WriteLine("Setting an item for incrementing and decrementing.");
cache.SetCounter("mycounter", 9000);
ulong? counter = cache.GetCounter("mycounter");
if (counter.HasValue) {
Console.Out.WriteLine("Fetched mycounter, value: " + counter.Value);
}
//Increment the counter
counter = cache.Increment("mycounter", 1);
if (counter.HasValue) {
Console.Out.WriteLine("Incremented mycounter with 1, new value: " + counter.Value);
}
//Decrement the counter
counter = cache.Decrement("mycounter", 9000);
if (counter.HasValue) {
Console.Out.WriteLine("Decremented mycounter with 9000, new value: " + counter.Value);
}
//Append and prepend
Console.Out.WriteLine("Storing bar for append/prepend");
cache.Set("foo", "bar");
Console.Out.WriteLine("Appending baz");
cache.Append("foo", " baz");
Console.Out.WriteLine("Prepending foo");
cache.Prepend("foo", "foo ");
Console.Out.WriteLine("New value: " + cache.Get("foo"));
//Cas
cache.Delete("castest");
Console.Out.WriteLine("Trying to CAS non-existant key castest: " + cache.CheckAndSet("castest", "a", 0));
Console.Out.WriteLine("Setting value for key: castest, value: a");
cache.Set("castest", "a");
Console.Out.WriteLine("Trying to CAS key castest with the wrong unique: " + cache.CheckAndSet("castest", "a", 0));
ulong unique;
cache.Gets("castest", out unique);
Console.Out.WriteLine("Getting cas unique for key castest: " + unique);
Console.Out.WriteLine("Trying to CAS again with the above unique: " + cache.CheckAndSet("castest", "b", unique));
string value = cache.Gets("castest", out unique) as string;
Console.Out.WriteLine("New value: " + value + ", new unique: " + unique);
Console.Out.WriteLine("Displaying the socketpool status:");
foreach (KeyValuePair<string, Dictionary<string, string>> host in cache.Status()) {
Console.Out.WriteLine("Host: " + host.Key);
foreach (KeyValuePair<string, string> item in host.Value) {
Console.Out.WriteLine("\t" + item.Key + ": " + item.Value);
}
Console.Out.WriteLine();
}
Console.Out.WriteLine();
Console.Out.WriteLine("Finished. Press enter to exit.");
Console.In.ReadLine();
}
}
  最早的客户端是采用取余的hash算法来查找服务器的,我们知道以往资料要放到 M 台服务器上,最简单的方法就是取余数 (hash_value % M) 然后放到对应的服务器上,那就是当添加或移除服务器时,缓存重组的代价相当巨大。添加服务器后,余数就会产生巨变,这样就无法获取与保存时相同的服务器, 从而影响缓存的命中率。
如今采用了Consistent Hashing算法来改善性能,具体可以看最后的参考网址。
  参考
  http://blog.iyunv.com/jinjazz/article/details/2664136
  http://www.cnblogs.com/dudu/archive/2009/07/19/1526407.html
  http://www.cnblogs.com/mecity/archive/2011/06/13/2079548.html
  http://www.cnblogs.com/wucg/archive/2011/03/01/1968185.html
  http://blog.iyunv.com/whjwhja6/article/details/9172463
  http://blog.iyunv.com/hguisu/article/details/7353551
  

运维网声明 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-106815-1-1.html 上篇帖子: net下Memcached简介使用 下篇帖子: Memcached通用类(基于Memcached Client Library)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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