zoukankan      html  css  js  c++  java
  • Redis工具之Jedis

        //jedis的连接池

     1 public void test1(){
     2         //创建连接池配置对象
     3         JedisPoolConfig poolConfig = new JedisPoolConfig();
     4         poolConfig.setMaxTotal(20); //最大资源个数
     5         //创建连接池
     6         //poolConfig 连接池的配置信息对象
     7         JedisPool jedisPool = new JedisPool(poolConfig, "192.168.**.**", 6379);
     8         //获得连接资源
     9         Jedis jedis = jedisPool.getResource();
    10         jedis.set("name", "张三疯"); //set name 张三疯
    11         jedis.hset("user", "username", "芷若"); // hset user username 芷若
    12         //取出数据
    13         String name = jedis.get("name");
    14         String hget = jedis.hget("user", "username");
    15         System.out.println(name);
    16         System.out.println(hget);
    17         //释放资源
    18         jedis.close();
    19         jedisPool.close();
    20     }

    //封装后的工具类

     1 public class RedisUtils {
     2     private static JedisPoolConfig poolConfig = null;
     3     private static JedisPool jedisPool = null;
     4     static{
     5 
     6         try {
     7             //读取配置文件 为连接参数赋值
     8             /*InputStream inStream = RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
     9             Properties props = new Properties();
    10             props.load(inStream);
    11             String host = props.getProperty("redis.host");
    12             int port = Integer.parseInt(props.getProperty("redis.port"));
    13             int maxTotal = Integer.parseInt(props.getProperty("redis.maxTotal"));*/
    14             
    15             //专门读取properties配置文件的
    16             //baseName:基本名称  不包含properties扩展名的名称
    17             //getBundle方法参数相对于src的地址
    18             ResourceBundle rb = ResourceBundle.getBundle("redis");
    19             String host = rb.getString("redis.host");
    20             int port = Integer.parseInt(rb.getString("redis.port"));
    21             int maxTotal = Integer.parseInt(rb.getString("redis.maxTotal"));
    22 
    23             poolConfig = new JedisPoolConfig();
    24             poolConfig.setMaxTotal(maxTotal); 
    25             jedisPool = new JedisPool(poolConfig,host, port);
    26 
    27         } catch (Exception e) {
    28             e.printStackTrace();
    29         }
    30     }
    31     public static Jedis getResource(){
    32         Jedis jedis = jedisPool.getResource();
    33         return jedis;
    34     }
    35 }

    //为了降低耦合性,将连接池的配置信息放入redis.properties配置文件中

     1 redis.host=192.168.**.**

    2 redis.port=6379

    3 redis.maxTotal=20 

    //测试工具的使用

    1 //测试jedis工具的使用
    2     public void test2(){
    3 
    4         Jedis jedis = RedisUtils.getResource();
    5         String name = jedis.get("name");
    6         System.out.println(name);
    7         jedis.close();
    8         
    9     }

     

  • 相关阅读:
    好的文章聚集地
    java连接mysql数据库8.0以上版本过程中遇到的坑
    Tomcat8.5安装与配置的坑
    用阿里fastJson解析Json字符串
    通过java代码HttpRequestUtil(服务器端)发送HTTP请求并解析
    Vue中美元$符号的意思
    java的jdk和jre区别
    java正则
    java8 四大核心函数式接口Function、Consumer、Supplier、Predicate(转载)
    SpringBoot:静态资源的访问和配置(转载)
  • 原文地址:https://www.cnblogs.com/itworkerlittlewrite/p/9451800.html
Copyright © 2011-2022 走看看