Jedis--连接池
程序员文章站
2022-05-18 09:25:24
...
黑马程序员
Jedis–连接池
@Test
public void test6(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(50);
jedisPoolConfig.setMaxIdle(10);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379);
Jedis resource = jedisPool.getResource();
resource.set("name","huang");
resource.close();
}
Jedis–连接池工具类
jedis.properties
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
package cn.itcast.jedis.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JedisPoolUtil {
public static JedisPool jedisPool;
static {
InputStream resourceAsStream = JedisPoolUtil.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
jedisPool = new JedisPool(jedisPoolConfig, properties.getProperty("host"), Integer.parseInt(properties.getProperty("port")));
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
@Test
public void test7(){
Jedis resource = JedisPoolUtil.getJedis();
resource.set("hello","huang");
System.out.println(resource.get("hello"));
resource.close();
}
上一篇: Redis在windows下的安装使用
下一篇: Docker_安装Nginx并运行
推荐阅读
-
dbcp 连接池不合理的锁导致连接耗尽解决方案
-
.NET Framework SQL Server 数据提供程序连接池
-
在Tomcat服务器下使用连接池连接Oracle数据库
-
详解Spring Boot Mysql 版本驱动连接池方案选择
-
MySQL中间件之ProxySQL(5):线程、线程池、连接池
-
.NET 数据库连接池
-
.net数据库连接池配置技巧(默认值)
-
Swoole Redis 连接池的实现
-
Python数据库连接池DBUtils
-
配置c3p0-config.xml数据库连接池,jdbcurl配置项报错Type The reference to entity "useUnicode" must end with the ';' delimiter.