Jedis连接Redis
1、引入jedis的jar包
在当前项目中引入jedis的jar包,我的项目是maven项目,所以直接在pom.xml文件中加入以下依赖:
加入依赖后,更新maven工程,检查maven依赖的jar包中是否添加成功,添加成功后就可以用了。
要是需要用到 spring-data-redis则 加入依赖:
2、编写代码
在maven工程的src/test/java包下创建一个包com.tgb.test.jedis,在该包中添加测试类JedisTest。首先利用单个Jedis对象连接Redis服务
@Test
public void testJedisSingle() {
// 创建一个jedis对象
Jedis jedis = new Jedis("192.168.1.101", 6379);
// 直接调用jedis对象的方法,方法名称和redis的命令一致
jedis.set("key1", "test01");
jedis.set("key2", "test02");
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)是从缓存库中取数据。
Jedis也可以通过连接池的形式来连接Redis的服务,类似于数据库连接池。
@Test
public void testJedisPool() {
// 创建一个jedis连接池
JedisPool jedisPool = new JedisPool("192.168.1.101", 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();
}
连接池的对象要及时关闭,否则会一直占用连接池的数量,如果连接的对象达到了连接池的上限,连接池便无法再提供连接了。
Spring框架整合Jedis
spring-redis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300"/>
<property name="maxWaitMillis" value="1500"/>
<property name="testOnBorrow" value="true"/>
</bean>
<!-- 连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="poolConfig" ref="jedisPoolConfig"/>
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<!-- <property name="password" value="${redis.password}"/>-->
<property name="timeout" value="${redis.timeout}"></property>
</bean>
<!-- redis模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>
</property>
</bean>
<!-- 启用缓存注解功能 -->
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
<property name="usePrefix" value="true"/>
<!-- 设置默认过期时间(秒) -->
<property name="defaultExpiration" value="900"/>
<property name="expires">
<map key-type="java.lang.String" value-type="java.lang.Long">
<!-- 设置字典业务缓存过期时间(秒) -->
<entry key="DICT" value="900"/>
</map>
</property>
</bean>
</beans>
properties配置:
#Redis连接信息
redis.host=192.168.1.101
redis.port=6379
redis.password=
redis.timeout=10000
测试:
package com.liuren.app.web.controller;
import com.liuren.app.web.enums.RedisCacheKeyEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* 测试用控制器
* date:2017/9/23 0023 - 15:17
*/
@Controller
@RequestMapping(value = "/test")
public class TestController extends BaseController {
@Resource(name = "redisTemplate")
private RedisTemplate<String,Object> redisTemplate ;
@RequestMapping(value = "/getRedis", method = RequestMethod.GET)
@ResponseBody
public String getRedis() {
redisTemplate.opsForValue().set(RedisCacheKeyEnum.SMS_AUTH_CODE.getPrefix() + "电话号码","123456789");
String _code = (String) redisTemplate.opsForValue().get(RedisCacheKeyEnum.SMS_AUTH_CODE.getPrefix() + "电话号码");
return _code;
}
}