Jedis 连接池实例

package com.ucas.redisDemo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 测试类
 * 
 * @author user
 *
 */
public class JedisTest {

	public static void main(String[] args) {
		JedisPoolConfig config = new JedisPoolConfig(); // 连接池的配置对象
		config.setMaxTotal(100); // 设置最大连接数
		config.setMaxIdle(10); // 设置最大空闲连接数

		JedisPool jedisPool = new JedisPool(config, "192.168.3.118", 6379);

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource(); // 获取连接
			jedis.auth("123456"); // 设置密码
			jedis.set("name", "java Hello"); // 设置值
			String value = jedis.get("name"); // 获取值
			System.out.println(value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
			if (jedisPool != null) {
				jedisPool.close();
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Suyebiubiu/article/details/108532198