1、首先下載jar包放到你的工程中
2、練習(xí)
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
|
package com.jianyuan.redisTest; import java.util.Iterator; import java.util.List; import java.util.Set; import redis.clients.jedis.Jedis; public class RedisTest { public static void main(String[] args) { //連接本地的Redis服務(wù) Jedis jedis = new Jedis( "127.0.0.1" , 6379 ); //權(quán)限認(rèn)證 jedis.auth( "wenhongyu66" ); jedis.select( 0 ); System.out.println( "連接成功" ); //查看服務(wù)是否運(yùn)行 System.out.println(jedis.ping()); //設(shè)置 redis 字符串?dāng)?shù)據(jù) jedis.set( "runoobkey" , "www.runoob.com" ); // 獲取存儲(chǔ)的數(shù)據(jù)并輸出 System.out.println( "redis 存儲(chǔ)的字符串為: " + jedis.get( "runoobkey" )); //存儲(chǔ)數(shù)據(jù)到列表中 jedis.lpush( "site-list" , "Runoob" ); jedis.lpush( "site-list" , "Google" ); jedis.lpush( "site-list" , "Taobao" ); System.out.println(jedis.llen( "site-list" )); // 獲取存儲(chǔ)的數(shù)據(jù)并輸出 List<String> list = jedis.lrange( "site-list" , 0 ,jedis.llen( "site-list" )); for ( int i= 0 ; i<list.size(); i++) { System.out.println( "列表項(xiàng)為: " +list.get(i)); } // 獲取數(shù)據(jù)并輸出 Set<String> keys = jedis.keys( "*" ); Iterator<String> it=keys.iterator() ; while (it.hasNext()){ String key = it.next(); System.out.println(key); } } } |
總結(jié):自己可以封裝一些工具類方便使用,包括連接池的配置,jedis參數(shù)的配置等。
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
|
private static JedisPool jedisPool = null ; private static Jedis jedis; static { jedis = getJedisPool().getResource(); } /** * 構(gòu)建redis連接池 */ public static JedisPool getJedisPool() { if (jedisPool == null ) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal( 1024 ); // 可用連接實(shí)例的最大數(shù)目,如果賦值為-1,表示不限制. config.setMaxIdle( 5 ); // 控制一個(gè)Pool最多有多少個(gè)狀態(tài)為idle(空閑的)jedis實(shí)例,默認(rèn)值也是8 config.setMaxWaitMillis( 1000 * 100 ); // 等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)/如果超過等待時(shí)間,則直接拋出異常 config.setTestOnBorrow( true ); // 在borrow一個(gè)jedis實(shí)例時(shí),是否提前進(jìn)行validate操作,如果為true,則得到的jedis實(shí)例均是可用的 jedisPool = new JedisPool(config, "127.0.0.1" , 6379 ); } return jedisPool; } /** * 釋放jedis資源 */ public static void returnResource(Jedis jedis) { if (jedis != null ) { jedis.close(); } } public static String get(String key) { String value = null ; Jedis jedis = null ; try { JedisPool pool = getJedisPool(); jedis = pool.getResource(); value = jedis.get(key); } catch (Exception e) { returnResource(jedis); e.printStackTrace(); } finally { returnResource(jedis); } return value; } |
RedisTemplate封裝了從JedisPool中取jedis以及返回池中
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
|
public class RedisTemplate { private JedisPool jedisPool; public RedisTemplate(JedisPool jedisPool) { this .jedisPool = jedisPool; } public <T> T execute(RedisCallback<T> callback) { Jedis jedis = jedisPool.getResource(); try { return callback.handle(jedis); } catch (Exception e) { // throw your exception throw e; } finally { returnResource(jedis); } } private void returnResource(Jedis jedis) { if (jedis != null ) { jedis.close(); } } } public interface RedisCallback<T> { public T handle(Jedis jedis); } |
以上這篇redis在java中的使用(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。