redis拾遺(二)——jedis連接redis

前言

上一篇博客中介紹了redis的基本數據,這篇博客就簡單介紹通過客戶端操作redis。

簡單連接實例

1、新建一個簡單的項目,在pom.xml中引入如下依賴

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>

        <!--jdk&lombok simple log start-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!--jdk&lombok simple log end-->
    </dependencies>

2、編寫一個簡單的測試類

@Slf4j
public class SimpleTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.72.128", 6379);
        jedis.set("liman", "2673");
        log.info("jedis simple get result:{}",jedis.get("liman"));
        jedis.close();
    }
}

3、運行結果

在這裏插入圖片描述

對一些數據類型的操作

這裏以zset和hash爲例進行說明

zset

/**
 * autor:liman
 * createtime:2020/7/4
 * comment:ZSet的實例
 */
@Slf4j
public class ZSetTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.72.128", 6379);
        //zadd 往zset中增加元素
        jedis.zadd("myzset", 20, "java");
        jedis.zadd("myzset", 30, "python");
        jedis.zadd("myzset", 90, "ruby");
        jedis.zadd("myzset", 40, "erlang");
        jedis.zadd("myzset", 70, "cpp");
        jedis.zadd("myzset", 50, "android");

        //按照分數倒序排列
        Set<String> set = jedis.zrevrangeByScore("myzset", 100, 10);
        log.info("redis zset content:{}",set);
    }
}

運行結果

在這裏插入圖片描述

hash

/**
 * autor:liman
 * createtime:2020/7/4
 * comment:
 */
@Slf4j
public class HashTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.72.128", 6379);
        Map<String,String> hmap = new HashMap<>();
        hmap.put("h1","a");
        hmap.put("h2","b");
        hmap.put("h3","c");
        hmap.put("h4","d");
        hmap.put("h5","e");
        //存放map元素
        jedis.hmset("hmap",hmap);
        //獲取map元素
        List<String> mapValue = jedis.hmget("hmap", "h1", "h2", "h3", "h4", "h5");
        log.info("hmapValue:{}",mapValue);
    }
}

運行結果

在這裏插入圖片描述

總結

本篇博客簡單實用jedis連接redis進行了相關操作,比較簡單,後面的redis博客總結會在這個基礎上進行一些案例的操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章