zoukankan      html  css  js  c++  java
  • Redis记录-JAVA连接Redis

    在Java程序中使用Redis之前,需要确保在机器上安装了Redis的Java驱动程序和Java环境。可以先在将Java电脑上并配置好环境。

    安装

    现在,让我们看看如何设置Redis Java驱动程序。

    Java连接到Redis服务器

    请参考以下一个简单的示例代码 -

    import redis.clients.jedis.Jedis; 
    
    public class RedisJava { 
       public static void main(String[] args) { 
          //Connecting to Redis server on localhost 
          Jedis jedis = new Jedis("localhost"); 
          System.out.println("Connection to server sucessfully"); 
          //check whether server is running or not 
          System.out.println("Server is running: "+jedis.ping()); 
       } 
    }
    
    Java

    现在,编译并运行上面的程序来测试与Redis服务器的连接。可以根据需要更改路径。假设jedis.jar的当前版本在当前路径中可以使用。
    执行上面代码,将生成以下结果 -

    $javac RedisJava.java 
    $java RedisJava 
    Connection to server sucessfully 
    Server is running: PONG
    
    Java

    Redis Java字符串示例

    import redis.clients.jedis.Jedis; 
    
    public class RedisStringJava { 
       public static void main(String[] args) { 
          //Connecting to Redis server on localhost 
          Jedis jedis = new Jedis("localhost"); 
          System.out.println("Connection to server sucessfully"); 
          //set the data in redis string 
          jedis.set("tutorial-name", "Redis tutorial"); 
          // Get the stored data and print it 
          System.out.println("Stored string in redis:: "+ jedis.get("tutorialname")); 
       } 
    }
    
    Java

    执行上面代码,将生成以下结果 -

    $javac RedisStringJava.java 
    $java RedisStringJava 
    Connection to server sucessfully 
    Stored string in redis:: Redis tutorial
    
    Java

    Redis Java列表示例

    import redis.clients.jedis.Jedis; 
    
    public class RedisListJava { 
       public static void main(String[] args) { 
          //Connecting to Redis server on localhost 
          Jedis jedis = new Jedis("localhost"); 
          System.out.println("Connection to server sucessfully"); 
    
          //store data in redis list 
          jedis.lpush("tutorial-list", "Redis"); 
          jedis.lpush("tutorial-list", "Mongodb"); 
          jedis.lpush("tutorial-list", "Mysql"); 
          // Get the stored data and print it 
          List<String> list = jedis.lrange("tutorial-list", 0 ,5); 
    
          for(int i = 0; i<list.size(); i++) { 
             System.out.println("Stored string in redis:: "+list.get(i)); 
          } 
       } 
    }
    
    Java

    执行上面代码,将生成以下结果 

    $javac RedisListJava.java 
    $java RedisListJava 
    Connection to server sucessfully 
    Stored string in redis:: Redis 
    Stored string in redis:: Mongodb 
    Stored string in redis:: Mysql
    
    Java

    Redis Java键示例

    import redis.clients.jedis.Jedis; 
    
    public class RedisKeyJava { 
       public static void main(String[] args) { 
          //Connecting to Redis server on localhost 
          Jedis jedis = new Jedis("localhost"); 
          System.out.println("Connection to server sucessfully"); 
          //store data in redis list 
          // Get the stored data and print it 
          List<String> list = jedis.keys("*"); 
    
          for(int i = 0; i<list.size(); i++) { 
             System.out.println("List of stored keys:: "+list.get(i)); 
          } 
       } 
    }
    
    Java

    执行上面代码,将生成以下结果 -

    $javac RedisKeyJava.java 
    $java RedisKeyJava 
    Connection to server sucessfully 
    List of stored keys:: tutorial-name 
    List of stored keys:: tutorial-list
  • 相关阅读:
    C# 函数式编程
    【python】 入门 搭建环境
    luogu P3978 [TJOI2015]概率论
    luogu P4778 Counting swaps
    luogu P2480 [SDOI2010]古代猪文
    luogu P3243 [HNOI2015]菜肴制作
    luogu P4744 [Wind Festival]Iron Man
    luogu P4448 [AHOI2018初中组]球球的排列
    luogu P1593 因子和
    luogu P1943 LocalMaxima_NOI导刊2009提高(1)
  • 原文地址:https://www.cnblogs.com/xinfang520/p/7723180.html
Copyright © 2011-2022 走看看