0
点赞
收藏
分享

微信扫一扫

如何使用 Jedis 在 Java 中使用 Redis

花姐的职场人生 2022-02-18 阅读 68

关于如何在 Java 中使用 redis 有多个客户端。在本教程中,我们将使用 Jedis,一个简单、快速的 Redis Java 客户端。我们还将使用 Maven 作为我们的构建工具,否则您可以在项目中导入 jedis 的 jar 文件。此外,这假设您已经安装了 redis 服务器,如果没有,请访问此链接以了解如何配置一个。

1.添加Jedis依赖使用Redis

我们需要在 pom.xml 中添加 jedis 依赖才能使用 jedis。在您的pom.xml中,在 Dependencies 部分下,添加

?

1

2

3

4

5

<dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>2.4.2</version>

</dependency>

2.创建示例类

这是一个简单的 Java 类。它使用 Redis的SETHASH命令。您可以在他们的网站上学习 Redis 中的不同命令。

JedisMain.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.exceptions.JedisException;

 

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

 

public class JedisMain {

 

    //address of your redis server

    private static final String redisHost = "localhost";

    private static final Integer redisPort = 6379;

 

    //the jedis connection pool..

    private static JedisPool pool = null;

 

    public JedisMain() {

        //configure our pool connection

        pool = new JedisPool(redisHost, redisPort);

 

    }

 

    public void addSets() {

        //let us first add some data in our redis server using Redis SET.

        String key = "members";

        String member1 = "Sedarius";

        String member2 = "Richard";

        String member3 = "Joe";

 

        //get a jedis connection jedis connection pool

        Jedis jedis = pool.getResource();

        try {

            //save to redis

            jedis.sadd(key, member1, member2, member3);

 

            //after saving the data, lets retrieve them to be sure that it has really added in redis

            Set members = jedis.smembers(key);

            for (String member : members) {

                System.out.println(member);

            }

        } catch (JedisException e) {

            //if something wrong happen, return it back to the pool

            if (null != jedis) {

                pool.returnBrokenResource(jedis);

                jedis = null;

            }

        } finally {

            ///it's important to return the Jedis instance to the pool once you've finished using it

            if (null != jedis)

                pool.returnResource(jedis);

        }

    }

 

    public void addHash() {

        //add some values in Redis HASH

        String key = "javapointers";

        Map<String, String> map = new HashMap<>();

        map.put("name", "Java Pointers");

        map.put("domain", "www.javapointers.com");

        map.put("description", "Learn how to program in Java");

 

        Jedis jedis = pool.getResource();

        try {

            //save to redis

            jedis.hmset(key, map);

 

            //after saving the data, lets retrieve them to be sure that it has really added in redis

            Map<String, String> retrieveMap = jedis.hgetAll(key);

            for (String keyMap : retrieveMap.keySet()) {

                System.out.println(keyMap + " " + retrieveMap.get(keyMap));

            }

 

        } catch (JedisException e) {

            //if something wrong happen, return it back to the pool

            if (null != jedis) {

                pool.returnBrokenResource(jedis);

                jedis = null;

            }

        } finally {

            ///it's important to return the Jedis instance to the pool once you've finished using it

            if (null != jedis)

                pool.returnResource(jedis);

        }

    }

 

    public static void main(String[] args){

        JedisMain main = new JedisMain();

        main.addSets();

        main.addHash();

    }

}

3.运行Main方法

运行类文件,它应该打印以下值:

?

1

2

3

4

5

6

Joe

Sedarius

Richard

domain www.javapointers.com

name Java Pointers

description Learn how to program in Java

在我们的 Redis 服务器中验证这一点,以确保它确实保存了数据

举报

相关推荐

0 条评论