IDEA下测试Redis

一、首先需要在pom.xml中加入Java Redis的jar包

  • <!--Java Redis的jar包-->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

     

二、进行简单的测试测试 (低性能,每创建一次都要关闭一次,浪费资源

  • package cn.e3mall.jedis;
    
    import org.junit.Test;
    import redis.clients.jedis.Jedis;
    
    /**
     * Jedis的测试方法
     */
    public class JedisText {
    
        @Test
        public void textJedis() throws Exception{
            //创建一个连接Jedis对象,参数:host、port
            Jedis jedis=new Jedis("127.0.0.1",6379);
            //直接使用Jedis操作Redis,所有Jedis命令都对应一个方法
            jedis.set("test123","my first jedis test");
            String string = jedis.get("test123");
            System.out.println(string);
            //关闭连接
            jedis.close();
        }
    }
    

  • 而且也能在Redis Desktop Manager查到

三、连接Jedis连接池

  •  @Test
        public void testJedisPool() throws Exception {
            //创建一个连接池对象,两个参数host、port
            JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
            //从连接池获得一个连接,就是一个jedis对象。
            Jedis jedis = jedisPool.getResource();
            //使用jedis操作redis
            String string = jedis.get("test123");
            System.out.println(string);
            //关闭连接,每次使用完毕后关闭连接。连接池回收资源。
            jedis.close();
            //关闭连接池。
            jedisPool.close();
        }

四、连接Jedis集群

  •     @Test
        public void testJedisCluster() throws Exception {
            //创建一个连接集群的JedisCluster对象。有一个参数nodes是一个set类型。set中包含若干个HostAndPort对象。
            Set<HostAndPort> nodes = new HashSet<>();
            nodes.add(new HostAndPort("192.168.40.129", 7001));
            nodes.add(new HostAndPort("192.168.40.129", 7002));
            nodes.add(new HostAndPort("192.168.40.129", 7003));
            nodes.add(new HostAndPort("192.168.40.129", 7004));
            nodes.add(new HostAndPort("192.168.40.129", 7005));
            nodes.add(new HostAndPort("192.168.40.129", 7006));
            JedisCluster jedisCluster = new JedisCluster(nodes);
            //直接使用JedisCluster对象操作redis。
            jedisCluster.set("test", "123");
            String string = jedisCluster.get("test");
            System.out.println(string);
            //关闭JedisCluster对象
            jedisCluster.close();
        }

     

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值