zoukankan      html  css  js  c++  java
  • redis-Jedis

    前言

    Jedis 是 Redis 官方首选的 Java 客户端开发包。

    Jedis只是消息的传递,他只是把数据传递给redis。方法不做赘述,以下记录Jedis的导入和实例化操作。

    使用Jedis

    使用maven添加依赖

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>3.3.0</version>
    </dependency>
    

    Jedis源码下载

    https://github.com/xetorthio/jedis/releases

    Jedis实例

    1. Jedis
    import redis.clients.jedis.Jedis;
    public class JedisClient {
        public static final String _HOST = "*.*.*.*";
        public static final Integer _PORT = 6379;
    
        public static void main(String[] args) {
            Jedis jedis = new Jedis(_HOST, _PORT);
    
            jedis.set("name", "zs");
            String name = jedis.get("name");
            System.out.println(name);
        }
    }
    
    1. JedisPool
    import redis.clients.jedis.Jedis;
    public class JedisPoolClient {
        public static final String _HOST = "*.*.*.*";
        public static final Integer _PORT = 6379;
    
        public static void main(String[] args) {
            // 连接池配置
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(10);
    
            // 连接池初始化
            JedisPool jedisPool = new JedisPool(jedisPoolConfig, _HOST, _PORT);
            // 获取jedis实例
            Jedis resource = jedisPool.getResource();
            // 使用redis连接
            resource.set("name", "RzZ");
            String name = resource.get("name");
            System.out.println(name);
            // 释放redis连接
            resource.close();
            
            // 不释放redis连接
            for (int i = 0; i < 20; i++) {
                System.out.println(i + ":" +jedisPool.getResource());
            }
        }
    }
    

    输出

    RzZ
    0:redis.clients.jedis.Jedis@4f2410ac
    1:redis.clients.jedis.Jedis@73c6c3b2
    2:redis.clients.jedis.Jedis@48533e64
    3:redis.clients.jedis.Jedis@64a294a6
    4:redis.clients.jedis.Jedis@7e0b37bc
    5:redis.clients.jedis.Jedis@3b95a09c
    6:redis.clients.jedis.Jedis@6ae40994
    7:redis.clients.jedis.Jedis@1a93a7ca
    8:redis.clients.jedis.Jedis@3d82c5f3
    9:redis.clients.jedis.Jedis@2b05039f     <==== 连接耗尽,等待释放连接,使用完一定要释放连接
    
    1. Sharde
    public class ShardedJedisClient {
        public static final String _HOST = "122.51.243.39";
        public static final Integer _PORT = 6379;
    
        public static void main(String[] args) {
            List<JedisShardInfo> shardInfos = Arrays.asList(new JedisShardInfo(_HOST, 7001)
                    , new JedisShardInfo(_HOST,7002)
                    , new JedisShardInfo(_HOST,7003));
            ShardedJedis shardedJedis = new ShardedJedis(shardInfos);
            shardedJedis.set("name", "zs");
            System.out.println(shardedJedis.get("name"));
    
            // 连接池配置
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(10);
            // 分片连接池
            ShardedJedisPool shardedJedisPool = new ShardedJedisPool(jedisPoolConfig,shardInfos);
            try(ShardedJedis resource = shardedJedisPool.getResource()){
                System.out.println(resource.get("name"));
            }
    
            // java1.7以上使用try-with-source,会总动释放连接
            for (int i = 0; i < 20; i++) {
                try(ShardedJedis resource = shardedJedisPool.getResource()){
                    System.out.println(i + ":" + resource);
                }
            }
        }
    }
    

    输出

    zs
    zs
    0:redis.clients.jedis.ShardedJedis@4f2410ac
    1:redis.clients.jedis.ShardedJedis@4f2410ac
    2:redis.clients.jedis.ShardedJedis@4f2410ac
    3:redis.clients.jedis.ShardedJedis@4f2410ac
    4:redis.clients.jedis.ShardedJedis@4f2410ac
    5:redis.clients.jedis.ShardedJedis@4f2410ac
    6:redis.clients.jedis.ShardedJedis@4f2410ac
    7:redis.clients.jedis.ShardedJedis@4f2410ac
    8:redis.clients.jedis.ShardedJedis@4f2410ac
    9:redis.clients.jedis.ShardedJedis@4f2410ac
    10:redis.clients.jedis.ShardedJedis@4f2410ac
    11:redis.clients.jedis.ShardedJedis@4f2410ac
    12:redis.clients.jedis.ShardedJedis@4f2410ac
    13:redis.clients.jedis.ShardedJedis@4f2410ac
    14:redis.clients.jedis.ShardedJedis@4f2410ac
    15:redis.clients.jedis.ShardedJedis@4f2410ac
    16:redis.clients.jedis.ShardedJedis@4f2410ac
    17:redis.clients.jedis.ShardedJedis@4f2410ac
    18:redis.clients.jedis.ShardedJedis@4f2410ac
    19:redis.clients.jedis.ShardedJedis@4f2410ac
    
    Process finished with exit code 0
    

    Jedis使用

    Jedis只是消息的传递,他只是把数据传递给redis。

    Jedis使用与redis-cli使用相似,只需使用Jedis实例调用redis方法即可。

    前文有redis环境搭建及相关操作的指令笔记,Jedis只是一个工具,了解redis的原理即可快速上手Jedis的基本使用。有关redis的工具类封装下文列出的参考资料中有引用,站在前人的肩膀上封装更适合自己的工具类即可。

    参考

  • 相关阅读:
    程序员通过什么渠道接外包项目
    中小型软件项目开发一般流程建议
    DevExpress GridControl功能总结
    页面UI注意事项,你在乎吗?
    加密,解密
    本地化(国际化)
    AutoLayout(自动布局)
    UItableView与UICollectionView
    分享iOS开发常用(三方类库,工具,高仿APP,实用网站,技术干货)
    NSPredicate
  • 原文地址:https://www.cnblogs.com/bcomll/p/13524626.html
Copyright © 2011-2022 走看看