Jedis操作

zmy愛吃炸雞發表於2020-10-26
連線池幫助類:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JedisPoolUtils {
    private static JedisPool jedisPool;
    static {
        //1.獲取配置資源
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        //2.載入資源
        Properties pro =new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3.建立連線池配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
        //4.建立連線池
        jedisPool=new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port"))
                ,Integer.parseInt(pro.getProperty("timeout")),pro.getProperty("password"));
    }
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}
測試操作:
import cn.web.util.JedisPoolUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
* redis操作步驟:
* 1.匯入Jedis包。
* 2.連結redis操作。
*
* jedis封裝的操作和redis客戶端操作方式是一樣的
* */
public class JedisTest {
    /*
    * 連線測試
    * */
    @Test
    public void test1(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        jedis.set("username","zhangs");
        jedis.close();
    }
    /*
    * 資料格式1:鍵值
    * */
    @Test
    public void test2() throws InterruptedException {
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        //設定值,取值
        jedis.set("auth","Hik1234");
        String auth = jedis.get("auth");
        System.out.println(auth);
        //設定過期時間
        jedis.setex("auth1",5,"Hik1234");
        System.out.println(jedis.get("auth1"));
        Thread.sleep(5000);
        System.out.println(jedis.get("auth1"));
        jedis.close();
    }
    /*
    * hash:讀寫
    * */
    @Test
    public void test3(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        //寫入hash資料
        jedis.hset("user", "zhangs", "20");
        jedis.hset("user", "zhangs1", "21");
        jedis.hset("user", "zhangs2", "22");
        //讀取單個資料
        String hget = jedis.hget("user", "zhangs");
        System.out.println(hget);
        //讀取全部資料
        Map<String, String> user = jedis.hgetAll("user");
        Set<String> strings = user.keySet();
        for (String key :
                strings) {
            System.out.println(key+":"+user.get(key));
        }
        jedis.close();
    }
    /*
    * list:資料結構
    * */
    @Test
    public void test4(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        //左右壓入資料
        jedis.lpush("list","a");
        jedis.lpush("list","b","c");
        jedis.rpush("list","d","e");
        //取出資料
        List<String> list = jedis.lrange("list", 0, -1);
        System.out.println(list);
        //彈出資料
        jedis.lpop("list");
        jedis.rpop("list");
        System.out.println(jedis.lrange("list",0,-1));
        jedis.close();
    }
    /*
    * set:資料結構 不可重複集合
    * */
    @Test
    public void test5(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        jedis.sadd("set","a","b","c");
        Set<String> set = jedis.smembers("set");
        System.out.println(set);
        jedis.close();
    }
    /*
    * sortedset:自動排序集合 可以重複
    * */
    @Test
    public void test6(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("Hik1234");
        jedis.zadd("sortset",9,"a");
        jedis.zadd("sortset",5,"b");
        jedis.zadd("sortset",8,"c");
        Set<String> sortset = jedis.zrange("sortset", 0, -1);
        System.out.println(sortset);
        jedis.close();
    }
    /*
    連線池連線
    */
    @Test
    public void test7(){
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(10);
        JedisPool jedisPool =new JedisPool(config,"127.0.0.1",6379,1000,"Hik1234");
        Jedis resource = jedisPool.getResource();
        resource.set("jedisPool","abc");
        System.out.println(resource.get("jedisPool"));
        resource.close();
    }
    /*
    * 連線池連線
    * */
    @Test
    public void test8(){
        Jedis jedis = JedisPoolUtils.getJedis();
        jedis.set("hello","world");
        System.out.println(jedis.get("hello"));
        jedis.close();
    }
}
配置檔案:jedis.properties
host=127.0.0.1 
port=6379 
maxTotal=50 
maxIdle=10 
timeout=1000 
password=Hik1234


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69983768/viewspace-2729747/,如需轉載,請註明出處,否則將追究法律責任。

相關文章