• JedisPool


    1. package com.wsd;
    2. import redis.clients.jedis.Jedis;
    3. import redis.clients.jedis.JedisPool;
    4. import redis.clients.jedis.JedisPoolConfig;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.util.HashMap;
    8. import java.util.Map;
    9. import java.util.Properties;
    10. public class Redis {
    11. public static void main(String[] args) {
    12. //读取properties file
    13. String fileName = "redis.properties";
    14. Map<String,String> map = getProperties(fileName);
    15. String host = map.get("host");
    16. int port = Integer.valueOf( map.get("port") );
    17. String auth = map.get("auth");
    18. int select = Integer.valueOf( map.get("select") );
    19. //Jedis 连接池的配置类,它用于配置连接池的行为和属性
    20. JedisPoolConfig poolConfig = new JedisPoolConfig();
    21. poolConfig.setMaxTotal(5); // 最大连接数
    22. poolConfig.setMaxIdle(3); // 最大空闲连接数
    23. poolConfig.setMinIdle(1); // 最小空闲连接数
    24. //创建jedis连接池
    25. JedisPool jedisPool = new JedisPool(poolConfig,host,port,3000,auth,select);
    26. //从连接池获取连接
    27. Jedis jedis = jedisPool.getResource();
    28. //执行set命令
    29. String OK = jedis.set("name","罗小白");
    30. System.out.println(OK);
    31. //执行get命令
    32. String name = jedis.get("name");
    33. System.out.println("name=" + name);
    34. if(jedis != null)
    35. jedis.close();
    36. }
    37. private static Map<String,String> getProperties(String fileName){
    38. Map<String,String> map = new HashMap<>();
    39. Properties properties = new Properties();
    40. InputStream inputStream = null;
    41. try{
    42. //读取properties file
    43. inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    44. properties.load(inputStream);
    45. String host = properties.getProperty("host");
    46. String port = properties.getProperty("port");
    47. String auth = properties.getProperty("auth");
    48. String select = properties.getProperty("select");
    49. map.put("host",host);
    50. map.put("port",port);
    51. map.put("auth",auth);
    52. map.put("select",select);
    53. }catch (IOException ex){
    54. System.out.println("read file:" + fileName + " fail");
    55. }finally {
    56. if(inputStream != null);
    57. try {
    58. inputStream.close();
    59. } catch (IOException e) {
    60. e.printStackTrace();
    61. }
    62. }
    63. return map;
    64. }
    65. }

     

  • 相关阅读:
    点击查看详情 | 网页版微信客户管理系统如何操作试用?
    C++菱形继承问题与虚拟继承原理
    OSS对象存储命令管理、数据迁移
    【C++】 内存管理
    回调地狱、syn函数和await函数
    什么是过期域名?做网站用过期域名好不好?
    cas:155371-19-0/1-乙基-3-甲基咪唑六氟磷酸盐/[C2MIm]PF6/分子量:256.13/离子液体保存温度
    API网关功能一览
    Shell 批量创建文件夹
    获取boss直聘城市地区josn数据
  • 原文地址:https://blog.csdn.net/weixin_41812346/article/details/134449800