1.安装并测试repcached
1.下载memcached-1.2.8-repcached-2.2.1.tar.gz
地址:http://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/
2. 在server2,server3上分别安装repcached
首先安装libevent,libevent-devel:
[root@server2 Share]# rmp -ivh libevent-1.4.13-4.el6.x86_64.rpm
[root@server2 Share]# rmp -ivh libevent-devel-1.4.13-4.el6.x86_64.rpm
或者在CentOS下直接运行 yum libevent libevent-devel
安装repcached
[root@server3 bin]# ./configure --enable-replication --program-transform-name=s/memcached/repcached/
[root@server3 bin]# make
[root@server3 bin]# make install
3.启动repcached
切换到非root用户
[root@server3 bin]# su admin
server2 上以master身份运行,server3中以slave身份运行
[admin@server2 ~]$ /usr/local/bin/repcached -p 11211 -v
[admin@server2 ~]$ replication: listen
[admin@server3 ~]$ /usr/local/bin/repcached -p 11211 -x server2 -v
replication: connect (peer=192.168.1.202:11212)
replication: marugoto copying
replication: start
开启两个终端,分别连接server2,server3的11211端口
[admin@server1 ~]$ telnet server2 11211
Trying 192.168.1.202...
Connected to server2.
Escape character is '^]'.
[admin@server1 ~]$ telnet server3 11211
Trying 192.168.1.203...
Connected to server3.
Escape character is '^]'.
在server2中设置一个变量aa
set aa 0 0 5
bbbbb
STORED
get aa
VALUE aa 0 5
bbbbb
END
在server3中查询变量aa
get aa
VALUE aa 0 5
bbbbb
END
在server3中修改aa的值
append aa 0 0 3
ccc
STORED
get aa
VALUE aa 0 8
bbbbbccc
END
在server2中查看aa
get aa
VALUE aa 0 8
bbbbbccc
END
在server3中删除aa
delete aa
DELETED
get aa
END
在server2中查看aa
delete aa
NOT_FOUND
get aa
END
2. Java操作memcached
使用JDBC连接PostGreSQL数据库,查询一段数据并保存在memcached。分别再从memcached和PostGreSQL中查询该段数据,比较二者时间差异。
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class MemChachedTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
String postGresURL = "jdbc:postgresql://10.1.20.202:5432/testdb";
Connection con = null;
Statement st = null;
String[] memCachedServer = {"10.1.20.202:11211"};
SockIOPool pool = null;
List<String> cacheValues = new ArrayList<String>();
try {
//连接数据库
con = DriverManager.getConnection(postGresURL, "username", "password");
st = con.createStatement();
//从数据中查询,并取出结果
String sql = "select group.display_name from group, group_item, device "
+ "where group_item.group_id= group.id and group_item.item_id= device.id; ";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
cacheValues.add(rs.getString(1));
}
rs.close();
//连接MemCached,并保存结果
pool = SockIOPool.getInstance();
pool.setServers(memCachedServer);
pool.initialize();
MemCachedClient memCachedClient = new MemCachedClient();
memCachedClient.set("cacheValues", cacheValues);
//再次查询数据库,并计算所耗时间
long beforeQuery = System.currentTimeMillis();
rs = st.executeQuery(sql);
long afterQuery = System.currentTimeMillis();
System.out.println(" Cost Time of Query from PostGreSQL (ms):" + (afterQuery - beforeQuery));
//查询MemCached,并计算所耗时间
long beforeGet = System.currentTimeMillis();
List<String> resultCached = (List<String>) memCachedClient.get("cacheValues");
long afterGet = System.currentTimeMillis();
System.out.println(" Cost Time of Get from MemCached (ms):" + (afterGet - beforeGet));
//打印从数据库中查询的结果
System.out.println(" -----data from PostGreSQL------");
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
//打印从MemCached查询的结果
System.out.println(" -----data from MemCached------");
for (String s :resultCached) {
System.out.println(s);
}
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
if(pool!=null)
{
pool.shutDown();
}
}
}
}
运行所得结果:
Cost Time of Query from PostGreSQL (ms):25
Cost Time of Get from MemCached (ms):8
-----data from PostGreSQL------
Net-192.168.0.0/24
Windows
Net-211.144.207.8/29
Net-10.1.70.0/24
Net-10.1.40.0/24
Net-10.1.30.0/24
Net-10.1.20.0/22
。。。。。。。。。。。。。。。。。。。。。。。。。。。
-----data from MemCached------
Net-192.168.0.0/24
Windows
Net-211.144.207.8/29
Net-10.1.70.0/24
Net-10.1.40.0/24
Net-10.1.30.0/24
Net-10.1.20.0/22
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
多运行几次,比较二者消耗时间的差异:
Cost Time of Query from PostGreSQL (ms):17
Cost Time of Get from MemCached (ms):8
Cost Time of Query from PostGreSQL (ms):18
Cost Time of Get from MemCached (ms):8
Cost Time of Query from PostGreSQL (ms):16
Cost Time of Get from MemCached (ms):7
Cost Time of Query from PostGreSQL (ms):20
Cost Time of Get from MemCached (ms):7
可以从PostGreSQL中查询数据所消耗的时间大约是从MemCached中查询的所消耗的两倍。因为数据库表记录比较少,所以性能提升不明显。