zoukankan      html  css  js  c++  java
  • Jedis工具类使用及设置

    Jedis是Redis数据库的java工具类,类似于JDBC中的Connection,也是对数据库进行CRUD的通道(这样说是不是有点不严谨~~~)

    附上几个Redis的通用命令:

    key* 查看所有的key值

    type   key 获得key的数据类型

    del key [key.....]
            可以删除一个或多个键,返回值是删除的键的个数
    exists key 判断key是否存在

    Jedis一般工具类的编写,如图:(此处以本机为数据库)

    代码如下:

     1 package com.didinx.filter;
     2 
     3 import redis.clients.jedis.Jedis;
     4 
     5 /**
     6  * @author o_0sky
     7  * @date 2019/2/14 1:07
     8  */
     9 public class Jedis1 {
    10     public static void main(String[] args) {
    11         //创建redis核心对象
    12         Jedis jedis = new Jedis("localhost", 6379);
    13         //存值
    14         jedis.set("key", "value");
    15         //取值并打印
    16         System.out.println(jedis.get("key"));
    17         //释放资源
    18         jedis.close();
    19     }
    20 }

     jedis如同connection一样,连接资源的创建和销毁都会很消耗性能,所以jedis提供了池化技术,下图是常用的池化技术的参数列表:

    联系上图:

    池化技术代码如下:

     1 package com.didinx.filter;
     2 
     3 import redis.clients.jedis.Jedis;
     4 import redis.clients.jedis.JedisPool;
     5 import redis.clients.jedis.JedisPoolConfig;
     6 
     7 /**
     8  * @author o_0sky
     9  * @date 2019/2/14 1:33
    10  */
    11 public class JedisPool1 {
    12     public static void main(String[] args) {
    13         //生成连接池配置对象,设置配置项
    14         JedisPoolConfig config = new JedisPoolConfig();
    15         //最大连接数
    16         config.setMaxTotal(100);
    17         //最大空闲数
    18         config.setMaxIdle(20);
    19         //获得连接池
    20         JedisPool jedisPool = new JedisPool(config, "localhost", 6379);
    21         Jedis jedis = null;
    22         try {
    23             //从连接池获取连接
    24             jedis = jedisPool.getResource();
    25             //给连接赋值
    26             jedis.set("age", "30");
    27             //从连接池中取值并打印
    28             System.out.println(jedis.get("age"));
    29         } catch (Exception e) {
    30             e.printStackTrace();
    31         } finally {
    32             //此处jedis.close()并不是销毁连接,而是将连接返回连接池
    33             if (null != jedis) {
    34                 jedis.close();
    35             }
    36         }
    37         //电脑关闭时,关闭连接池
    38         if (null != jedisPool) {
    39             jedisPool.close();
    40         }
    41 
    42     }
    43 }

    如同JDBC,Jedis也有工具类,配置文件为properties格式,代码如下:

     1 package didinx;
     2 
     3 import redis.clients.jedis.Jedis;
     4 import redis.clients.jedis.JedisPool;
     5 import redis.clients.jedis.JedisPoolConfig;
     6 
     7 import java.util.ResourceBundle;
     8 
     9 /**
    10  * @author o_0sky
    11  * @date 2019/2/14 1:49
    12  */
    13 public class JedisUtil {
    14     //设置参数
    15     private static String host;
    16     private static int port;
    17     private static int maxtotal;
    18     private static int maxwaitmillis;
    19     private static JedisPool jedisPool;
    20 
    21     //加载配置文件并给参数赋值
    22     static {
    23         ResourceBundle rb = ResourceBundle.getBundle("jedis");
    24         maxtotal = Integer.parseInt(rb.getString("maxtotal"));
    25         maxwaitmillis = Integer.parseInt(rb.getString("maxwaitmillis"));
    26         port = Integer.parseInt(rb.getString("port"));
    27         host = rb.getString("host");
    28     }
    29 
    30     /**
    31      * 初始化连接池
    32      */
    33     static {
    34         JedisPoolConfig config = new JedisPoolConfig();
    35         config.setMaxTotal(maxtotal);
    36         config.setMaxWaitMillis(maxwaitmillis);
    37         jedisPool = new JedisPool(config, host, port);
    38     }
    39 
    40     /**
    41      * 获取连接方法
    42      *
    43      * @return
    44      */
    45     public static Jedis getJedis() {
    46         return jedisPool.getResource();
    47     }
    48 
    49     /**
    50      * 释放资源
    51      *
    52      * @param jedis
    53      */
    54     public static void closeJedis(Jedis jedis) {
    55         if (null != jedis) {
    56             jedis.close();
    57         }
    58     }
    59 }
  • 相关阅读:
    3500常用汉字与标点符号(已排除不支持GB2312的)
    http报头正文开头会有一个整数的问题
    Arduino "Card failed, or not present"(即找不到SD卡)错误解决方案
    Arduino运行时突然[卡死在某一行/立即重启/串口输出乱码/程序执行不正常]的可能原因
    C++编程常见错误
    本地Apache服务器访问时502 Server dropped connection 错误解决方法
    Borůvka (Sollin) 算法求 MST 最小生成树
    搜索算法总结:迭代加深、双向、启发式
    三分法
    状压 DP:[USACO06NOV] Corn Fields,[USACO13NOV] No Change
  • 原文地址:https://www.cnblogs.com/linsky/p/10367848.html
Copyright © 2011-2022 走看看