Memcached(一)在Windows上安装和测试memcached
1)下载memcached的windows安装程序
memcached-1.2.4-Win32-Preview-20080309_bin.zip 或其他版本
2)解压memcached 用管理员身份运行cmd.exe cd到解压目录下 执行 memcached.exe -d install 安装memcached
3)启动memcached memcached.exe -d start
然后测试程序
package com.guowuxin.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)); System.out.println("Connect success"); for (int i = 1; i < 10; i++) { cache.set("guowuxin" + i, 3600, new User(i + "","guowuxin","guowuxin")); } System.out.println("insert success"); User myObject = (User) cache.get("guowuxin1"); System.out.println("Get object from mem :" + myObject); } }
package com.guowuxin.memcached; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = -372274003834027815L; private String userId; private String username; private String password; public User(final String userId,final String username,final String password) { super(); this.userId = userId; this.username = username; this.password = password; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { StringBuffer sb=new StringBuffer(); sb.append("userId="+this.userId); sb.append("&username="+this.username); sb.append("&password="+this.password); return sb.toString(); } }
2016-02-25 15:26:17.110 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
Connect success
2016-02-25 15:26:17.113 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@fa9cf
insert success
Get object from mem :userId=1&username=guowuxin&password=guowuxin