用jedis操作redis内存数据库:
package sn.sniper.redis;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.Transaction;
public class RedisUtil {
private String host;
private int port;
@Before
public void init() {
//this.host = "192.168.31.231";
this.host = "192.168.1.231";
this.port = 6379;
}
@Test
public void testIncr() {
Jedis jedis = new Jedis(host, port);
String value = jedis.get("sniper");
value = String.valueOf(Integer.parseInt(value)+1);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
jedis.set("sniper", value);
jedis.close();
}
@Test
public void testTransaction() {
Jedis jedis = new Jedis(host, port);
Transaction t = jedis.multi();
//t.incr("sniper");
t.set("sniper", "x");
t.incr("sniper");
int i = 10/0;
t.set("sniper1", "x");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.exec();
jedis.close();
}
/**
* 单连接方式
*/
@Test
public void testSingle() {
Jedis jedis = new Jedis(host, port);
//指定连接数据库
jedis.select(1);
jedis.set("sniper", "bbb");
String value = jedis.get("sniper");
System.out.println(value);
jedis.close();
}
/**
* 连接池方式
*/
@Test
public void testPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(10);
poolConfig.setMaxTotal(100);
poolConfig.setMaxWaitMillis(10000);
poolConfig.setTestOnBorrow(true);
JedisPool pool = new JedisPool(poolConfig, host, port);
Jedis jedis = pool.getResource();
jedis.set("sniper", "bbb");
String value = jedis.get("sniper");
//用完后放回连接池
pool.returnResourceObject(jedis);
System.out.println(value);
}
/**
* 订阅频道
*/
@Test
public void subscribe() {
Jedis jedis = new Jedis(host, port);
JedisPubSub jedisPubSub = new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println(channel + ":" + message);
}
};
jedis.subscribe(jedisPubSub, "sniper");
}
/**
* 发布消息
*/
@Test
public void publish() {
Jedis jedis = new Jedis(host, port);
jedis.publish("sniper", "hello");
}
@Test
public void testPipeline() {
//方式一:请求了1000次,执行了1000次
Jedis jedis = new Jedis(host, port);
long begin = System.currentTimeMillis();
for(int i=0; i<1000; i++) {
jedis.set("a" + i, i+"");
}
System.out.println(System.currentTimeMillis() - begin);
//方式二:请求了一次
Pipeline p = jedis.pipelined();
begin = System.currentTimeMillis();
for(int i=0; i<1000; i++) {
p.set("b" + i, i+"");
}
p.sync();
System.out.println(System.currentTimeMillis() - begin);
}
/**
* 主从结构
*/
@Test
public void testMore() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
List shards = new ArrayList();
JedisShardInfo shard1 = new JedisShardInfo("192.168.31.231", "6379");
JedisShardInfo shard2 = new JedisShardInfo("192.168.31.231", "7000");
JedisShardInfo shard3 = new JedisShardInfo("192.168.31.231", "7001");
shards.add(shard1);
shards.add(shard2);
shards.add(shard3);
ShardedJedisPool pool = new ShardedJedisPool(poolConfig, shards);
ShardedJedis jedis = pool.getResource();
String value = jedis.get("sniper");
System.out.println(value);
}
/**
* 主从结构,sentinel自动failover连接
*/
@Test
public void testSentinel() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
Set sentinels = new HashSet();
sentinels.add("192.168.31.231:26379");
//sentinels.add("192.168.31.231:26793");
//让sentinel自动选择当前可用的主节点
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, poolConfig);
HostAndPort hostAndPort = pool.getCurrentHostMaster();
System.out.println(hostAndPort.getHost() + ":" + hostAndPort.getPort());
Jedis jedis = pool.getResource();
String value = jedis.get("sniper");
System.out.println(value);
}
}