初識Jedis

文章主要分兩部分,第一部分:簡單分析一些類,第二部分:代碼示例java

1、相關類分析redis

一、JedisPoolConfig類分析spa

JedisPoolConfig類是JedisPool的配置類code

//最大空閒鏈接數, 默認8個
config.setMaxIdle(8);

//最大鏈接數, 默認8個
config.setMaxTotal(8);

//獲取鏈接時的最大等待毫秒數(若是設置爲阻塞時BlockWhenExhausted),若是超時就拋異常, 小於零:阻塞不肯定的時間, 默認-1
config.setMaxWaitMillis(-1);

//在獲取鏈接的時候檢查有效性, 默認false
config.setTestOnBorrow(false);

二、JedisPool(ShardedJedisPool)類分析blog

Jedis鏈接池管理類(ShardedJedisPool在使用切片時使用)ip

//獲取Jedis
Jedis getResource()

初始化非切片鏈接池示例get

JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(true);
jedisPool = new JedisPool(config,"127.0.0.1",6379);string

初始化切片鏈接池示例it

JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(true);

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));

// 構造池
shardedJedisPool = new ShardedJedisPool(config, shards);io

三、Jedis類

Jedis類是操做redis的核心類

 1 //設置鍵值對
 2 String set(final String key, String value)
 3 //取鍵值
 4 String get(final String key)
 5 //判斷鍵是否存在
 6 exists(final String... keys)
 7 exists(final String key)
 8 //刪除鍵值對
 9 Long del(final String... keys)
10 Long del(String key)
11 //返回數據類型 "none","string", "list", "set"
12 String type(final String key)
13 //模糊匹配鍵(使用?和*匹配)
14 //h?llo will match hello hallo hhllo
15 //h*llo will match hllo heeeello
16 Set<String> keys(final String pattern)
17 //隨機返回一個key
18 String 
19 //設置過時
20 Long expire(final String key, final int seconds)

實例化示例:(經過JedisPool獲取)

jedis = jedisPool.getResource(); 

四、JedisShardInfo類

JedisShardInfo類是Jedis切片信息類

實例化示例:

 JedisShardInfo shard1 = new JedisShardInfo("127.0.0.1", 6379, "master");

 2、使用Jedis操做String類型數據

package com.my.redis.redisTest.test;

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 Test1 {
    
    private static Jedis jedis;//非切片客戶端鏈接
    private  JedisPool jedisPool;//非切片鏈接池
    private  ShardedJedis shardedJedis;//切片客戶端鏈接
    private  ShardedJedisPool shardedJedisPool;//切片鏈接池

    public Test1(){
        initialPool(); 
//        initialShardedPool(); 
//        shardedJedis = shardedJedisPool.getResource(); 
        jedis = jedisPool.getResource(); 
        
    }
    
    public static void main(String[] args) {
        Test1 test = new Test1();
        
        jedis.set("name", "limouren");
        System.out.print(jedis.get("name"));
        jedis.del("name");
        System.out.print(jedis.get("name"));
    }
    
    /**
     * @Description: 初始化非切片鏈接池  
     * @author lige
     */
    public void initialPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(5); 
        config.setMaxWaitMillis(1000l); 
        config.setTestOnBorrow(true);
        jedisPool = new JedisPool(config,"127.0.0.1",6379,0,"Abc12345");
    }
    
    /**
     * @Description: 初始化切片鏈接池 
     * @author lige
     */
    public void initialShardedPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(5); 
        config.setMaxWaitMillis(1000l); 
        config.setTestOnBorrow(true);
        
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); 
        shards.add(new JedisShardInfo("127.0.0.1", 6379, "master")); 
        
        // 構造池 
        shardedJedisPool = new ShardedJedisPool(config, shards); 
    }
}
相關文章
相關標籤/搜索