Jedis接口

Jedis是Redis数据库在java中的api接口
创建一个maven项目。添加Junit和Jedis依赖

package ocm.qfedu.test;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.*;

public class TestJedis {
    private Jedis j=null;
    
    @Before
    public void bafore(){
        j=new Jedis();
    }
    @After
    public void after(){
        if(j!=null){
            j.close();
        }
        j=null;
    }
    //String型
    @Test
    public void testString(){
       j.set("name","zhangsan");
    }
    //Hash型
    @Test
    public void testHash(){
        Map<String,String> map=new HashMap<>();
        map.put("name","douguodalu");
        map.put("time","2019-10-9");
        j.hmset("lib",map);
        System.out.println(j.hget("lib", "name"));
        System.out.println(j.hgetAll("lib"));
    }
    //Lists型
    @Test
    public void testLists(){
        j.lpush("python","shujufenxu","pachong");
        j.rpush("python","jiagou");
        System.out.println(j.lrange("python", 0, 10));
    }
    //Set型
    @Test
    public void testSet(){
        j.sadd("root","wangwu","lisi","lisi2");
        System.out.println(j.smembers("root"));
    }
    //Zset型
    @Test
    public void testZset(){
        j.zadd("shouji",100,"vivo");
        j.zadd("shouji",99,"oppo");
        j.zadd("shouji",60,"huawu");
        System.out.println(j.zrangeByScore("shouji", 10, 100));
    }
    //Pool连接池
    @Test
    public void testPool(){
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        //设置最大的空闲数
        config.setMaxIdle(20);
        //设置最大连接总数
        config.setMaxTotal(50);
        //设置等待时间
        config.setMaxWaitMillis(3000);
        List<JedisShardInfo> shard=new ArrayList<>();
        shard.add(new JedisShardInfo("localhost","6379"));
        ShardedJedisPool pool = new ShardedJedisPool(config, shard);
        ShardedJedis jedis = pool.getResource();
        System.out.println(jedis.smembers("java"));
    }
}

你可能感兴趣的:(Jedis接口)