Windows上memcached的使用
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 键值
例:
至此服务端配置完成
客户端的版本比较多,并且不能互用,因为采用了压缩机制,日志等功能,所以在选择客户端时要注意这些。
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 = ;
cache.ConnectTimeout = ;
cache.MinPoolSize = ;
cache.MaxPoolSize = ; //----------------
// 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", );
cache.Set("mydate", new DateTime(, , ));
//Use custom hash
cache.Set("secondstring", "Flygande b鋍kasiner s鰇a hwila p?mjuka tufvor", ); //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[] + ", value 2: " + array[]);
} //Get several values at once
object[] result = cache.Get(new string[]{"myinteger", "mydate"});
if (result[] != null && result[] is int) {
Console.Out.WriteLine("Fetched item with key: myinteger, value: " + (int)result[]);
}
if (result[] != null && result[] is DateTime) {
Console.Out.WriteLine("Fetched item with key: mydate, value: " + (DateTime)result[]);
} str = cache.Get("secondstring", ) 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", );
ulong? counter = cache.GetCounter("mycounter");
if (counter.HasValue) {
Console.Out.WriteLine("Fetched mycounter, value: " + counter.Value);
} //Increment the counter
counter = cache.Increment("mycounter", );
if (counter.HasValue) {
Console.Out.WriteLine("Incremented mycounter with 1, new value: " + counter.Value);
} //Decrement the counter
counter = cache.Decrement("mycounter", );
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", ));
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", ));
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.csdn.net/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.csdn.net/whjwhja6/article/details/9172463
http://blog.csdn.net/hguisu/article/details/7353551
Windows上memcached的使用的更多相关文章
- php memcached在windows上的使用
php的memcached是比memcache,效率更高的memcache缓存扩展. 然而windows下并没有这个扩展,于是做单元测试时要把代码上传到linux服务器,再运行,甚是麻烦. (当然另外 ...
- Windows上使用Vagrant打造Laravel Homestead可协同跨平台开发环境
1.简介 Laravel 致力于让整个 PHP 开发过程变得让人愉悦,包括本地开发环境,为此官方为我们提供了一整套本地开发环境 —— Laravel Homestead. Laravel Homest ...
- 大数据高性能数据库Redis在Windows上的使用教程
Redis学习笔记----Redis在windows上的安装配置和使用 Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括s ...
- windows下Memcached 架设及java应用(转)
1 Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可 ...
- windows上php环境下memcache和mongodb的安装
mangodb安装 1. 下载mongodb的安装文件,我安装的windows 64位的,下载地址如下: https://fastdl.mongodb.org/win32/mongodb-win32- ...
- 做梦也没有想到:Windows 上的 .NET Core 表现更糟糕
昨天晚上 18:15 左右我们发布了跑在 Windows 上 .NET Core 博客系统,本想与 .NET Framework 版进行同“窗”的较量,结果刚发布上线就发现 CPU 占用异常高,发布不 ...
- Windows 上安装 Jekyll.
Jekyll是一个静态网站生成工具.它允许用户使用HTML.Markdown或Textile来建立静态页面,然后通过模板引擎Liquid(Liquid Templating Engine)来运行. 原 ...
- 在Windows上安装Elasticsearch 5.0
在windows上安装Elasticsearch Elasticsearch可以使用.zip软件包安装在Windows上. elasticsearch-service.bat命令,它将设置Elasti ...
- 使用Gitblit 在windows 上部署你的Git Server
Gitblit: 在windows 上部署你的Git Server 前言 之前在dudu的文章里看到过用bonobogit 部署在 IIS 7.5 上的Window 平台的git 服务器.学着部署使用 ...
随机推荐
- 蜗牛—苍茫IT文章大学的路(十)
昨晚,有个叫***培训机构鼓吹我们学校.起初我还以为是介绍这个游戏吧.谁知道.它原来是一个培训结构.去年我买的表啊 我知道这会不会去,我也浪费了时间审查.因为今天下午和晚上来测试啊.我没有审查,. 当 ...
- char与byte差异
很多人刚开始学习(包含I,我已经学会了一年多java该)肯会char和byte怀疑这两个数据类型,相互混淆.今天,大量的信息专门搜索,至byte和char两个数据类型进行了总结和比较.第一批成果与大家 ...
- 新秀发挥云17号:RHEL改变以太网地址克隆虚拟机后,
新秀发挥云17号:RHEL改变以太网地址克隆虚拟机后, (一)变化hostname # vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=your-v ...
- 【android】ImageView的src和background以及两者之间的神奇的差异
一.ImageView中XML属性src和background的差别: background会依据ImageView组件给定的长宽进行拉伸.而src就存放的是原图的大小,不会进行拉伸.src是图片内容 ...
- 谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!
谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个 ...
- POJ 1565 Skew Binary(简单的问题)
[简要题意]:第二个是数字系统的代表性的定义.而给了你这个号码系统提示的形式和十进制转换之间的关系.现在给你一些这样的系统.让你把它变成2二进制输出. [分析]:当中 base[k] = 2^(k ...
- ultraEdit-32 PHP/HTML智能提示
原文 ultraEdit-32 PHP/HTML智能提示 高级–>配置–>编辑器–>自动完成–>勾选自动显示……选项,在下面输入框中输入你要求输出多个字符才出现提示,比如 ec ...
- EF结合SqlBulkCopy
EF结合SqlBulkCopy在项目中的使用 这是我第一次写博客,由于水平有限,写不出什么好东西,还望见谅. 我现在参与的这个项目采用的是EF框架,方便了数据库的访问.但在实际中,发现项目中导入市县E ...
- 玩转Web之servlet(四)---B/S是怎样使用http协议完毕通信过程的
在上一篇文章中,我简单的说了一下B/S架构的流程图,关于浏览器和server之间的通信过程知识含糊的说了一下,在这篇文章中我再总结一下B/S架构里是怎样利用http协议去完毕通信的. (一)通讯过程 ...
- 【phpMyAdmin】更改配置文件连接到其他server
默认phpMyAdmin安装完毕后对机器的访问mysql,但有时我们需要访问其它server的mysql数据库,所以我们需要配置. 真,phpMyAdmin已经为我们做了配置的选项.可是须要我们进行一 ...