java來操作redis

1.我在maven項(xiàng)目中進(jìn)行測試,下面是我的pom.xml依賴

<dependency>
          <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.5.2</version>
  </dependency>

應(yīng)為我的redis是3.2版本的,這里的依賴可以根據(jù)實(shí)際情況來更改依賴

2.我在測試類中進(jìn)行準(zhǔn)備工作

  Jedis jedis = null;
    @Before
    public void before(){
        jedis = new Jedis("127.0.0.1");
    }

    @After
    public void after(){
        jedis.close();
        jedis = null;
    }

3.進(jìn)行測試工作

1.hash類型

   @Test
    public void hashMap(){
        HashMap<String,String> actor = new HashMap<>();
        actor.put("jack","jack value");
        actor.put("rose","rose value");
        System.out.println(jedis.hmset("actor", actor));

        System.out.println(jedis.hgetAll("actor"));
        System.out.println(jedis.hget("actor", "jack"));
    }

2.list類型

  @Test
    public void testList(){
        System.out.println(jedis.rpush("person", "xiaohong"));
        System.out.println(jedis.rpush("person", "xiaowen"));
        System.out.println(jedis.rpush("person", "xiaoming"));
        List<String> person = jedis.lrange("person", 0, 100);
        for (String s : person) {
            System.out.println(s);
        }
    }

3.set類型

  @Test
    public void testSet(){
        System.out.println(jedis.sadd("food", "jipai", "doufu", "xiaochi", "hongshaorou"));
        Set<String> food = jedis.smembers("food");
        for (String s : food) {
            System.out.println(s);
        }
    }

4.zset類型

@Test
    public void testZset(){
        System.out.println(jedis.zadd("fenshu", 100, "java"));
        System.out.println(jedis.zadd("fenshu", 99, "html5"));
        System.out.println(jedis.zadd("fenshu", 60, "testing"));
        System.out.println(jedis.zadd("fenshu", 7, "python"));
        Set<String> fenshu = jedis.zrangeByScore("fenshu", 60, 100);
        for (String s : fenshu) {
            System.out.println(s);
        }
    }

5.利用連接池來連接redis

   @Test
    public void testPool(){
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        //設(shè)置最大的空閑數(shù)
        config.setMaxIdle(20);
        //設(shè)置最大連接總數(shù)
        config.setMaxTotal(50);
        //設(shè)置等待時(shí)間
        config.setMaxWaitMillis(5);
        List<JedisShardInfo> share = new ArrayList<>();
        share.add(new JedisShardInfo("127.0.0.1",6379));
        ShardedJedisPool pool = new ShardedJedisPool(config,share);
        ShardedJedis j = pool.getResource();
        List<String> person = j.lrange("person", 0, 100);
        for (String s : person) {
            System.out.println(s);

        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容