Memcached的windows版本安装!
一般情况下,我们用linux作为生产环境,但是开发还是在windows下面,所以我们需要 win版本。。但是memcache官方网站只提供了源码。
下面介绍使用第三方编译的win版本的方法:
这是一个由Kenneth Dalgleish基于Kronuz的1.2.1构建。官方的memcached的团队不对这个版本支持!
核心关键在这个:
http://splinedancer.com/memcached-win32/
memcached for Windows
This is a port of memcached to the win32 architecture by Kenneth Dalgleish, based on Kronuz's 1.2.1 port. This port is not supported by the official memcached team.
Install
The win32 version of memcached can be run both as a NT Service or from the command line. To install memcached as a service, follow the next steps:
- Unzip the binaries in your desired directory (eg. c:\memcached)
- Install the service using the command: 'c:\memcached\memcached.exe -d install' from the command line
- Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
- Use the server, by default listening to port 11211
Building from source
To build from source, you will need Visual Studio 2005 (any edition with C++ should work), Windows SDK (eg.Windows SDK for Windows Server 2008 and .NET Framework 3.5) and libevent (win32 binary provided on this page).- Install Visual Studio 2005
- Install Windows SDK
- Put libevent.lib in Win32-Prj/ folder
- Open solution file and it should build
Downloads
memcached 1.2.4 Win32 Beta
- memcached 1.2.4 Win32 Beta Binaries (09.03.2008)
- memcached 1.2.4 Win32 Beta Source (09.03.2008)
- memcached 1.2.4 Win32 Beta Patch for SVN revision 662 (tag 1.2.4) (09.03.2008)
Libevent 1.3e Win32
(Needed if building from source)- Libevent 1.3e Win32 Binary (06.03.2008)
Windows下Memcache安装
1、下载memcache for windows。下载地址:http://splinedancer.com/memcached-win32/,解压到d:\memcached。
2、在命令行状态下输入: d:\memcached\memcached.exe -d install 。至此memcached已经安装成windows服务
3、在命令行下输入: d:\memcached\memcached.exe -d start 以启动memcached服务。当然也可以选择在windows服务中启动
就是这么简单,简简单单的三步memcache的服务器端就准备完毕
配置PHP
1、下载php_memcache.dll扩展,下载地址:http://www.php100.com/html/download/server/2010/0125/3858.html,如果你已经拥有php_memcache.dll请略过这一步。
2、在php.ini中添加一行:”extension=php_memcache.dll” 。如果已经存在这一行就把前面的分号去掉
3、重启Apache使用phpinfo()查看,若有memcache相关则证明安装成功
memcached的基本设置:
-p 监听的端口
-l连接的IP地址,默认是本机
-dstart启动memcached服务
-drestart重起memcached服务
-dstop|shutdown关闭正在运行的memcached服务
-dinstall安装memcached服务
-duninstall卸载memcached服务
-u以的身份运行(仅在以root运行的时候有效)
-m最大内存使用,单位MB。默认64MB
-M内存耗尽时返回错误,而不是删除项
-c最大同时连接数,默认是1024
-f块大小增长因子,默认是1.25
-n最小分配空间,key+value+flags默认是48
-h 显示帮助
Memcache环境测试:运行下面的php文件,如果有输出This is a value!,就表示环境搭建成功。开始领略Memcache的魅力把!
<?php $mem = new Memcache; $mem->connect("127.0.0.1", 11211); $mem->set('key', 'This is a value!', 0, 60); $val = $mem->get('key'); echo $val;
JAVA下的安装:
2.从https://github.com/gwhalin/Memcached-Java-Client下载Memcached相关的jar包。
测试程序:
import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemCachedTest { private static MemCachedClient mcc = new MemCachedClient(); static { String[] servers = {"192.168.123.100:11211"}; //创建一个连接池 SockIOPool pool = SockIOPool.getInstance(); //设置缓存服务器 pool.setServers(servers); //设置初始化连接数,最小连接数,最大连接数以及最大处理时间 pool.setInitConn(50); pool.setMinConn(50); pool.setMaxConn(500); pool.setMaxIdle(1000 * 60 * 60); //设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小 pool.setMaintSleep(30); //关闭套接字缓存 pool.setNagle(false); //连接建立后的超时时间 pool.setSocketTO(3000); //连接建立时的超时时间 pool.setSocketConnectTO(0); //初始化连接池 pool.initialize(); } protected MemCachedTest(){ } public static MemCachedClient getInstance(){ return mcc; } public static void main(String[] args) { MemCachedClient mcc= MemCachedTest.getInstance(); for ( int i = 0; i < 10; i++ ) { boolean success = mcc.set( "" + i, "Hello!" ); String result = (String)mcc.get( "" + i ); System.out.println( String.format( "set( %d ): %s", i, success ) ); System.out.println( String.format( "get( %d ): %s", i, result ) ); } System.out.println( "\n\t -- sleeping --\n" ); try { Thread.sleep( 100000 ); } catch ( Exception ex ) { } for ( int i = 0; i < 10; i++ ) { boolean success = mcc.set( "" + i, "Hello!" ); String result = (String)mcc.get( "" + i ); System.out.println( String.format( "set( %d ): %s", i, success ) ); System.out.println( String.format( "get( %d ): %s", i, result ) ); } } }
参考资料:
对Memcached有疑问的朋友可以参考下列文章:
Linux下的Memcache安装:http://www.ccvita.com/257.html
Windows下的Memcache安装:http://www.ccvita.com/258.html
Memcache基础教程:http://www.ccvita.com/259.html
Discuz!的Memcache缓存实现:http://www.ccvita.com/261.html
Memcache协议中文版:http://www.ccvita.com/306.html
Memcache分布式部署方案:http://www.ccvita.com/395.html本例的代码经过本人亲自测试!!