winston 发表于 2011-12-20 11:49:08

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

1. What is Memcached?
Memcached是一个免费开源、高性能、分布式的内存对象缓存系统。Memcached是在内存中,为特定数据(字符串或对象)构建key-value的小块数据存储。

2. 下载Memcached的服务器端软件
Windows平台版本下载:http://splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-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 -dstart
c:/memcached/memcached.exe -dstart
或:

view plaincopy to clipboardprint?

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

view plaincopy to clipboardprint?

[*]HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcachedServer .
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcachedServer .
将其内容修改为:

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?




package com.sinosuperman.memcached;

import java.io.Serializable;


public class User implements Serializable{   

    private static final long serialVersionUID = -372274003834027815L;

    String userId;
      
    public User(String userId) {   
      super();   
      this.userId = userId;   
    }
      
    public String getUserId() {   
      return userId;   
    }   
      
    public void setUserId(String userId) {   
      this.userId = userId;   
    }   
         
    @Override   
    public String toString() {   
      // TODO Auto-generated method stub

      StringBuffer sb=new StringBuffer();   
      sb.append("userId="+this.userId);   
      return sb.toString();   
    }   
}   package com.sinosuperman.memcached;

import java.io.Serializable;


public class User implements Serializable{

        private static final long serialVersionUID = -372274003834027815L;

        String userId;
       
        public User(String userId) {
                super();
                this.userId = userId;
        }
       
        public String getUserId() {
                return userId;
        }
       
        public void setUserId(String userId) {
                this.userId = userId;
        }
               
        @Override
        public String toString() {
                // TODO Auto-generated method stub
                StringBuffer sb=new StringBuffer();
                sb.append("userId="+this.userId);
                return sb.toString();
        }
}



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




package com.sinosuperman.memcached;

import java.io.IOException;
import java.net.InetSocketAddress;

import net.spy.memcached.MemcachedClient;

public class TestMemcached {
    public static void main(String[] args) throws IOException {
      MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
      for (int i = 1; i < 10; i++) {
            cache.set("T0001" + i, 3600, new User(i + ""));   
      }
      User myObject = (User) cache.get("T00011");
      System.out.println("Get object from mem :" + myObject);   
    }   
}package com.sinosuperman.memcached;

import java.io.IOException;
import java.net.InetSocketAddress;

import net.spy.memcached.MemcachedClient;

public class TestMemcached {
        public static void main(String[] args) throws IOException {
                MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
                for (int i = 1; i < 10; i++) {
                        cache.set("T0001" + i, 3600, new User(i + ""));       
                }
                User myObject = (User) cache.get("T00011");
                System.out.println("Get object from mem :" + myObject);
        }
}
7. 运行测试

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




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
2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection:Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080
Get object from mem :userId=12011-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
2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection:Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080
Get object from mem :userId=1
页: [1]
查看完整版本: Memcached使用入门——(1)Memcached基础及示例程序