实战Memcached缓存系统(1)Memcached基础及示例程序
来源:未知 责任编辑:智问网络 发表时间:2013-09-22 21:30 点击:次
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/
在控制台输入命令安装:
[plain]
c:/memcached/memcached.exe -d install
启动:
[plain]
c:/memcached/memcached.exe -d start
或:
[plain]
net start "memcached Server"
默认的缓存大小为64M,如果不够用,请打开注册表,找到:
[plain]
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server .
将其内容修改为:
[plain]
“C:/memcached/memcached.exe” -d runservice -m 512
4. 下载Memcached的客户端API包
下载地址:http://spymemcached.googlecode.com/files/memcached-2.5.jar
5. 编写一个Java数据类
[java]
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的客户端
[java]
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. 运行测试
运行结果应该如下:
[java]
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=1
尊重知识,转载请注明本文来自:编程艺术家Poechant的CSDN博客http://blog.csdn.net/poechant
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/
在控制台输入命令安装:
[plain]
c:/memcached/memcached.exe -d install
启动:
[plain]
c:/memcached/memcached.exe -d start
或:
[plain]
net start "memcached Server"
默认的缓存大小为64M,如果不够用,请打开注册表,找到:
[plain]
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server .
将其内容修改为:
[plain]
“C:/memcached/memcached.exe” -d runservice -m 512
4. 下载Memcached的客户端API包
下载地址:http://spymemcached.googlecode.com/files/memcached-2.5.jar
5. 编写一个Java数据类
[java]
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的客户端
[java]
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. 运行测试
运行结果应该如下:
[java]
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=1
尊重知识,转载请注明本文来自:编程艺术家Poechant的CSDN博客http://blog.csdn.net/poechant
相关新闻>>
- SQLite缓存机制解析
- 实战MongoDB-Replication之Master-Slave
- MongoDB实战系列之三:MongoDB的主从部署
- MongoDB实战系列之四:mongodb副本集部署
- 实战Memcached缓存系统(2)Memcached Java API基础之MemcachedC
- 实战Memcached缓存系统(3)Memcached配置参数初解
- 实战Memcached缓存系统(6)Memcached CAS的多线程程序实例
- 实战Memcached缓存系统(8)Memcached异步实时读写问题的解决方
- MongoDB实战系列之五:mongodb的分片配置
- 实战Memcached缓存系统(4)Memcached的CAS协议
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>