java使用memcached

来源:互联网 发布:mac进入屏保快捷键 编辑:程序博客网 时间:2024/06/05 10:24

Memcached的windows版本安装!

  • 博客分类:
  • Memcached
  • 网络编程
memcached

一般情况下,我们用linux作为生产环境,但是开发还是在windows下面,所以我们需要win版本。。但是memcache官方网站只提供了源码。

 

下面介绍使用第三方编译的win版本的方法:

 

这是一个由Kenneth Dalgleish基于Kronuz1.2.1构建。官方memcached的团队不对这个版本支持!

 

核心关键在这个:

http://splinedancer.com/memcached-win32/

 

 

memcached for Windows

This is a port of memcached tothe win32 architecture by KennethDalgleish, based on Kronuz's 1.2.1port. This port is not supported by the official memcachedteam.

Install

The win32 version of memcached can be run both as a NT Serviceor from the command line. To install memcached as a service, followthe next steps:

  1. Unzip the binaries in your desired directory (eg.c:\memcached)
  2. Install the service using the command:'c:\memcached\memcached.exe -d install' from the command line
  3. Start the server from the Microsoft Management Console or byrunning the following command: 'c:\memcached\memcached.exe -dstart'
  4. Use the server, by default listening to port 11211

Building from source

To build from source, you will need Visual Studio 2005 (any editionwith C++ should work), Windows SDK (eg.WindowsSDK for Windows Server 2008 and .NET Framework 3.5)and libevent (win32binary provided on this page).
  1. Install Visual Studio 2005
  2. Install Windows SDK
  3. Put libevent.lib in Win32-Prj/ folder
  4. Open solution file and it should build

Downloads

memcached 1.2.4 Win32 Beta

  • memcached 1.2.4 Win32 BetaBinaries (09.03.2008)
  • memcached 1.2.4 Win32 BetaSource (09.03.2008)
  • memcached 1.2.4 Win32 Beta Patch for SVN revision 662 (tag1.2.4) (09.03.2008)

Libevent 1.3e Win32

(Needed if building from source)
  • Libevent1.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地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载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代码 复制代码 收藏代码java使用memcached
  1. <?php  
  2. $mem new Memcache;  
  3. $mem->connect("127.0.0.1"11211);  
  4. $mem->set('key''This is value!'0, 60);  
  5. $val $mem->get('key');  
  6. echo $val 
<?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包。

测试程序:

 

Java代码 复制代码 收藏代码java使用memcached
  1. import com.danga.MemCached.MemCachedClient;  
  2. import com.danga.MemCached.SockIOPool;  
  3.    
  4. public class MemCachedTest  
  5.    
  6.     private static MemCachedClient mcc new MemCachedClient();  
  7.    
  8.     static  
  9.         String[] servers {"192.168.123.100:11211"};  
  10.         //创建一个连接池  
  11.         SockIOPool pool SockIOPool.getInstance();  
  12.         //设置缓存服务器  
  13.         pool.setServers(servers);  
  14.         //设置初始化连接数,最小连接数,最大连接数以及最大处理时间  
  15.         pool.setInitConn(50);  
  16.         pool.setMinConn(50);  
  17.         pool.setMaxConn(500);  
  18.         pool.setMaxIdle(1000 60 60);  
  19.         //设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小  
  20.         pool.setMaintSleep(30);  
  21.         //关闭套接字缓存  
  22.         pool.setNagle(false);  
  23.         //连接建立后的超时时间  
  24.         pool.setSocketTO(3000);  
  25.         //连接建立时的超时时间  
  26.         pool.setSocketConnectTO(0);  
  27.         //初始化连接池  
  28.         pool.initialize();  
  29.      
  30.    
  31.     protected MemCachedTest(){  
  32.    
  33.      
  34.    
  35.     public static MemCachedClient getInstance(){  
  36.         return mcc;  
  37.      
  38.    
  39.     public static void main(String[] args)  
  40.    
  41.         MemCachedClient mcc= MemCachedTest.getInstance();  
  42.             for int 010i++  
  43.             boolean success mcc.set( "" i, "Hello!" );  
  44.             String result (String)mcc.get( "" );  
  45.             System.out.println( String.format( "set( %d ): %s"i, success );  
  46.             System.out.println( String.format( "get( %d ): %s"i, result );  
  47.          
  48.   
  49.         System.out.println( "\n\t -- sleeping --\n" );  
  50.         try Thread.sleep( 100000 ); catch Exception ex  
  51.   
  52.         for int 010i++  
  53.             boolean success mcc.set( "" i, "Hello!" );  
  54.             String result (String)mcc.get( "" );  
  55.             System.out.println( String.format( "set( %d ): %s"i, success );  
  56.             System.out.println( String.format( "get( %d ): %s"i, result );  
  57.          
  58.      
  59.  
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

 

 

本例的代码经过本人亲自测试!!

原创粉丝点击