redis使用
package org.arrow.redis.test;
import com.google.common.collect.Sets;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import redis.clients.jedis.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by root on 2016/7/6.
*/
public class JedisTest {
public static void main(String[] args) throws IOException {
Set<HostAndPort> hostPorts = Sets.newHashSet();
Set<String> strs = Sets.newHashSet();
Set<String> strings = Sets.newHashSet();
hostPorts.add(new HostAndPort("192.168.2.45", 6379));
hostPorts.add(new HostAndPort("192.168.2.46", 6379));
hostPorts.add(new HostAndPort("192.168.2.47", 6379));
hostPorts.add(new HostAndPort("192.168.2.45", 6380));
hostPorts.add(new HostAndPort("192.168.2.46", 6380));
hostPorts.add(new HostAndPort("192.168.2.47", 6380));
final JedisCluster redisCluster = new JedisCluster(hostPorts);
Map<String, JedisPool> nodes = redisCluster.getClusterNodes();
Iterator<Map.Entry<String, JedisPool>> iterNodes = nodes.entrySet().iterator();
while (iterNodes.hasNext()) {
Map.Entry<String, JedisPool> entry = iterNodes.next();
Jedis jedis = entry.getValue().getResource();
System.out.println(entry.getKey());
}
redisCluster.set("foo", "bar");
redisCluster.set("foo", "1234567jtt");
System.out.println(redisCluster.get("foo"));
for (int i = 0; i < 100; i++) {
redisCluster.rpush("mylist", "e-" + i);
}
for (int i = 0; i < 30; i++) {
String str = redisCluster.lpop("mylist");
}
List<String> userList = redisCluster.lrange("mylist", 0, -1);
System.out.println(userList);
for (int i = 0; i < 30; i++) {
String str = redisCluster.lpop("mylist");
}
Long length = redisCluster.llen("mylist");
System.out.println("length = " + length);
System.out.println(redisCluster.lrange("mylist", 0, 3));
redisCluster.lset("mylist", 0, "hello list!");
System.out.println(redisCluster.lindex("mylist", 1));
System.out.println(redisCluster.lrem("mylist", 1, "vector"));
Long s = redisCluster.del("mylist");
System.out.println("deleted = " + s);
System.out.println(redisCluster.echo("abcdddd"));
System.out.println(redisCluster.exists("foo"));
redisCluster.append("foo", "append hello, world");
System.out.println(redisCluster.get("foo"));
redisCluster.setex("foo1", 2, "foo is exp!");
System.out.println(redisCluster.get("foo1"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(redisCluster.get("foo1"));
System.out.println(redisCluster.getrange("foo", 0, 3));
redisCluster.zadd("zhongsou:hackers", 1940, "Alan Kay");
redisCluster.zadd("zhongsou:hackers", 1953, "Richard Stallman");
redisCluster.zadd("zhongsou:hackers", 1943, "Jay");
redisCluster.zadd("zhongsou:hackers", 1920, "Jellon");
redisCluster.zadd("zhongsou:hackers", 1965, "Yukihiro Matsumoto");
redisCluster.zadd("zhongsou:hackers", 1916, "Claude Shannon");
redisCluster.zadd("zhongsou:hackers", 1969, "Linus Torvalds");
redisCluster.zadd("zhongsou:hackers", 1912, "Alan Turing");
Set<String> hackers = redisCluster.zrange("zhongsou:hackers", 0, -1);
System.out.println(hackers);
Set<String> hackers2 = redisCluster.zrevrange("zhongsou:hackers", 0, -1);
System.out.println(hackers2);
Set<String> hackers3 = redisCluster.zrangeByScore("zhongsou:hackers", "-inf", "1920");
System.out.println(hackers3);
try {
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
final int kkk = i;
final Sub sub = new Sub("id:" + i);
service.submit(new Runnable() {
@Override
public void run() {
redisCluster.psubscribe(sub, "mychannel", "mychannel" + kkk);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
ExecutorService service = Executors.newFixedThreadPool(3);
for (int i = 0; i < 3; i++) {
final int kkk = i;
service.submit(new Runnable() {
@Override
public void run() {
while (true) {
redisCluster.publish("mychannel" + kkk, "msg-" + new Random().nextInt(10));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
System.in.read();
}
}
/**
* 订阅者
*/
final class Sub extends JedisPubSub {
private String name;
public Sub(String name){
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void onMessage(String channel, String message) {
System.out.println(name + " from channel = " + channel + ", message = " + message);
}
public void onPMessage(String pattern, String channel, String message) {
}
public void onSubscribe(String channel, int subscribedChannels) {
}
public void onUnsubscribe(String channel, int subscribedChannels) {
}
public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
public void onPSubscribe(String pattern, int subscribedChannels) {
}
}