Jedis工具类
配置文件:jedis.properties
host=127.0.0.1 port=6379 maxTotal=50 maxIdle=10
配置文件详情:
#最大活动对象数 redis.pool.maxTotal=1000 #最大能够保持idel状态的对象数 redis.pool.maxIdle=100 #最小能够保持idel状态的对象数 redis.pool.minIdle=50 #当池内没有返回对象时,最大等待时间 redis.pool.maxWaitMillis=10000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1. redis.pool.timeBetweenEvictionRunsMillis=30000 #向调用者输出“链接”对象时,是否检测它的空闲超时; redis.pool.testWhileIdle=true # 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3. redis.pool.numTestsPerEvictionRun=50 #redis服务器的IP redis.ip=xxxxxx #redis服务器的Port redis1.port=6379
工具类:
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JedisPoolUtils { private static JedisPool jedisPool; static { Properties properties = new Properties(); try { properties.load(JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties")); } catch (IOException e) { e.printStackTrace(); } JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle"))); config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal"))); jedisPool = new JedisPool(config,properties.getProperty("host"),Integer.parseInt(properties.getProperty("port"))); } /** * 获取连接的方法 */ public static Jedis getJedis(){ return jedisPool.getResource(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示