0
点赞
收藏
分享

微信扫一扫

Redis——简易工具类

快乐与微笑的淘气 2022-02-02 阅读 66

依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.10.2</version>
</dependency>

工具类

public class RedisUtil {
	private static JedisPool pool;

	public static Jedis getJedis() {
		if (pool == null) {
			init();
		}
		return pool.getResource();
	}

	private static void init() {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		String host = bundle.getString("host");
		Integer port = Integer.parseInt(bundle.getString("port"));
		Integer maxTotal = Integer.parseInt(bundle.getString("MaxTotal"));
		Integer maxIdle = Integer.parseInt(bundle.getString("MaxIdle"));

		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(maxIdle);
		config.setMaxTotal(maxTotal);
		pool = new JedisPool(config, host, port);
	}
}

redis.properties

host=localhost
port=6379
MaxTotal=50
MaxIdle=20

测试代码

@Test
public void fun1() {
	Jedis jedis = RedisUtil.getJedis();
	jedis.set("A", "你好,世界!");
	System.out.println(jedis.get("A"));
	jedis.close();
}

测试效果

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

你好,世界!
举报

相关推荐

0 条评论