wget http://download.redis.io/releases/redis-3.0.3.tar.gz
tar xzf redis-3.0.3.tar.gz
cd redis-3.0.3
make
#啟動服務的
cd /src
./redis-server
#使用客戶端 可以輸入ping、info查看信息
./redis-cli
yum install epel-release
yum install redis
#啟動停止
service redis start/stop
#客戶端
redis-cli
redis> SET firstname shekhar
OK
redis> GET firstname
"shekhar"
redis> INCR votes
(integer) 1
redis> INCR votes
(integer) 2
redis> INCR votes
(integer) 3
redis> DECR votes
(integer) 2
其他一些操作(包括?APPEND、GETRANGE、MSET?和?STRLENGTH?也可用于字符串。etc設備安裝位置?請參見http://doc.redisfans.com/string/index.html?)
? ? ? ? 有序的字符串列表,按照插入順序排序。Redis 中的列表是一個有序的字符串集合,您可以向其中添加任意數量的(惟一或非惟一)元素。除了向列表添加元素和從中獲取元素的操作之外,Redis 還支持對列表使用取出、推送、范圍和其他一些操作。
使用 LPUSH 命令,最近添加的單詞位于列表頂部,以前添加的單詞會在后面:
redis> LPUSH words austerity
(integer) 1
redis> LPUSH words socialism moratorium socialism socialism
(integer) 5
redis> LRANGE words 0 2
1) "socialism"
2) "socialism"
3) "moratorium"
redis > LLEN words刪除:
(integer) 5
#刪除所有 socialism刪除列表:
redis> LREM words 0 socialism
(integer) 2
redis 127.0.0.1:6379> DEL words
(integer) 1
redis> SADD uniquewords austerity
(integer) 1
redis> SADD uniquewords socialism
(integer) 1
redis> SADD uniquewords socialism
(integer) 0
redis> SMEMBERS uniquewords
1) "austerity"
2) "socialism"
redis> ZADD wordswithlength 9 austerity要獲得有序集的大小,可使用 ZCARD 命令:
(integer) 1
redis> ZADD wordswithlength 7 furtive
(integer) 1
redis> ZADD wordswithlength 5 bigot
(integer) 1
redis> ZRANGE wordswithlength 0 -1
1) "bigot"
2) "furtive"
3) "austerity"
redis 127.0.0.1:6379> ZCARD wordswithlength
(integer) 3
redis> HSET user:1 name shekhar
(integer) 1
redis> HSET user:1 lastname gulati
(integer) 1
redis> HGET user:1
redis> HGET user:1 name
"shekhar"
redis> HGETALL user:1
1) "name"
2) "shekhar"
3) "lastname"
4) "gulati"
? ? ? ? 1. 下載
? ? ? ? ? ??鏈接:http://pan.baidu.com/s/1bnwg0OZ?密碼:ekwu
pom
? <dependency>
? ?<groupId>redis.clients</groupId>
? ?<artifactId>jedis</artifactId>
? ?<version>2.1.0</version>
? </dependency>
?
測試代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import ?java.util.HashMap; import ?java.util.List; import ?java.util.Map; import ?redis.clients.jedis.Jedis; public ?class ?TestRedis?{ ???? private ?Jedis?jedis; ???? public ?TestRedis()?{ ???????? jedis?=? new ?Jedis( "123.59.42.xx" ,? 6379 ); ???????? //?權限認證 ???????? //?jedis.auth("admin"); ???? } ???? /** ????? *?redis存儲字符串 ????? */ ???? public ?void ?testString()?{ ???????? jedis.set( "name" ,? "Jerome蘇" ); ???????? jedis.expire( "name" ,? 60 );? //?設置存活時間 ???????? //?jedis.del("name");?//刪除某個鍵 ???????? System.out.println( "name----->>>" ?+?jedis.get( "name" )); ???? } ???? /** ????? *?redis操作Map ????? */ ???? public ?void ?testMap()?{ ???????? Map<String,?String>?map?=? new ?HashMap<String,?String>(); ???????? map.put( "name" ,? "jerome" ); ???????? map.put( "age" ,? "24" ); ???????? map.put( "email" ,? "jerome_s@qq.com" ); ???????? jedis.hmset( "user" ,?map);? //?存入redis ???????? //?取出 ???????? List<String>?rsmap?=?jedis.hmget( "user" ,? "name" ,? "age" ); ???????? System.out.println(rsmap); ???????? //?刪除map中的某個鍵值 ???????? //?jedis.hdel("user",?"age"); ???????? //?System.out.println(jedis.hlen("user"));?//?返回key為user的鍵中存放的值的個數2 ???????? //?System.out.println(jedis.exists("user"));//?是否存在key為user的記錄?返回true ???????? //?System.out.println(jedis.hkeys("user"));//?返回map對象中的所有key ???????? //?System.out.println(jedis.hvals("user"));//?返回map對象中的所有value ???? } ???? /** ????? *?jedis操作List ????? */ ???? public ?void ?testList()?{ ???????? //?開始前,先移除所有的內容 ???????? jedis.del( "animals" ); ???????? //?-1表示取得所有 ???????? System.out.println(jedis.lrange( "animals" ,? 0 ,?- 1 )); ???????? //?存入List ???????? jedis.lpush( "animals" ,? "cat" ); ???????? jedis.lpush( "animals" ,? "dog" ); ???????? jedis.lpush( "animals" ,? "tiger" ); ???????? System.out.println(jedis.lrange( "animals" ,? 0 ,?- 1 )); ???????? //?刪除單個 ???????? jedis.rpush( "animals" ,? "cat" ); ???????? System.out.println(jedis.lrange( "animals" ,? 0 ,?- 1 )); ???? } ???? /** ????? *?jedis操作Set ????? */ ???? public ?void ?testSet()?{ ???????? //?添加 ???????? jedis.sadd( "user" ,? "jerome" ); ???????? jedis.sadd( "user" ,? "jelly" ); ???????? jedis.sadd( "user" ,? "jack" ); ???????? jedis.sadd( "user" ,? "nemo" ); ???????? jedis.sadd( "user" ,? "who" ); ???????? //?移除noname ???????? jedis.srem( "user" ,? "who" ); ???????? System.out.println(jedis.smembers( "user" )); //?獲取所有加入的value ???????? System.out.println(jedis.sismember( "user" ,? "who" )); //?判斷?who是否是user集合的元素 ???????? System.out.println(jedis.srandmember( "user" )); ???????? System.out.println(jedis.scard( "user" )); //?返回集合的元素個數 ???? } ???? /** ????? *?redis排序 ????? *? ????? *?@throws?InterruptedException ????? */ ???? public ?void ?testSort()? throws ?InterruptedException?{ ???????? //?注意,此處的rpush和lpush是List的操作。路由器安裝教程、是一個雙向鏈表(但從表現來看的) ???????? jedis.del( "a" ); //?先清除數據,再加入數據進行測試 ???????? jedis.rpush( "a" ,? "1" ); ???????? jedis.lpush( "a" ,? "6" ); ???????? jedis.lpush( "a" ,? "3" ); ???????? jedis.lpush( "a" ,? "9" ); ???????? System.out.println(jedis.lrange( "a" ,? 0 ,?- 1 )); //?[9,?3,?6,?1] ???????? System.out.println(jedis.sort( "a" ));? //?[1,?3,?6,?9]?//輸入排序后結果 ???????? System.out.println(jedis.lrange( "a" ,? 0 ,?- 1 )); ???? } ???? public ?static ?void ?main(String[]?args)?{ ???????? TestRedis?redis?=? new ?TestRedis(); ???????? redis.testMap(); ???????? //?RedisUtil.getJedis().set("newname",?"中文測試"); ???????? //?System.out.println(RedisUtil.getJedis().get("newname")); ???? } } |
? ? 使用連接池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import ?redis.clients.jedis.Jedis; import ?redis.clients.jedis.JedisPool; import ?redis.clients.jedis.JedisPoolConfig; public ?final ?class ?RedisUtil?{ ???? //?Redis服務器IP ???? private ?static ?String?ADDR?=? "192.168.0.100" ; ???? //?Redis的端口號 ???? private ?static ?int ?PORT?=? 6379 ; ???? //?訪問密碼 ???? //?private?static?String?AUTH?=?"admin"; ???? //?可用連接實例的最大數目,默認值為8; ???? //?如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。 ???? private ?static ?int ?MAX_ACTIVE?=? 1024 ; ???? //?控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例,默認值也是8。 ???? private ?static ?int ?MAX_IDLE?=? 200 ; ???? //?等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException; ???? private ?static ?int ?MAX_WAIT?=? 10000 ; ???? private ?static ?int ?TIMEOUT?=? 10000 ; ???? //?在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的; ???? private ?static ?boolean ?TEST_ON_BORROW?=? true ; ???? private ?static ?JedisPool?jedisPool?=? null ; ???? /** ????? *?初始化Redis連接池 ????? */ ???? static ?{ ???????? try ?{ ???????????? JedisPoolConfig?config?=? new ?JedisPoolConfig(); ???????????? config.setMaxActive(MAX_ACTIVE); ???????????? config.setMaxIdle(MAX_IDLE); ???????????? config.setMaxWait(MAX_WAIT); ???????????? config.setTestOnBorrow(TEST_ON_BORROW); ???????????? //?jedisPool?=?new?JedisPool(config,?ADDR,?PORT,?TIMEOUT,?AUTH); ???????????? jedisPool?=? new ?JedisPool(config,?ADDR,?PORT,?TIMEOUT); ???????? }? catch ?(Exception?e)?{ ???????????? e.printStackTrace(); ???????? } ???? } ???? /** ????? *?獲取Jedis實例 ????? *? ????? *?@return ????? */ ???? public ?synchronized ?static ?Jedis?getJedis()?{ ???????? try ?{ ???????????? if ?(jedisPool?!=? null )?{ ???????????????? Jedis?resource?=?jedisPool.getResource(); ???????????????? return ?resource; ???????????? }? else ?{ ???????????????? return ?null ; ???????????? } ???????? }? catch ?(Exception?e)?{ ???????????? e.printStackTrace(); ???????????? return ?null ; ???????? } ???? } ???? /** ????? *?釋放jedis資源 ????? *? ????? *?@param?jedis ????? */ ???? public ?static ?void ?returnResource( final ?Jedis?jedis)?{ ???????? if ?(jedis?!=? null )?{ ???????????? jedisPool.returnResource(jedis); ???????? } ???? } } |
code:?鏈接:http://pan.baidu.com/s/1qW5PJic?密碼:pyha
代碼
? ? ? ??https://github.com/JeromeSuz/demo_code_repo/tree/master/src/main/java/redis
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态