Jedis use Redis cache operations (Chapter II)

           table of Contents

  •   Jedis Profile
  •   Jedis use Redis connection
  •   Use JedisPool connection pool Redis
  •   Jedis operations String
  •   Jedis Operation List
  •   Jedis operation set
  •   Jedis operation Hash

 

  About a .Jedis

    Jedis is the integration redis operation commands, the client encapsulates the redis java.

    Jedis use redis database operation, it is equivalent to using JDBC operations Mysql, SqlServer database.

    

 

 

 


 Two, Jedis connected Redis

    Jedis use Redis connection in two ways: Jedis objects directly connected Redis, JedisPool connection pool to manage connections.

    Jedis objects directly connected:

    


 

 Three, Jedis objects directly connected Redis   

    Jedis use objects directly connected way, we must first download the Jedis corresponding jar package,
    as shown in the latest version is now jedis-2.9.0

    The downloaded jar package added to the project in the classpath, you can use Redis Jedis to connect to the database.

     

    Direct Jedis objects can be divided into five steps:
    1. introducing redis.clients.jedis.Jedis class
    2, Jedis new object whose constructor new Jedis (String host, int port ), host database Redis ip address parameter, port is the port number.
    3, call jedis.connect () method to connect the Redis
    . 4, a method of operating a data call jedis built and the results obtained (Jedis API speaking in detail later)
    5, call jedis.disconnect () closes the connection method

 

     Example: Direct connected Redis database, all access to the local redis key, and print the output Jedis

   

. 1  public  class JedisToRedis {
 2  
. 3      public  static  void main (String [] args) {
 . 4          // Create Object jedis 
. 5          Jedis jedis = new new Jedis ( "127.0.0.1", 6379 );
 . 6          // 3. connect the Redis 
. 7          jedis. Connect ();
 . 8          // 4. the method of using the built-in operating Jedis Redis data acquisition result and
 9          // here we use the key Jedis Redis method for obtaining all the key 
10          the Set <String> = jedis.keys Keys ( "*" ) ;
 11          // Key will receive the full print out 
12          for (String Key: Keys
 13              ) {
14             System.out.println(key);
15         }
16         //关闭jedis
17         jedis.close();
18     }
19 
20 }

      effect:     

      In order to show the effect of Example 1, we get all the native Redis key command in the console with redis

      

      It can be seen that there's a native redis key there is "key", "ok", the next run JedisDemo.main (), view the console output is as follows:

      

 

 

 


 

  Four, Jedis connection pool to manage connections Redis

      The way production is generally used for the connection pool Redis connection management, all Jedis objects first on the pool
      when the need to connect each Redis, you only need to borrow in the pond, run out and then returned to the pond.

      

      Jedis provides JedisPool connection pool for this class as Jedis while using the Apache generic object pooling tool commons-pool as a resource management tool to be used JedisPool connection pool,

      要先下载commons-pool jar包,如图最新版本是commons-pool-2.6.0
       附:jedis2.9.0.jar自带了连接池资源的管理工具JedisPoolConfig
       ,后面例子将使用jedis自带的JedisPoolConfig类来配置连接池)

      Jedis连接池管理方式连接Redis可以分为以下几个步骤:
      1、导入包
      2、获得连接池配置对象JedisPoolConfig
      3、设置连接池信息(最大连接数、最大空闲连接数等)
      4、获得连接池
      5、获得核心对象Jedis
      6、用Jedis操作Redis数据并取得结果
      7、关闭Jedis连接,关闭JedisPool连接池,释放资源

       例二,Jedis连接池管理连接Redis数据库,获取本机redis中所有key,并打印输出

      

        //1.获得连接池配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2.设置连接池信息(最大连接数)
        //设置连接池信息(最大空闲连接数)
        config.setMaxTotal(30);
        config.setMaxIdle(10);
        //3.获得连接池
        JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379);
        //4.得到核心对象 
         Redis redis = jedisPool.getResource();  
        /5.使用Jedis内置方法操作Redis数据并获取结果
        //这里我们使用Jedis的key方法获得Redis中的所有key
        Set<String> keys = jedis.keys("*");
        //将获得的key全部打印出来
        for (String key:keys
             ) {
            System.out.println(key);
        }
        //关闭jedis
        jedis.close();

 


 

   Jedis两种连接方式的优劣性对比

    客户端连接Redis使用的是TCP协议,直连的方式每次需要建立TCP连接,而连接池的方式是可以预先初始化好Jedis连接,

    所以每次只需要从Jedis连接池借用即可,而借用和归还操作是在本地进行的,只有少量的并发同步开销,远远小于新建TCP连接的开销。

    另外直连的方式无法限制Jedis对象的个数,在极端情况下可能会造成连接泄露,而连接池的形式可以有效的保护和控制资源的使用。

    但是直连的方式也并不是一无是处,下面给出两种方式各自的优劣势。

    

 

 

 


   Jedis内置基本方法:

//验证redis密码,如果没有设置密码这段可以省略
jedis.auth(String password);
//连接
jedis.connect();
//关闭连接
jedis.disconnect();
//列出符合特定条件的key   其中pattern为匹配条件,填*则为列出所有
Set<String>  keys = jedis.keys(String pattern);
//移出给定的一个或多个key,如果key不存在,则忽略该命令
jedis.del(String keyname1,String keyname2…….);

 //检查给定key是否存在 ,返回一个boolean
 jedis.exists(String keyname);
 //将key1改名为key2,当key1和key2相同或者key1不存在时,返回一个错误
 jedis.rename(String key1 , String key2);
  //返回给定key所储存的值的类型。 none(key不存在),string(字符串),list(列表),set(集合),zset(有序集),hash(哈希表)
  jedis.type(String keyname);
  //移出给定key的生存时间(设置这个key永不过期)
  jedis.persist(String keyname);
  //设置key生存时间,当key过期时,它会被自动删除。 Time为秒数
  jedis.expire(String keyname , int time);

  

   Jedis操作String数据相关方法:

//字符串值value关联到key,如果key不存在则会新增一个key
jedis.set(String keyname, String value); 
//将值value关联到key,并将key的生存时间设为seconds(秒)。
jedis.setex(String keyname,String value ,int secondes); 
//清空整个redis实例所有的key 
jedis.flushAll(); 
//清空redis当前数据库所有的key 
jedis.flushDB(); 
//返回key的个数
jedis.dbSize();

  

  Jedis操作Hash表相关方法:

//设置一张hash表的两种方式:
//1、分别给哈希表key中的域设置值,分别对key中的字段1、字段2设置值
jedis.hset(String keyname , String fieldname1 , String value1);
jedis.hset(String keyname , String fieldname2 , String value2);
//2、用一个JAVA中的HashMap直接存储为redis中的哈希表
Map map = new HashMap();
map.put(String fieldname1 , String value1);
map.put(String filedname2 , String value2);
jedis.hmset(String keyname , Map map);
//返回哈希表key中给定字段field的值,返回对应值的类型
jedis.hget(String keyname , String fieldname);
//返回哈希表key中给定多个字段的值,返回一个List
jedis.hget(String keyname , String fieldname1,String fieldname2….);
//返回哈希表key中的所有字段和值,返回一个Map<String,String>
jedis.hgetAll(String keyname);
//删除哈希表key中的一个或多个指定字段
jedis.hdel(String keyname , String fieldname1 , String fieldname2…..);
//查看哈希表key中,给定字段field是否存在。 
jedis.hexists(String keyname , String fieldname); 
//返回哈希表key中的所有字段 
jedis.hkeys(String keyname); 
//返回哈希表key中的所有值 
jedis.hvals(String keyname);

  

  Jedis操作List列表数据相关方法:

//将值value插入到列表key的表头。 
jedis.lpush(String keyname , String value);
//返回列表key中指定区间内的元素,区间以偏移量start和stop指定. 下标(index)参数start和stop从0开始; 负数下标代表从后开始(-1表示列表的最后一个元素,-2表示列表的倒数第二个元素,以此类推)  返回一个List
jedis.lrange(String keyname , int start , int stop);
//返回列表key的长度 , 返回一个Int
jedis.llen(String keyname);

  

  Jedis操作Set集合数据相关方法:

//将member元素加入到集合key当中。
jedis.sadd(String keyname , String value);
//移除集合中的member元素。
jedis.srem(String keyname , String value);
//返回集合key中的所有成员。返回一个set
jedis.smembers(String keyname);
//判断元素是否是集合key的成员
jedis.sismember(String keyname , String value);
//返回集合key的元素的数量
jedis.scard(String keyname);
//返回一个集合的全部成员,该集合是所有给定集合的交集
jedis.sinter(String key1 , String key2);
//返回一个集合的全部成员,该集合是所有给定集合的并集
jedis.union(String key1 , String key2);
//返回一个集合的全部成员,该集合是所有给定集合的差集
jedis.sdiff(String key1 , String key2);

 

 

  

 

 

    

 

 

   

Guess you like

Origin www.cnblogs.com/268lwc/p/12407547.html