jedis连接池使用实例

实例代码:

package com.bijian.study;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class TestPool {

	private Jedis jedis;

	private JedisPool jedisPool;

	private ShardedJedis shardedJedis;

	private ShardedJedisPool shardedJedisPool;

	public TestPool() {
		initialPool();
		initialShardedPool();
		shardedJedis = shardedJedisPool.getResource();
		jedis = jedisPool.getResource();
	}

	private void initialPool() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(20);
		config.setMaxIdle(5);
		config.setMaxWait(1000);
		config.setTestOnBorrow(false);
		jedisPool = new JedisPool(config, "192.168.128.129", 6379);
	}

	private void initialShardedPool() {
		// 池基本配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(20);
		config.setMaxIdle(5);
		config.setMaxWait(1000l);
		config.setTestOnBorrow(false);
		// slave链接
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo("192.168.128.129", 6379, "master"));

		// 构造池
		shardedJedisPool = new ShardedJedisPool(config, shards);
	}

	public static void main(String[] args) {
		new TestPool().show();
	}
	
	public void show() {
		testKey();
		testString();
		testList();
		testSet();
		testSortedSet();
		shardedJedisPool.returnResource(shardedJedis);
		jedisPool.returnResource(jedis);// add by lhs
	}

	private void testKey() {
		System.out.println("=============key==========================");
		// 清空数据
		System.out.println(jedis.flushDB());//OK
		System.out.println(jedis.echo("foo"));//foo
		// 判断key否存在
		System.out.println(shardedJedis.exists("foo"));//false
		shardedJedis.set("key", "values");
		System.out.println(shardedJedis.exists("key"));//true
	}

	private void testString() {
		System.out.println("=============String==========================");
		// 清空数据
		System.out.println(jedis.flushDB());//OK
		// 存储数据
		shardedJedis.set("foo", "bar");
		System.out.println(shardedJedis.get("foo"));//bar
		// 若key不存在,则存储
		shardedJedis.setnx("foo", "foo not exits");
		System.out.println(shardedJedis.get("foo"));//bar
		// 覆盖数据
		shardedJedis.set("foo", "foo update");
		System.out.println(shardedJedis.get("foo"));//foo update
		// 追加数据
		shardedJedis.append("foo", " hello, world");
		System.out.println(shardedJedis.get("foo"));//foo update hello, world
		// 设置key的有效期,并存储数据
		shardedJedis.setex("foo", 2, "foo not exits");
		System.out.println(shardedJedis.get("foo"));//foo not exits
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
		}
		System.out.println(shardedJedis.get("foo"));//null
		// 获取并更改数据
		shardedJedis.set("foo", "foo update");
		System.out.println(shardedJedis.getSet("foo", "foo modify"));//foo update
		// 截取value的值
		System.out.println(shardedJedis.getrange("foo", 1, 3));//oo
		System.out.println(jedis.mset("mset1", "mvalue1", "mset2", "mvalue2",
				"mset3", "mvalue3", "mset4", "mvalue4"));//OK
		System.out.println(jedis.mget("mset1", "mset2", "mset3", "mset4"));//[mvalue1,mvalue2,mvalue3,mvalue4]
		System.out.println(jedis.del(new String[] { "foo", "foo1", "foo3" }));//1
	}

	private void testList() {
		System.out.println("=============list==========================");
		// 清空数据
		System.out.println(jedis.flushDB());//OK
		// 添加数据
		shardedJedis.lpush("lists", "80");
		shardedJedis.lpush("lists", "75");
		shardedJedis.lpush("lists", "100");
		// 数组长度
		System.out.println(shardedJedis.llen("lists"));//3
		// 排序
		System.out.println(shardedJedis.sort("lists"));//[75,80,100]
		// 字串
		System.out.println(shardedJedis.lrange("lists", 0, 3));//[100,75,80]
		// 修改列表中单个值
		shardedJedis.lset("lists", 0, "hello list!");
		// 获取列表指定下标的值
		System.out.println(shardedJedis.lindex("lists", 1));//75
		// 删除列表指定下标的值
		System.out.println(shardedJedis.lrem("lists", 1, "80"));//1
		// 删除区间以外的数据[保留指定key的值范围内的数据]
		System.out.println(shardedJedis.ltrim("lists", 0, 1));//OK
		// 列表出栈
		System.out.println(shardedJedis.lpop("lists"));//hello list!
		// 整个列表值
		System.out.println(shardedJedis.lrange("lists", 0, -1));//[75]

	}

	private void testSet() {
		System.out.println("=============set==========================");
		// 清空数据
		System.out.println(jedis.flushDB());//OK
		// 添加数据
		shardedJedis.sadd("sets", "HashSet");
		shardedJedis.sadd("sets", "SortedSet");
		shardedJedis.sadd("sets", "TreeSet");
		// 判断value是否在列表中
		System.out.println(shardedJedis.sismember("sets", "TreeSet"));//true
		// 整个列表值
		System.out.println(shardedJedis.smembers("sets"));//[SortedSet, TreeSet, HashSet]
		// 删除指定元素
		System.out.println(shardedJedis.srem("sets", "SortedSet"));//1
		// 出栈
		System.out.println(shardedJedis.spop("sets"));//TreeSet
		System.out.println(shardedJedis.smembers("sets"));//[HashSet]
		
		shardedJedis.sadd("sets1", "HashSet1");
		shardedJedis.sadd("sets1", "SortedSet1");
		shardedJedis.sadd("sets1", "TreeSet");
		shardedJedis.sadd("sets2", "HashSet2");
		shardedJedis.sadd("sets2", "SortedSet1");
		shardedJedis.sadd("sets2", "TreeSet1");
		// 交集
		System.out.println(jedis.sinter("sets1", "sets2"));//[SortedSet1]
		// 并集
		System.out.println(jedis.sunion("sets1", "sets2"));//[HashSet2, HashSet1, TreeSet1, SortedSet1, TreeSet]
		// 差集
		System.out.println(jedis.sdiff("sets1", "sets2"));//[TreeSet, HashSet1]
	}

	private void testSortedSet() {
		System.out.println("=============zset==========================");
		// 清空数据
		System.out.println(jedis.flushDB());//OK
		// 添加数据
		shardedJedis.zadd("zset", 10.1, "hello");
		shardedJedis.zadd("zset", 10.0, ":");
		shardedJedis.zadd("zset", 9.0, "zset");
		shardedJedis.zadd("zset", 11.0, "zset!");
		// 元素个数
		System.out.println(shardedJedis.zcard("zset"));//4
		// 元素下标
		System.out.println(shardedJedis.zscore("zset", "zset"));//9.0
		// 集合子集
		System.out.println(shardedJedis.zrange("zset", 0, -1));//[zset, :, hello, zset!]
		// 删除元素
		System.out.println(shardedJedis.zrem("zset", "zset!"));//1
		// 返回集合中score在给定区间的数量
		System.out.println(shardedJedis.zcount("zset", 9.5, 10.5));//2
		// 整个集合值
		System.out.println(shardedJedis.zrange("zset", 0, -1));//[zset, :, hello]
	}
}

运行结果:

=============key==========================
OK
foo
false
true
=============String==========================
OK
bar
bar
foo update
foo update hello, world
foo not exits
null
foo update
oo 
OK
[mvalue1, mvalue2, mvalue3, mvalue4]
1
=============list==========================
OK
3
[75, 80, 100]
[100, 75, 80]
75
1
OK
hello list!
[75]
=============set==========================
OK
true
[SortedSet, TreeSet, HashSet]
1
TreeSet
[HashSet]
[SortedSet1]
[HashSet2, HashSet1, TreeSet1, SortedSet1, TreeSet]
[TreeSet, HashSet1]
=============zset==========================
OK
4
9.0
[zset, :, hello, zset!]
1
2
[zset, :, hello]

 

PS:如上实例是基于jedis-2.0.0.jar、commons-pool-1.6.jar运行的。

 

文章来源:http://blog.sina.com.cn/s/blog_5d97745a01018gsh.html

你可能感兴趣的:(redis,jedis连接池,jedis)