<正则吃饺子> :关于redis集群的测试demo

时间:2022-11-17 18:48:28

redis集群的测试demo,来自网络,具体不详。

1、下载地址,如下:http://download.csdn.net/detail/u012543819/9729291

2、项目是java项目,结构如下:

<正则吃饺子> :关于redis集群的测试demo

3、类代码,如下:

package com.demo.redis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class RedisConnector {

private JedisCluster cluster;
private Set<HostAndPort> clusterNodes;

RedisConnector() {
connectRedis();
}

private void connectRedis() {
clusterNodes
= new HashSet<HostAndPort>();



clusterNodes.add(
new HostAndPort("123.123.123.123", 6661));
clusterNodes.add(
new HostAndPort("123.123.123.123", 6662));

clusterNodes.add(
new HostAndPort("123.123.123.123", 6663));
clusterNodes.add(
new HostAndPort("123.123.123.123", 6664));
clusterNodes.add(
new HostAndPort("123.123.123.123", 6665));
clusterNodes.add(
new HostAndPort("123.123.123.123", 6666));

cluster
= new JedisCluster(clusterNodes);

}

public void putData() {
// 1、测试多个
int max = 10;
for (int i = 0; i < max; i++) {
try {
if (cluster == null) {
cluster
= new JedisCluster(clusterNodes);
}
else {
String keyString
= "key" + i;
cluster.set(keyString, String.valueOf(i));
System.out.println(
"put:" + keyString + " " + "value:" + i);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

// 2、测试单个
// try {
// if (cluster == null) {
// cluster = new JedisCluster(clusterNodes);
// } else {
// String keyString = "name";
// String value = "nnnn";
// cluster.set(keyString, value);
// System.out.println("put:" + keyString + " " + "value:" + value);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }

}

public ArrayList<String> getData() {

ArrayList
<String> dataList = new ArrayList<String>();

int max = 6;
for (int i = 0; i < max; i++) {

try {
if (cluster == null) {
cluster
= new JedisCluster(clusterNodes);
}
else {
String keyString
= "key" + i;
dataList.add(cluster.get(keyString));
}
}
catch (Exception e) {

e.printStackTrace();
}

}
return dataList;
}

public void putHyperLog() {

// try {
// long revalue = cluster.pfadd("user","1");
// System.out.println(revalue);
// } catch (Exception e1) {
//
// e1.printStackTrace();
// }

int max = 10000;
for (int i = 0; i < max; i++) {

try {
if (cluster == null) {
cluster
= new JedisCluster(clusterNodes);
}
else {
String keyString
= "key_" + i;
for (int j = 0; j < 10; j++) {
long revalue = cluster.pfadd(keyString, String.valueOf(i));
System.out.println(i
+ "th" + revalue);
}

}
}
catch (Exception e) {

e.printStackTrace();
}

}

}

public void getHyperLogCount() {
try {

if (cluster == null) {
cluster
= new JedisCluster(clusterNodes);
}
else {
for (int i = 0; i < 10000; i++) {
long count = cluster.pfcount("key_" + i);
System.out.println(
"key_" + i + ": total " + count);
}

}
}
catch (Exception e) {

e.printStackTrace();
}

}

public void close() throws IOException {
cluster.close();
}
}

测试main方法

package com.demo.redis;

import java.util.ArrayList;

public class redisAccessStartup {

public static void main(String[] args) {

RedisConnector redisConnector
= new RedisConnector();

//测试放入数据
redisConnector.putData();

//测试取数据
// ArrayList<String> data = redisConnector.getData();
// for (int i = 0; i < data.size(); i++) {
// System.out.println("key" + i + " is:" + data.get(i));
// }

// redisConnector.putHyperLog();
// redisConnector.getHyperLogCount();

}

}

4、测试效果

<正则吃饺子> :关于redis集群的测试demo