zoukankan      html  css  js  c++  java
  • Redis 入门到分布式 (三) Redis客户端的使用

    个人博客网:https://wushaopei.github.io/    (你想要这里多有)

    一、Java客服端:jedis

    • 获取Jedis
    • Jedis基本使用
    • Jedis连接池使用

    1、Jedis是什么?

    Jedis是官方提供的一个客户端,用于对redis进行操作。遵循redis提供的协议,各种语言都有对应的客户端。

    2、Jedis的maven依赖:

    	        <dependency>
    			<groupId>redis.clients</groupId>
    			<artifactId>jedis</artifactId>
    			<version>2.9.0</version>
                            <type>jar</type>
                            <scope>compile</scope>
    		</dependency>

    3、Jedis直连:

        #1.生成一个Jedis对象,这个对象负责和指定Redis节点进行通信
    
        Jedis jedis = new Jedis("127.0.0.1",6379);
    
        #2.jedis执行set操作
        
        jedis.set("hello","world");
    
        #3.jedis执行get操作,value="world"
    
        String value = jedis.get("hello");

    4、Jedis的构造函数分析:

    Jedis (String host, int port ,int connectionTimeout, int soTimeout)
    • host : Redis 节点的所在机器的IP
    • port : Redis 节点的端口
    • connectionTimeout : 客户端连接超时
    • soTimeout : 客户端读写超时

    5、简单使用:

        //1.string
        //输出结果: OK
        jedis.set("hello","world");
        //输出结果: world
        jedis.get("hello"):
        //输出结果: 1
        jedis.incr("counter");
        //2.hash
        jedis.hset("myhash","f1","v1");
        jedis.hset("myhash","f2","v2");
        //输出结果:{f1=v1,f2=v2}
        jedis.hgetAll("myhash");
        // 3.list
        jedis.rpush("mylist","1");
        jedis.rpush("mylist","2");
        jedis.rpush("mylist","3");
        //输出结果: [1,2,3]
        jedis.lrange("mylist",0,-1);
        // 4. set
        jedis.sadd("myset","a");
        jedis.sadd("myset","a");
        jedis.sadd("myset","a");
        //输出结果:[b,a]
        jedis.smembers("myset");
        // 5. zset
        jedis.zadd("myzset",99,"tom");
        jedis.zadd("myzset",66,"peter");
        jedis.zadd("myzset",33,"james");
        //输出结果:[[["james"],33.0],[["peter"],66.0],[["tom"],99.0]]
        jedis.zrangeWithScores("myzset",0,-1);

    6、Jedis连接池使用:

    • Jedis直连
    • Jedis连接池

    方案对比

    • JedisPool使用

    1)Jedis直连:

    连接池:

    方案对比:

    Jedis连接池的使用:

    初始化Jedis连接池,通常来讲JedisPool是单例的。

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig ();
    
    JedisPool jedisPool = new JedisPool(poolConfig,"127.0.0.1",6379);
    
    Jedis jedis = null;
     
    try {
    
        //1.从连接池获取jedis对象
        jedis = jedisPool.getResource();
    
        //2.执行操作
        jedis.set("hello","world");
    
    }catch(Exception e ){
    
        e.printStackTrace();
    
    }finallly{
    
        if(jedis != null)
    
        //如果使用JedisPool,close操作不是关闭链接,代表归还连接池    
        jedis.close();
    
    }
  • 相关阅读:
    Java随笔
    Java随笔
    Java随笔
    CF1271D Portals(反悔贪心)
    CF938D Buy a Ticket(最短路)
    CF1117C Magic Ship(二分)
    HDU6820 Tree(树形dp)
    P2393 美味(主席树+贪心)
    HDU6831 Fragrant numbers(区间dp)
    HDU6832 A Very Easy Graph Problem(生成树)
  • 原文地址:https://www.cnblogs.com/wushaopei/p/11979105.html
Copyright © 2011-2022 走看看