• redis java操作


    Redis 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());
     }
    }
    
    

    现在,让我们来编译和运行上面的程序来测试连接Redis服务器。你可以根据实际情况改变路径。这里假设jedis.jar的当前版本在当前路径中可用

    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("tutorial-name"));
     }
    }
    
    

    现在,让我们编译并运行上述程序。

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

    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));
         }
     }
    }

    现在,让我们编译并运行上述程序。

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




    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));
         }
       }
    }
    
    

    现在,让我们编译并运行上述程序。

    $javac RedisKeyJava.java
    $java RedisKeyJava
    Connection to server sucessfully
    List of stored keys:: tutorial-name
    List of stored keys:: tutorial-list
  • 相关阅读:
    项目中 2个或者多个EF模型 表名称相同会导致生成的实体类 覆盖的解决方法
    Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法;关于如何查看 EntityValidationErrors 详细信息的解决方法
    深夜杳杳
    我在学习
    IIS下新建虚拟目录
    Win10资源管理器异常
    Docker笔记三:Docker数据卷和DockerFile
    Docker笔记二:Docker拷贝,提交,进入容器和阿里云加速
    ElasticSearch7.x系列三:Logstash的使用
    ElasticSearch7.x系列二:Kibana的使用和C#的Nest客户端
  • 原文地址:https://www.cnblogs.com/panxuejun/p/5901403.html
Copyright © 2020-2023  润新知