前期准备
(1)spring-data-redir1.7.2.jar
(2) spring4.3.2版本相关jar包
下载链接: spring4.2.3下载
注:其他版本可能会出现不兼容问题,出现各种错误。
第一步 配置redis连接池
使用reids 大多数情况下会使用连接池。在spring 中配置如下:
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50" />
<!-- 最大链接数 -->
<property name="maxTotal" value="100" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000" />
</bean>
第二步,配置redis连接工厂
在使用spring提供的redisTemplate 之前需要配置Spring所提供的连接工厂,在spring-data-redis方案中提供了四种工厂模型。
(1)JredisConnectionFactory
(2)JedisConnectionFactory
(3)LettureConnectionFactory
(4)SrpConnectionFactory
他们都是接口RedisConnectionFactory的实现类,这里我们使用
JedisConnectionFactory.
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="ip或域名" />
<property name="port" value="6379" />
<property name="poolConfig" ref="poolConfig" />
<!--<property name="password" value="密码"/> -->
</bean>
第三步 配置SpringTemplate
有了RedisConnectionFactory工厂,就可以使用RedisTemplate了。普通的连接没有办法把java对象直接存入redis.需要把对象序列化。Spring模板中提供了RedisSerializer接口和一些实现类。
可以选择Spring提供的方案序列化,也可以实现在spring data redis 中定义的RedisSerializer接口。本例使用StringRedisSerialier 和 jdkSerializationRedisSerializer分别作为键序列器和值序列器,配置如下:
<!-- 有了RedisConnectionFactory后配置redisTemplate -->
<bean id ="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name = "connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
第四步 新建序列化角色类用于测试
/**
- 因为要序列化对象,所以需要实现Serializable 接口,表明它能够序列化
*/
public class Role implements Serializable{
/**
* 因为要序列化对象,所以需要实现Serializable 接口,表明它能够序列化
*/
private long id;
public Role(long id, String roleName) {
super();
this.id = id;
this.roleName = roleName;
}
public Role() {
}
private String roleName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
第五步 测试
public class RedisTemplateText {
public static void main(String args[]) {
test();
}
public static void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
RedisTemplate<String, Role> redisTemplate = applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("redisTemplate Text");
redisTemplate.opsForValue().set("role1", role);
Role role1 = (Role)redisTemplate.opsForValue().get("role1");
System.out.println(role1.getRoleName());
}
}
注: 以上都是基于RedisTemplate,基于连接池的操作,不能保证每次使用redisTemplate是操作同一个对Redis 的连接。
可以使用SessionCallback接口实现使用同一个连接
public static void testSessionCallBack() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
RedisTemplate<String, Role> redisTemplate = applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("SessionCallBack Text");
SessionCallback callBack = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations ops) throws DataAccessException {
ops.boundValueOps("role2").set(role);
return (Role)ops.boundValueOps("role2").get();
}
};
Role saveRole = (Role)redisTemplate.execute(callBack);
System.out.println(saveRole.getRoleName());
}