zoukankan      html  css  js  c++  java
  • 创建jedis对象

      1.先在taotao-parent的pom.xml中复制 以下内容到rest的pom.xml中

    <!-- Redis客户端 -->
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${jedis.version}</version>
    </dependency>

    2.创建jedis实例测试jedis

    @Test
    public void testJedis(){

    //创建jedis对象

    Jedis jedis = new Jedis("192.168.1.125",6379);

    //调用jedis对象的方法,方法名和redis的命令一致

    jedis.set("key1", "jedis test");
    String string = jedis.get("key1");
    System.out.println(string);
    //关闭jedis

    jedis.close();



    }

    //每次取资源都需要创建一个jedis对象,我们这里使用jedispool连接池
    @Test
    public void testJedisPool(){
    //创建jedisPool连接池
    JedisPool jedisPool = new JedisPool("192.168.1.125", 6379);
    //从连接池中获取Jedis对象
    Jedis jedis = jedisPool.getResource();

    String string = jedis.get("key1");


    System.out.println(string);

    //每次用完必须关闭jedis,不然资源很快耗尽

    jedis.close();
    jedisPool.close();


    }

    // jedisCluster 集群版测试

    @Test
    public void testJedisCluster() {

    HashSet<HostAndPort> nodes = new HashSet<>();
    // 设置集群
    nodes.add(new HostAndPort("192.168.1.125", 7001));
    nodes.add(new HostAndPort("192.168.1.125", 7002));
    nodes.add(new HostAndPort("192.168.1.125", 7003));
    nodes.add(new HostAndPort("192.168.1.125", 7004));
    nodes.add(new HostAndPort("192.168.1.125", 7005));
    nodes.add(new HostAndPort("192.168.1.125", 7006));

    JedisCluster cluster = new JedisCluster(nodes);

    cluster.set("key1", "key1");

    String string = cluster.get("key1");

    System.out.println(string);

    cluster.close();

    }

  • 相关阅读:
    short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
    SpringMVC常用的注解有哪些?
    Spring支持的ORM?
    什么是代理?
    一对一、一对多的关联查询 ?
    iHTML 的 form 提交之前如何验证数值文本框的内容全部为数字?
    解释JDBC抽象和DAO模块?
    Bean 工厂和 Application contexts 有什么区别?
    GitHub的注册
    HTML的学习
  • 原文地址:https://www.cnblogs.com/xiaohouzai/p/6919198.html
Copyright © 2011-2022 走看看