edis.jar包:https://github.com/xetorthio/jedis/downloads java
commons-pool:http://commons.apache.org/pool/download_pool.cgi git
添加到lib github
個人版本:jedis-2.1.0.jar commons-pool-1.6-bin.zip redis
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; /** * redis的Java客戶端Jedis測試驗證 * * @author */ public class Test { /** * 非切片客戶端連接 */ private Jedis jedis; /** * 非切片連接池 */ private JedisPool jedisPool; /** * 切片客戶端連接 */ private ShardedJedis shardedJedis; /** * 切片連接池 */ private ShardedJedisPool shardedJedisPool; /** * 構造函數 */ public Test() { initialPool(); initialShardedPool(); shardedJedis = shardedJedisPool.getResource(); jedis = jedisPool.getResource(); } private void initialPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(1000l); config.setTestOnBorrow(false); jedisPool = new JedisPool(config, "192.168.1.102", 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.120.51", 6379, "master")); // 構造池 shardedJedisPool = new ShardedJedisPool(config, shards); } public void show() { // key檢測 testKey(); // string檢測 testString(); // list檢測 testList(); // set檢測 testSet(); // sortedSet檢測 testSortedSet(); // hash檢測 testHash(); shardedJedisPool.returnResource(shardedJedis); } private void testKey() { System.out.println("=============key=========================="); // 清空數據 System.out.println(jedis.flushDB()); System.out.println(jedis.echo("foo")); // 判斷key否存在 System.out.println(shardedJedis.exists("foo")); shardedJedis.set("key", "values"); System.out.println(shardedJedis.exists("key")); } private void testString() { System.out.println("=============String=========================="); // 清空數據 System.out.println(jedis.flushDB()); // 存儲數據 shardedJedis.set("foo", "bar"); System.out.println(shardedJedis.get("foo")); // 若key不存在,則存儲 shardedJedis.setnx("foo", "foo not exits"); System.out.println(shardedJedis.get("foo")); // 覆蓋數據 shardedJedis.set("foo", "foo update"); System.out.println(shardedJedis.get("foo")); // 追加數據 shardedJedis.append("foo", " hello, world"); System.out.println(shardedJedis.get("foo")); // 設置key的有效期,並存儲數據 shardedJedis.setex("foo", 2, "foo not exits"); System.out.println(shardedJedis.get("foo")); try { Thread.sleep(3000); } catch (InterruptedException e) { } System.out.println(shardedJedis.get("foo")); // 獲取並更改數據 shardedJedis.set("foo", "foo update"); System.out.println(shardedJedis.getSet("foo", "foo modify")); // 截取value的值 System.out.println(shardedJedis.getrange("foo", 1, 3)); //也可寫做shardedJedis..substr() System.out.println(jedis.mset("mset1", "mvalue1", "mset2", "mvalue2", "mset3", "mvalue3", "mset4", "mvalue4")); System.out.println(jedis.mget("mset1", "mset2", "mset3", "mset4")); System.out.println(jedis.del(new String[] { "foo", "foo1", "foo3" })); } private void testList() { System.out.println("=============list=========================="); // 清空數據 System.out.println(jedis.flushDB()); // 添加數據 shardedJedis.lpush("lists", "vector"); shardedJedis.lpush("lists", "ArrayList"); shardedJedis.lpush("lists", "LinkedList"); // 數組長度 System.out.println(shardedJedis.llen("lists")); // 排序 System.out.println(shardedJedis.sort("lists")); // 字串 System.out.println(shardedJedis.lrange("lists", 0, 3)); // 修改列表中單個值 shardedJedis.lset("lists", 0, "hello list!"); // 獲取列表指定下標的值 System.out.println(shardedJedis.lindex("lists", 1)); // 刪除列表指定下標的值 System.out.println(shardedJedis.lrem("lists", 1, "vector")); // 刪除區間之外的數據 System.out.println(shardedJedis.ltrim("lists", 0, 1)); // 列表出棧 System.out.println(shardedJedis.lpop("lists")); // 整個列表值 System.out.println(shardedJedis.lrange("lists", 0, -1)); } private void testSet() { System.out.println("=============set=========================="); // 清空數據 System.out.println(jedis.flushDB()); // 添加數據 shardedJedis.sadd("sets", "HashSet"); shardedJedis.sadd("sets", "SortedSet"); shardedJedis.sadd("sets", "TreeSet"); // 判斷value是否在列表中 System.out.println(shardedJedis.sismember("sets", "TreeSet"));; // 整個列表值 System.out.println(shardedJedis.smembers("sets")); // 刪除指定元素 System.out.println(shardedJedis.srem("sets", "SortedSet")); // 出棧 System.out.println(shardedJedis.spop("sets")); System.out.println(shardedJedis.smembers("sets")); // 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")); // 並集 System.out.println(jedis.sunion("sets1", "sets2")); // 差集 System.out.println(jedis.sdiff("sets1", "sets2")); } private void testSortedSet() { System.out.println("=============zset=========================="); // 清空數據 System.out.println(jedis.flushDB()); // 添加數據 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")); // 元素下標 System.out.println(shardedJedis.zscore("zset", "zset")); // 集合子集 System.out.println(shardedJedis.zrange("zset", 0, -1)); // 刪除元素 System.out.println(shardedJedis.zrem("zset", "zset!")); System.out.println(shardedJedis.zcount("zset", 9.5, 10.5)); // 整個集合值 System.out.println(shardedJedis.zrange("zset", 0, -1)); } private void testHash() { System.out.println("=============hash=========================="); // 清空數據 System.out.println(jedis.flushDB()); //添加數據 shardedJedis.hset("hashs", "entryKey", "entryValue"); shardedJedis.hset("hashs", "entryKey1", "entryValue1"); shardedJedis.hset("hashs", "entryKey2", "entryValue2"); // 判斷某個值是否存在 System.out.println(shardedJedis.hexists("hashs", "entryKey")); // 獲取指定的值 System.out.println(shardedJedis.hget("hashs", "entryKey")); // 批量獲取指定的值 System.out.println(shardedJedis.hmget("hashs", "entryKey", "entryKey1")); // 刪除指定的值 System.out.println(shardedJedis.hdel("hashs", "entryKey")); // 爲key中的域 field 的值加上增量 increment System.out.println(shardedJedis.hincrBy("hashs", "entryKey", 123l)); // 獲取全部的keys System.out.println(shardedJedis.hkeys("hashs")); // 獲取全部的values System.out.println(shardedJedis.hvals("hashs")); } /** * @param args */ public static void main(String[] args) { new Test().show(); } }