public static void main(String[] args)
{
Jedis jedis = new Jedis("192.168.110.101", 6379);
String result = jedis.ping(); // 测试连接
System.out.println(result);
jedis.close();
}
//关键字(Keys)
System.out.println("exists :" + jedis.exists("name"));
System.out.println("del :" + jedis.del("name"));
//字符串(String)
System.out.println("set :" + jedis.set("name", "李志伟"));
System.out.println("get :" + jedis.get("name"));
//哈希(Hashs)
for (int i = 0; i < 10; i++)
{
System.out.println("hset :" + jedis.hset("hset", "set-key" + i, "set-value" + i));
}
System.out.println("hkeys :" + jedis.hkeys("hset"));
//列表(Lists)
System.out.println("rpush :" + jedis.rpush("lset", "lset001", "lset002", "lset003", "lset004"));
System.out.println("lrange :" + jedis.lrange("lset", 0, 2));
//集合(Sets)
System.out.println("sadd :" + jedis.sadd("sadd", "sadd001", "sadd002", "sadd003"));
System.out.println("scard :" + jedis.scard("sadd"));
//有序集合(Sorted Sets)
Map<String, Double> scoreMembers = new HashMap<String, Double>();
scoreMembers.put("001", 0.1D);
scoreMembers.put("002", 0.2D);
scoreMembers.put("003", 0.3D);
System.out.println("zadd :" + jedis.zadd("zadd", scoreMembers));
System.out.println("zrange :" + jedis.zrange("zadd", 1L, 2L));
//HyperLogLog
for (int i = 0; i < 10; i++)
{
System.out.println("pfadd :" + jedis.pfadd("HyperLogLog", UUID.randomUUID().toString()));
}
System.out.println("pfcount :" + jedis.pfcount("HyperLogLog"));
//发布/订阅(Pub/Sub)
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
final Jedis j = new Jedis("192.168.110.101", 6379);
j.subscribe(new JedisPubSub()
{
@Override
public void onMessage(String channel, String message)
{
System.out.println("onMessage--channel:" + channel + " message:" + message);
this.unsubscribe();
}
}, "channel001");
j.close();
System.out.println("连接已关闭");
}
});
thread.start();
Thread.sleep(10);
System.out.println("publish :" + jedis.publish("channel001", "发送一条消息"));
//事务(Transactions)
Transaction transaction = jedis.multi();
System.out.println("set :" + transaction.set("multi001", "123"));
System.out.println("incr :" + transaction.incr("multi001"));
System.out.println("transaction.exec :" + transaction.exec());
// 脚本(Scripting)
System.out.println("eval :" + jedis.eval("local msg = \"Hello, world!\" return msg"));
//连接(Connection)
System.out.println("ping :" + jedis.ping());
System.out.println("select :" + jedis.select(0));
//服务(Server)
System.out.println("dbSize :" + jedis.dbSize());
System.out.println("clientList :" + jedis.clientList());
public static void main(String[] args)
{
JedisPoolConfig config = new JedisPoolConfig();
// 连接池中最大连接数。高版本:maxTotal,低版本:maxActive
config.setMaxTotal(8);
// 连接池中最大空闲的连接数
config.setMaxIdle(4);
// 连接池中最少空闲的连接数
config.setMinIdle(1);
// 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait
config.setMaxWaitMillis(15000);
// 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除
config.setMinEvictableIdleTimeMillis(300000);
// 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3
config.setNumTestsPerEvictionRun(3);
// “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1
config.setTimeBetweenEvictionRunsMillis(60000);// 一分钟
// 向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值
config.setTestOnBorrow(false);
// 向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值
config.setTestOnReturn(false);
// 向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值
config.setTestWhileIdle(false);
JedisPool pool = new JedisPool(config, "192.168.110.101", 6379);
Jedis jedis = pool.getResource();// 从pool中获取资源
try
{
jedis.set("k1", "v1");
System.out.println(jedis.get("k1"));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
jedis.close();
// pool.returnResource(jedis); // 此方法已过时
}
for (int i = 0; i < 10; i++)
{
jedis = pool.getResource();
// jedis.close(); // 去掉注释观察效果
System.out.println("NumActive:" + pool.getNumActive());
System.out.println("NumIdle:" + pool.getNumIdle());
System.out.println("NumWaiters:" + pool.getNumWaiters());
}
pool.close();
pool.destroy();
}
public static void main(String[] args)
{
String host = null;
int port = 0;
Set<String> sentinels = new HashSet<String>();
sentinels.add("192.168.110.100:26379");
sentinels.add("192.168.110.100:36379");
sentinels.add("192.168.110.100:46379");
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("master001", sentinels);
host = jedisSentinelPool.getCurrentHostMaster().getHost();
port = jedisSentinelPool.getCurrentHostMaster().getPort();
System.out.println(host + ":" + port);
Jedis jedis = jedisSentinelPool.getResource();
jedis.set("001", "ASDFG");
System.out.println(jedis.get("001"));
jedis.close();
// 关闭Redis Master服务
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.println(input);
host = jedisSentinelPool.getCurrentHostMaster().getHost();
port = jedisSentinelPool.getCurrentHostMaster().getPort();
System.out.println(host + ":" + port);
jedis = jedisSentinelPool.getResource();
jedis.set("001", "ZXCVB");
System.out.println(jedis.get("001"));
jedis.close();
jedisSentinelPool.close();
jedisSentinelPool.destroy();
}
192.168.110.102:6379
ASDFG
2015-10-2 22:50:30 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 192.168.110.101:6379
192.168.110.101:6379
ZXCVB
public static void main(String[] args)
{
JedisPoolConfig config = new JedisPoolConfig();
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("192.168.110.101", "Redis001", 6379, 20 * 1000, 1));
shards.add(new JedisShardInfo("192.168.110.102", "Redis002", 6379, 20 * 1000, 2));
shards.add(new JedisShardInfo("192.168.110.103", "Redis003", 6379, 20 * 1000, 4));
ShardedJedisPool shardedJedisPool =new ShardedJedisPool(config, shards);
for (int i = 0; i < 10; i++)
{
ShardedJedis shardedJedis = shardedJedisPool.getResource();
String key = "shard" + i;
shardedJedis.set(key, "v-" + i);
System.out.println(shardedJedis.get(key));
JedisShardInfo shardInfo = shardedJedis.getShardInfo(key);
System.out.println("getHost:" + shardInfo.getHost());
shardedJedis.close();
}
shardedJedisPool.close();
shardedJedisPool.destroy();
}
v-0
getHost:192.168.110.102
v-1
getHost:192.168.110.101
v-2
getHost:192.168.110.102
v-3
getHost:192.168.110.103
v-4
getHost:192.168.110.102
v-5
getHost:192.168.110.102
v-6
getHost:192.168.110.103
v-7
getHost:192.168.110.102
v-8
getHost:192.168.110.102
v-9
getHost:192.168.110.103
//位置:redis.clients.util.Sharded<R, S extends ShardInfo<R>>
private void initialize(List<S> shards)
{
nodes = new TreeMap<Long, S>();
for (int i = 0; i != shards.size(); ++i)
{
final S shardInfo = shards.get(i);
if (shardInfo.getName() == null)
for (int n = 0; n < 160 * shardInfo.getWeight(); n++)
{
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);
}
else
for (int n = 0; n < 160 * shardInfo.getWeight(); n++)
{
nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);
}
resources.put(shardInfo, shardInfo.createResource());
}
}
原文:http://www.cnblogs.com/LiZhiW/p/4852930.html