57 } 58 else 59 {
60 Console.WriteLine(\61 }
62 Console.ReadLine(); 63
64 SockIOPool.GetInstance().Shutdown(); //关闭池,关闭sockets 65 } 66 } 67}
652. 4 运行结果
653.
654. 后记: 是个不错的东西 ,使用起来也很方便,php ,ruby 的项目中用这个的很多,但是.net项目中用的较少(恕俺孤陋寡闻) 。希望有兴趣的朋友们 多多交流 。
655. 在Win开发环境下面配置了一下Memcached
656.
开发环境win下面配置使用Memcached方法概述
657. 再简单的事情没有做一遍都不能明白其中时候如此,今天配置Memcached就发现这个问题。帮助很全,先是在memcached for Windows获取到了需要的win下面的Memcached,按照方法: 658. 引用 659. Unzip the binaries in your desired directory (eg. c:memcached) Install the service using the command: 'c:memcachedmemcached.exe -d install' from the command line Start the server from the Microsoft Management Console or by running the following command: 'c:memcachedmemcached.exe -d start' Use the server, by default listening to port 11211 660. 然后
661. 在php.ini 加入一行 'extension=php_memcache.dll' 662. 然后到
http://pecl4win.php.net/ext.php/php_memcache.dll获取php_memcache.dll
663. 并复制到 ext 中(记住版本不要错了!)
664. 重启Apache,发现PHPInfo就是提示出不来memcache,真是无语了,代码测试总是提示 665. 引用
666. Fatal error: Class 'Memcache' not found in D:xampplitehtdocsmemcacheindex.php on line 20 667. 开始觉得奇怪,于是搜索在官方网站发现了http://www.php.net/manual/zh/ref.memcache.php 668.
669. Hi there:
For run memcached in a windows box: (tested with latest php,apache and memcache in xp sp2)
a) download the php_memcache.dll it can be found in the pecl file. b) put the dll in the extension folder (c:/php/extension for example). You cannot miss this folder because they are filled with php*.dll files. In some cases the extension folder used is the system32, a non-standard way to put dll but still works. c)configure the php.ini
; i put this like the latest extension extension=php_memcache.dll
; i'm not sure about this but don't hurts.. [Memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20 memcache.chunk_size =8192 memcache.default_port = 11211
d)This is important, memcached works with a EXTERNAL service. This service must be downloaded and installed prior to use the memcache. I use: http://jehiah.cz/projects/memcached-win32/
e)Remember to install the service and to start the service memcached.exe -d install for install and run services.msc for start the memcached service (or restart the system). f) check the firewall ports.
Finally restart the apache/iis and runs some test. At least in phpinfo must show some info about the memcache. Final notes :The \about memcache is not only can help for speed some process (or reduce the cpu use), also can be used like a global session for store a whole object also this \shared among all the users, like APPLICATION used in ASP. So (for example) it's possible to do a user counter without needing of database or writing a file. 670.
试试态度加上看了一下,看到了熟悉的东西了: 671. view plaincopy to clipboardprint? 672. $mem=newMemcache;
673. $mem->connect('127.0.0.1',11211); 674.
675. $mem->set('key','Thisisatest!',0,60); 676.
677. $val=$mem->get('key'); 678. echo$val;
679. $mem = new Memcache;
$mem->connect('127.0.0.1', 11211);
$mem->set('key', 'This is a test!', 0, 60); $val = $mem->get('key'); echo $val;
680. 在浏览器输出了 681. This is a test!
682. 另外作为服务器开发者,自然很关注的一个问题就是这个
Memcached的原理问题,在老农如是想,如是说,如是为博客中提到: 683. 引用
684. Memcached 本身的启动过程,在 memcached.c 的 main 函数中顺序如下:
685. 1 、调用 settings_init() 设定初始化参数 686. 2 、从启动命令中读取参数来设置 setting 值 687. 3 、设定 LIMIT 参数
688. 4 、开始网络 socket 监听(如果非 socketpath 存在)( 1.2 之后支持 UDP 方式)
689. 5 、检查用户身份( Memcached 不允许 root 身份启动) 690. 6 、如果有 socketpath 存在,开启 UNIX 本地连接(Sock 管道)
691. 7 、如果以 -d 方式启动,创建守护进程(如上调用 daemon 函数)
692. 8 、初始化 item 、 event 、状态信息、 hash 、连接、 slab 693. 9 、如设置中 managed 生效,创建 bucket 数组 694. 10 、检查是否需要锁定内存页 695. 11 、初始化信号、连接、删除队列 696. 12 、如果 daemon 方式,处理进程 ID
697. 13 、event 开始,启动过程结束, main 函数进入循环。 698. posted @ 2009-01-14 13:01 linFen 阅读(321) 评论(0) 编辑
699. 利用memcached构建高性能的Web应用程序
700. 面临的问题
701. 对于高并发高访问的Web应用程序来说,数据库存取瓶颈一直是个令人头疼的问题。特别当你的程序架构还是建立在单数据库模式,而一个数据池连接数峰值已经达到500的时候,那你的程序运行离崩溃的边缘也不远了。很多小网站的开发人员一开始都将注意力放在了产品需求设计上,缺忽视了程序整体性能,可扩展性等方面的考虑,结果眼看着访问量一天天网上爬,可突然发现有一天网站因为访问量过大而崩溃了,到时
候哭都来不及。所以我们一定要未雨绸缪,在数据库还没罢工前,想方设法给它减负,这也是这篇文章的主要议题。
702. 大家都知道,当有一个request过来后,web服务器交给app服务器,app处理并从db中存取相关数据,但db存取的花费是相当高昂 的。特别是每次都取相同的数据,等于是让数据库每次都在做高耗费的无用功,数据库如果会说话,肯定会发牢骚,你都问了这么多遍了,难道还记不住吗?是啊, 如果app拿到第一次数据并存到内存里,下次读取时直接从内存里读取,而不用麻烦数据库,这样不就给数据库减负了?而且从内存取数据必然要比从数据库媒介 取快很多倍,反而提升了应用程序的性能。
703. 因此,我们可以在web/app层与db层之间加一层cache层,主要目的:1. 减少数据库读取负担;2. 提高数据读取速度。而且,cache存取的媒介是内存,而一台服务器的内存容量一般都是有限制的,不像硬盘容量可以做到TB级别。所以,可以考虑采用分布 式的cache层,这样更易于破除内存容量的限制,同时又增加了灵活性。 704. Memcached 介绍
705. Memcached是开源的分布式cache系统,现在很多的大型web应用程序包括 facebook,youtube,wikipedia,yahoo等等都在使用memcached来支持他们每天数亿级的页面访问。通过把cache层 与他们的web架构集成,他们的应用程序在提高了性能的同时,还大大降低了数据库的负载。 706.
具体的memcached资料大家可以直接从它的官方网站[1]上得到。这里我就简单给大家介绍一下memcached的工作原理:
707. Memcached处理的原子是每一个(key,value)对(以下简称kv对),key会通过一个hash算法转化成hash-key,便于查找、对比以及做到尽可能的散列。同时,memcached用的是一个二级散列,通过一张大hash表来维护。
708. Memcached有两个核心组件组成:服务端(ms)和客户端(mc),在一个memcached的查询中,mc先通过计算key 的hash值来确定kv对所处在的ms位置。当ms确定后,客户端就会发送一个查询请求给对应的ms,让它来查找确切的数据。因为这之间没有交互以及多播 协议,所以memcached交互带给网络的影响是最小化的。
709. 举例说明:考虑以下这个场景,有三个mc分别是X,Y,Z,还有三个ms分别是A,B,C: 710. 设置kv对
711. X想设置key=”foo”,value=”seattle”
712. X拿到ms列表,并对key做hash转化,根据hash值确定kv对所存的ms位置 713. B被选中了 714. X连接上B,B收到请求,把(key=”foo”,value=”seattle”)存了起来
715. 获取kv对
716. Z想得到key=”foo”的value
717. Z用相同的hash算法算出hash值,并确定key=”foo”的值存在B上
718. Z连接上B,并从B那边得到value=”seattle” 719. 其他任何从X,Y,Z的想得到key=”foo”的值的请求都会发向B
720. 721. Memcached服务器(ms) 722. 内存分配
723. 默认情况下,ms是用一个内置的叫“块分配器”的组件来分配内存的。舍弃c++标准的malloc/free的内存分配,而采用块分配器的主要 目的是为了避免内存碎片,否则操作系统要花费更多时间来查找这些逻辑上连续的内存块(实际上是断开的)。用了块分配器,ms会轮流的对内存进行大块的分 配,并不断重用。当然由于块的大小各不相同,当数据大小和块大小不太相符的情况下,还是有可能导致内存的浪费。 同时,ms对key和data都有相应的限制,key的长度不能超过250字节,data也不能超过块大小的限制 --- 1MB。 724. 因为mc所使用的hash算法,并不会考虑到每个ms的内存大小。理论上mc会分配概率上等量的kv对给每个ms,这样如果每个ms的内存都不 太一样,那可能会导致内存使用率的降低。所以一种替代的解决方案是,根据每个ms的内存大小,找出他们的最大公约数,然后在每个ms上开n个容量=最大公 约数的instance,这样就等于拥有了多个容量大小一样的子ms,从而提供整体的内存使用率。 725. 缓存策略
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库memcached使用文档(8)在线全文阅读。
相关推荐: