標籤:redis
先啟動 redis-server /etc/redis/redis.conf
package com.test;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Set;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; /** * * @author luozhonghua * */public class RedisClient {private Jedis jedis;// 非切片額用戶端串連private JedisPool jedisPool;// 非切片串連池private ShardedJedis shardedJedis;// 切片額用戶端串連private ShardedJedisPool shardedJedisPool;// 切片串連池public RedisClient() {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); System.out.println("begin...");jedisPool = new JedisPool(config, "127.0.0.1", 6379); System.out.println("end...");}/** * 初始化切片池 */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("127.0.0.1", 6379, "master"));// 構造池shardedJedisPool = new ShardedJedisPool(config, shards);}public void show() {KeyOperate();StringOperate();ListOperate();SetOperate();SortedSetOperate();HashOperate();jedisPool.returnResource(jedis);shardedJedisPool.returnResource(shardedJedis);}private void KeyOperate() { System.out.println("======================key=========================="); // 清空資料 System.out.println("清空庫中所有資料:"+jedis.flushDB()); // 判斷key否存在 System.out.println("判斷key999鍵是否存在:"+shardedJedis.exists("key999")); System.out.println("新增key001,value001索引值對:"+shardedJedis.set("key001", "value001")); System.out.println("判斷key001是否存在:"+shardedJedis.exists("key001")); // 輸出系統中所有的key System.out.println("新增key002,value002索引值對:"+shardedJedis.set("key002", "value002")); System.out.println("系統中所有鍵如下:"); Set<String> keys = jedis.keys("*"); Iterator<String> it=keys.iterator() ; while(it.hasNext()){ String key = it.next(); System.out.println(key); } // 刪除某個key,若key不存在,則忽略該命令。 System.out.println("系統中刪除key002: "+jedis.del("key002")); System.out.println("判斷key002是否存在:"+shardedJedis.exists("key002")); // 設定 key001的到期時間 System.out.println("設定 key001的到期時間為5秒:"+jedis.expire("key001", 5)); try{ Thread.sleep(2000); } catch (InterruptedException e){ } // 查看某個key的剩餘存留時間,單位【秒】.永久生存或者不存在的都返回-1 System.out.println("查看key001的剩餘存留時間:"+jedis.ttl("key001")); // 移除某個key的存留時間 System.out.println("移除key001的存留時間:"+jedis.persist("key001")); System.out.println("查看key001的剩餘存留時間:"+jedis.ttl("key001")); // 查看key所儲存的值的類型 System.out.println("查看key所儲存的值的類型:"+jedis.type("key001")); /* * 一些其他方法:1、修改鍵名:jedis.rename("key6", "key0"); * 2、將當前db的key移動到給定的db當中:jedis.move("foo", 1) */ }private void StringOperate() { System.out.println("======================String_1=========================="); // 清空資料 System.out.println("清空庫中所有資料:"+jedis.flushDB()); System.out.println("=============增============="); jedis.set("key001","value001"); jedis.set("key002","value002"); jedis.set("key003","value003"); System.out.println("已新增的3個索引值對如下:"); System.out.println(jedis.get("key001")); System.out.println(jedis.get("key002")); System.out.println(jedis.get("key003")); System.out.println("=============刪============="); System.out.println("刪除key003索引值對:"+jedis.del("key003")); System.out.println("擷取key003鍵對應的值:"+jedis.get("key003")); System.out.println("=============改============="); //1、直接覆蓋原來的資料 System.out.println("直接覆蓋key001原來的資料:"+jedis.set("key001","value001-update")); System.out.println("擷取key001對應的新值:"+jedis.get("key001")); //2、直接覆蓋原來的資料 System.out.println("在key002原來值後面追加:"+jedis.append("key002","+appendString")); System.out.println("擷取key002對應的新值"+jedis.get("key002")); System.out.println("=============增,刪,查(多個)============="); /** * mset,mget同時新增,修改,查詢多個索引值對 * 等價於: * jedis.set("name","ssss"); * jedis.set("jarorwar","xxxx"); */ System.out.println("一次性新增key201,key202,key203,key204及其對應值:"+jedis.mset("key201","value201", "key202","value202","key203","value203","key204","value204")); System.out.println("一次性擷取key201,key202,key203,key204各自對應的值:"+ jedis.mget("key201","key202","key203","key204")); System.out.println("一次性刪除key201,key202:"+jedis.del(new String[]{"key201", "key202"})); System.out.println("一次性擷取key201,key202,key203,key204各自對應的值:"+ jedis.mget("key201","key202","key203","key204")); System.out.println(); //jedis具備的功能shardedJedis中也可直接使用,下面測試一些前面沒用過的方法 System.out.println("======================String_2=========================="); // 清空資料 System.out.println("清空庫中所有資料:"+jedis.flushDB()); System.out.println("=============新增索引值對時防止覆蓋原先值============="); System.out.println("原先key301不存在時,新增key301:"+shardedJedis.setnx("key301", "value301")); System.out.println("原先key302不存在時,新增key302:"+shardedJedis.setnx("key302", "value302")); System.out.println("當key302存在時,嘗試新增key302:"+shardedJedis.setnx("key302", "value302_new")); System.out.println("擷取key301對應的值:"+shardedJedis.get("key301")); System.out.println("擷取key302對應的值:"+shardedJedis.get("key302")); System.out.println("=============超過有效期間索引值對被刪除============="); // 設定key的有效期間,並儲存資料 System.out.println("新增key303,並指定到期時間為2秒"+shardedJedis.setex("key303", 2, "key303-2second")); System.out.println("擷取key303對應的值:"+shardedJedis.get("key303")); try{ Thread.sleep(3000); } catch (InterruptedException e){ } System.out.println("3秒之後,擷取key303對應的值:"+shardedJedis.get("key303")); System.out.println("=============擷取原值,更新為新值一步完成============="); System.out.println("key302原值:"+shardedJedis.getSet("key302", "value302-after-getset")); System.out.println("key302新值:"+shardedJedis.get("key302")); System.out.println("=============擷取子串============="); System.out.println("擷取key302對應值中的子串:"+shardedJedis.getrange("key302", 5, 7)); } private void ListOperate() {}private void SetOperate() {}private void SortedSetOperate() {}private void HashOperate() {}public static void main(String[]args){ new RedisClient().show(); }}
linux下小試redis demo