Jedis连接Redis

  上篇博客介绍完了Redis的安装,既然服务已经安装好了,也能正常启动了,那么下面就要用代码测试一下到底服务好不好使。下面就记录一下Jedis连接Redis的过程,执行起来比较简单,和在Redis的命令操作页面执行命令是一样的。

 

Jedis连接Redis

 

1、引入jedis的jar包

 

  在当前项目中引入jedis的jar包,我的项目是maven项目,所以直接在pom.xml文件中加入以下依赖:

 

<!-- Redis客户端 -->
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

  加入依赖后,更新maven工程,检查maven依赖的jar包中是否添加成功,添加成功后就可以用了。

 

 

2、编写代码

 

  在maven工程的src/test/java包下创建一个包com.tgb.test.jedis,在该包中添加测试类JedisTest。首先利用单个Jedis对象连接Redis服务。

 

@Test
public void testJedisSingle() {
	// 创建一个jedis对象
	Jedis jedis = new Jedis("192.168.243.3", 6379);
	// 直接调用jedis对象的方法,方法名称和redis的命令一致
	jedis.set("key1", "feng");
	jedis.set("key2", "yao");
	String key1 = jedis.get("key1");
	String key2 = jedis.get("key2");
	System.out.println(key1 + " " + key2);
	// 关闭jedis
	jedis.close();
}

  创建对象时,构造函数需要传入两个参数,第一个是Redis服务安装的虚拟机的IP,第二个是Redis服务的端口,默认是6379,如果安装时修改过,就传入修改的端口号。Redis是key-value型数据库,set(key,value)是向缓存库中添加数据,get(key)是从缓存库中取数据,执行上面的方法会得到结果:feng yao。

 

 

  Jedis也可以通过连接池的形式来连接Redis的服务,类似于数据库连接池。

 

@Test
public void testJedisPool() {
	// 创建一个jedis连接池
	JedisPool jedisPool = new JedisPool("192.168.243.3", 6379);
	// 从连接池中获得Jedis对象
	Jedis jedis = jedisPool.getResource();
	String key1 = jedis.get("key1");
	String key2 = jedis.get("key2");
	System.out.println(key1 + " " + key2);
	// 关闭jedis对象
	jedis.close();
	jedisPool.close();
}

  连接池的对象要及时关闭,否则会一直占用连接池的数量,如果连接的对象达到了连接池的上限,连接池便无法再提供连接了。上面方法得到的结果依然是:feng yao。

 

 

Spring框架整合Jedis

 

  Spring框架整合Jedis也比较简单,只需要在Spring的配置文件中配置相应内容即可,这里是创建了一个applicationContext-jedis.xml,并在web.xml中加载Spring容器时扫描该配置文件。

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<!-- 连接池配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow" value="true" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle" value="true" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted" value="false" />
	</bean>
        <!-- jedis客户端单机版 -->
	<bean id="redisClient" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.243.3"></constructor-arg>
		<constructor-arg name="port" value="6379"></constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
	</bean>
	<bean id="jedisClient" class="com.taotao.rest.dao.impl.JedisClientSingle"/>
</beans>

 

  Jedis连接Redis服务,利用的就是JedisPool,Spring整合Jedis只需要把JedisPool类在Spring框架加载时注入就行,这样代码中就可以直接用了。

 

  至此为止,Redis的服务算是完全安装测试完了,我们就可以在项目中连接Redis服务了。初次学习,如有不合适或不对的地方,希望过路的大牛指正,谢谢!

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值