zoukankan      html  css  js  c++  java
  • Jedis的使用方法

    一、连接单机版

           1、创建一个jedis对象。需要指定服务端的ip及端口。

           2、使用jedis对象操作数据库,每个redis命令对应一个方法。

           3、打印结果

           4、关闭jedis

    @Test
    
    public void testJedis() throws Exception{
       Jedis jedis = new Jedis("192.168.25.153",6379);
       String result = jedis.get("hello");
       System.out.println(result);
       jedis.close();
    }

    二、连接单机版,使用连接池

        1、创建一个JedisPool对象。需要指定服务端的ip及端口

        2、从JedisPool中获取Jedis对象

        3、操作完毕后关闭jedis对象,连接池回收资源

        4、关闭JedisPool对象

    @Test
    public void testJedisPool() throws Exception{
       JedisPool jedisPool = new JedisPool("192.163.25.153",6379);
       Jedis jedis = jedisPool.getResource();
       jedis.set("jedis","test");
       String result = jedis.get("jedis");
       System.out.println(result);
       jedis.close();
       jedisPool.close();
    }

    三、连接集群版

       1、使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis结点的列表

       2、直接使用JedisCluster对象操作redis。在系统中单例存在。

       3、打印结果

       4、系统关闭前,关闭JedisCluster对象。

    @Test
    public void testJedisCluster() throws Exception{
       Set<HostAndPort> nodes = new HashSet();
       nodes.add(new HostAndPort("192.168.25.153",7001);
       nodes.add(new HostAndPort("192.168.25.153",7002);
       nodes.add(new HostAndPort("192.168.25.153",7003);
       nodes.add(new HostAndPort("192.168.25.153",7004);
       nodes.add(new HostAndPort("192.168.25.153",7005);
       nodes.add(new HostAndPort("192.168.25.153",7006);
       JedisCluster jedisCluster = new JedisCluster(nodes);
       jedisCluster.set("hello","100");
       String result = jedisCluster.get("hello");
       System.out.printn(result);
       jedisCluster.close();
    }
  • 相关阅读:
    mvc的cshtml Request取不到值
    entityframework导航属性筛选
    关于引用类型一个有意思的测试
    如何去除decimal后面的零?
    webapi调用post时自动匹配参数
    entityframework删除时提示“DELETE 语句与 REFERENCE 约束”的解决方法。
    FormsAuthenticationTicket学习笔记
    mvc部署
    redis 订阅发布 事务 WATCH 乐观锁
    idea==>gitee
  • 原文地址:https://www.cnblogs.com/xiaoxli/p/9402371.html
Copyright © 2011-2022 走看看