Redis(四)----jedis的简单使用

我们安装了mysql并不是只是通过navicat这样的客户端来操作它。主要的用途,还是通过java程序来使用mysql数据库服务。我们的redis也一样。所以,我们看看,如果通过java程序来访问redis。

1.先启动redis服务

2.导入jar包

commons-pool2-2.3.jar

jedis-2.7.0.jar


3.单实例连接

public class JedisTest {

	// 通过java程序访问redis数据库
	@Test
	// 获得单一的jedis对象操作数据库
	public void test1() {

		// 1、获得连接对象
		Jedis jedis = new Jedis("192.168.17.128", 6379);
		// 2、获得数据
		String username = jedis.get("username");
		System.out.println(username);
		//3、存储
		jedis.set("addr", "beijing");
		System.out.println(jedis.get("addr"));
	}
}

4.连接池连接

// 通过jedis的pool获得jedis连接对象
@Test
public void test2() {

	// 0、创建连接池的配置对象
	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxIdle(30);// 最大闲置个数。只要超过30个闲置的,就开始关。
	poolConfig.setMinIdle(10);// 最小闲置个数。
	poolConfig.setMaxTotal(50);// 最大连接数。超过50个连接,就排队等。

	// 1、创建一个redis的连接池
	JedisPool pool = new JedisPool(poolConfig, "192.168.17.128", 6379);

	// 2、从池子中获取redis的连接资源
	Jedis jedis = pool.getResource();

	// 3、操作数据库
	jedis.set("xxx", "yyy");
	System.out.println(jedis.get("xxx"));
	
	//4、关闭资源
	jedis.close();
	pool.close();//真正开发中,池子是不用关的
}

5.封装getJedis的工具类

public class JedisPoolUtils {

	private static JedisPool pool = null;

	static {
		// 0、创建连接池的配置对象
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(30);// 最大闲置个数。只要超过30个闲置的,就开始关。
		poolConfig.setMinIdle(10);// 最小闲置个数。
		poolConfig.setMaxTotal(50);// 最大连接数。超过50个连接,就排队等。
		pool = new JedisPool(poolConfig, "192.168.17.128", 6379);
	}

	// 获得jedis资源的方法
	public static Jedis getJedis() {
		return pool.getResource();
	}

	public static void main(String[] args) {
		Jedis jedis = getJedis();
		System.out.println(jedis.get("xxx"));
	}
}

6.把创建连接池的配置对象所需的参数放到properties文件中

创建redis.properties

redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=50
redis.host=192.168.17.128
redis.port=6379
public class JedisPoolUtils {

	private static JedisPool pool = null;

	static {
		// 加载配置文件
		InputStream inputStream = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
		Properties pro = new Properties();
		try {
			pro.load(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 创建连接池的配置对象
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));// 最大闲置个数。只要超过30个闲置的,就开始关。
		poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));// 最小闲置个数。
		poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));// 最大连接数。超过50个连接,就排队等。
		pool = new JedisPool(poolConfig, pro.get("redis.host").toString(),
				Integer.parseInt(pro.get("redis.port").toString()));
	}

	// 获得jedis资源的方法
	public static Jedis getJedis() {
		return pool.getResource();
	}

	public static void main(String[] args) {
		Jedis jedis = getJedis();
		System.out.println(jedis.get("xxx"));
	}
}
源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值