找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3719|回复: 0

Memcached使用入门——(1)Memcached基础及示例程序

[复制链接]
发表于 2011-12-20 11:49:08 | 显示全部楼层 |阅读模式
1. What is Memcached?
Memcached是一个免费开源、高性能、分布式的内存对象缓存系统。Memcached是在内存中,为特定数据(字符串或对象)构建key-value的小块数据存储。

2. 下载Memcached的服务器端软件
Windows平台版本下载:http://splinedancer.com/memcache ... ew-20080309_bin.zip
Linux平台版本下载:http://memcached.googlecode.com/files/memcached-1.4.10.tar.gz

3. 在服务器上部署Memcached Server
以下以Windows平台为例:
参考:http://www.codeforest.net/how-to-install-memcached-on-windows-machine
下载下来的Windows版本解压到C:/memcached/
在控制台输入命令安装:
view plaincopy to clipboardprint?

  • c:/memcached/memcached.exe  -d install  

c:/memcached/memcached.exe  -d install
启动:

view plaincopy to clipboardprint?

  • c:/memcached/memcached.exe -d  start  

c:/memcached/memcached.exe -d  start
或:

view plaincopy to clipboardprint?

  • net start "memcached Server"  

net start "memcached Server"
默认的缓存大小为64M,如果不够用,请打开注册表,找到:

view plaincopy to clipboardprint?

  • HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached  Server .  

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached  Server .
将其内容修改为:

view plaincopy to clipboardprint?

  • “C:/memcached/memcached.exe” -d runservice -m 512  

“C:/memcached/memcached.exe” -d runservice -m 512



4. 下载Memcached的客户端API包
下载地址:http://spymemcached.googlecode.com/files/memcached-2.5.jar

5. 编写一个Java数据类
view plaincopy to clipboardprint?

  1. package com.sinosuperman.memcached;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5.   
  6. public class User implements Serializable{   
  7.   
  8.     private static final long serialVersionUID = -372274003834027815L;  
  9.   
  10.     String userId;  
  11.       
  12.     public User(String userId) {   
  13.         super();   
  14.         this.userId = userId;   
  15.     }  
  16.       
  17.     public String getUserId() {   
  18.         return userId;   
  19.     }   
  20.       
  21.     public void setUserId(String userId) {   
  22.         this.userId = userId;   
  23.     }   
  24.          
  25.     @Override   
  26.     public String toString() {   
  27.         // TODO Auto-generated method stub  
  28.   
  29.         StringBuffer sb=new StringBuffer();   
  30.         sb.append("userId="+this.userId);   
  31.         return sb.toString();   
  32.     }   
  33. }   package com.sinosuperman.memcached;
  34. import java.io.Serializable;
  35. public class User implements Serializable{
  36.         private static final long serialVersionUID = -372274003834027815L;
  37.         String userId;
  38.        
  39.         public User(String userId) {
  40.                 super();
  41.                 this.userId = userId;
  42.         }
  43.        
  44.         public String getUserId() {
  45.                 return userId;
  46.         }
  47.        
  48.         public void setUserId(String userId) {
  49.                 this.userId = userId;
  50.         }
  51.                
  52.         @Override
  53.         public String toString() {
  54.                 // TODO Auto-generated method stub
  55.                 StringBuffer sb=new StringBuffer();
  56.                 sb.append("userId="+this.userId);
  57.                 return sb.toString();
  58.         }
  59. }
复制代码



6. 编写一个Memcached的客户端
view plaincopy to clipboardprint?

  1. package com.sinosuperman.memcached;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetSocketAddress;  
  5.   
  6. import net.spy.memcached.MemcachedClient;  
  7.   
  8. public class TestMemcached {  
  9.     public static void main(String[] args) throws IOException {  
  10.         MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));  
  11.         for (int i = 1; i < 10; i++) {  
  12.             cache.set("T0001" + i, 3600, new User(i + ""));   
  13.         }  
  14.         User myObject = (User) cache.get("T00011");  
  15.         System.out.println("Get object from mem :" + myObject);   
  16.     }   
  17. }  package com.sinosuperman.memcached;
  18. import java.io.IOException;
  19. import java.net.InetSocketAddress;
  20. import net.spy.memcached.MemcachedClient;
  21. public class TestMemcached {
  22.         public static void main(String[] args) throws IOException {
  23.                 MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  24.                 for (int i = 1; i < 10; i++) {
  25.                         cache.set("T0001" + i, 3600, new User(i + ""));       
  26.                 }
  27.                 User myObject = (User) cache.get("T00011");
  28.                 System.out.println("Get object from mem :" + myObject);
  29.         }
  30. }
复制代码
7. 运行测试

运行结果应该如下:
view plaincopy to clipboardprint?

  1. 2011-12-15 17:25:30.276 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue  
  2. 2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080  
  3. Get object from mem :userId=1  2011-12-15 17:25:30.276 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
  4. 2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080
  5. Get object from mem :userId=1
复制代码
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-19 09:49 , Processed in 0.013536 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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