素材分享网站源码,安全网站开发,已备案网站,软件开发具体做什么在 Spring 中使用 Redis#xff0c;除了需要 jedis.jar 外#xff0c;还需要下载 spring-data-redis.jar#xff0c;这里值得注意的是 jar 包和 Spring 版本兼容的问题#xff0c;我使用的 jar 包版本是 1.8.1#xff0c;而 Spring 的版本是 5.0.4#xff0c;如果使用其他…在 Spring 中使用 Redis除了需要 jedis.jar 外还需要下载 spring-data-redis.jar这里值得注意的是 jar 包和 Spring 版本兼容的问题我使用的 jar 包版本是 1.8.1而 Spring 的版本是 5.0.4如果使用其他的版本可能存在不兼容的问题从而产生异常。
把下载的 jar 包导入到工程环境中这样就可以在使用 Spring 提供的 RedisTemplate 操作 Redis 了只是在使用前需要对 Spring 提供的方案进行探讨以便更好地使用它们。
在大部分情况下我们都会用到连接池于是先用 Spring 配置一个 JedisPoolConfig 对象这个配置相对而言比较简单使用 Spring 配置 JedisPoolConfig 对象代码如下所示。
bean idpoolConfig classredis.clients.jedis.JedisPoolConfig!-- 最大空闲数 --property namemaxIdle value50 /!-- 最大连接数 --property namemaxTotal value100 /!-- 最大等待时间 --property namemaxWaitMillis value20000 /
/bean这样就设置了一个连接池的配置继续往下配置。
在使用 Spring 提供的 RedisTemplate 之前需要配置 Spring 所提供的连接工厂在 Spring Data Redis 方案中它提供了 4 种工厂模型。 JredisConnectionFactory。JedisConnectionFactory。LettuceConnectionFactory。SrpConnectionFactory。虽然使用哪种实现工厂都是可以的但是要根据环境进行测试以验证使用哪个方案的性能是最佳的。无论如何它们都是接口 RedisConnectionFactory 的实现类更多的时候我们都是通过接口定义去理解它们所以它们是具有接口适用性特性的。我将以使用最为广泛的 JedisConnectionFactory 为例进行讲解。
例如在 Spring 中配置一个 JedisConnectionFactory 对象配置 JedisConnectionFactory 代码如下所示。
bean idconnectionFactoryclassorg.springframework.data.redis.connection.jedis.JedisConnectionFactoryproperty namehostName valuelocalhost /property nameport value6379 /!--property namepassword valuepassword/ --property namepoolConfig refpoolConfig /
/bean解说 hostName代表的是服务器默认值是 localhost所以如果是本机可以不配置它。port代表的是接口端口默认值是 6379所以可以使用默认的 Redis 端口也可以不配置它。password代表的是密码在需要密码连接 Redis 的场合需要配置它。poolConfig是连接池配置对象可以设置连接池的属性。我们完成了一个 Redis 连接工厂的配置。这里配置的是 JedisConnectionFactory如果需要的是 LettuceConnectionFactory可以把使用 Spring 配置 JedisPoolConfig 对象代码中的 Bean 元素的 class 属性修改为 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactor 即可这取决于项目的需要和特殊性。有了 RedisConnectionFactory 工厂就可以使用 RedisTemplate 了。
普通的连接使用没有办法把 Java 对象直接存入 Redis而需要我们自己提供方案这时往往就是将对象序列化然后使用 Redis 进行存储而取回序列化的内容后在通过转换转变为 Java 对象Spring 模板中提供了封装的方案在它内部提供了 RedisSerializer 接口org.springframework.data.redis.serializer.RedisSerializer和一些实现类。
可以选择 Spring 提供的方案去处理序列化当然也可以去实现在 spring data redis 中定义的 RedisSerializer 接口在 Spring 中提供了以下几种实现 RedisSerializer 接口的序列化器。 GenericJackson2JsonRedisSerializer通用的使用 Json2.jar 的包将 Redis 对象的序列化器。Jackson2JsonRedisSerializerT通过 Jackson2.jar 包提供的序列化进行转换由于版本太旧Spring 不推荐使用。JdkSerializationRedisSerializerT使用 JDK 的序列化器进行转化。OxmSerializer使用 Spring O/X 对象 Object 和 XML 相互转换。StringRedisSerializer使用字符串进行序列化。GenericToStringSerializer通过通用的字符串序列化进行相互转换。使用它们就能够帮助我们把对象通过序列化存储到 Redis 中也可以把 Redis 存储的内容转换为 Java 对象为此 Spring 提供的 RedisTemplate 还有两个属性。 keySerializer——键序列器。valueSerializer——值序列器。有了上面的了解就可以配置 RedisTemplate 了。选用 StringRedisSerializer 作为 Redis 的 key 的序列化器而使用 JdkSerializationRedisSerializer 作为其 value 的序列化器则可以以下代码的方法来配置 RedisTemplate。
bean idjdkSerializationRedisSerializerclassorg.springframework.data.redis.serializer.JdkSerializationRedisSerializer /
bean idstringRedisSerializerclassorg.springframework.data.redis.serializer.StringRedisSerializer /
bean idredisTemplate classorg.springframework.data.redis.core.RedisTemplateproperty nameconnectionFactory refconnectionFactory /property namekeySerializer refstringRedisSerializer /property namevalueSerializer refjdkSerializationRedisSerializer /
/bean这样就配置了一个 RedisTemplate 的对象并且 spring data redis 知道会用对应的序列化器去转换 Redis 的键值。
举个例子新建一个角色对象使用 Redis 保存它的对象使用 Redis 保存角色类对象如下所示。
package com.pojo;
import java.io.Serializable;
public class Role implements Serializable {/*** 注意对象要可序列化需要实现Serializable接口往往要重写serialVersionUID*/private static final long serialVersionUID 3447499459461375642L;private long id;private String roleName;private String note;//省略setter和getter方法
}因为要序列化对象所以需要实现 Serializable 接口表明它能够序列化而 serialVersionUID 代表的是序列化的版本编号。
接下来就可以测试保存这个 Role 对象了测试代码如下所示。
ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);
RedisTemplate redisTemplate applicationContext.getBean(RedisTemplate.class);
Role role new Role();
role.setId(1L);
role.setRoleName(role_name_1);
role.setNote (note_l);
redisTemplate.opsForValue().set(role_1, role);
Role role1 (Role) redisTemplate.opsForValue().get (role_1);
System.out.println(role1.getRoleName());在 System.out.println(role1.getRoleName()) 这行打下断点会发现这里已经成功保存和获取了一个 Java 对象这段代码演示的是如何使用 StringRedisSerializer 序列化 Redis 的 key而使用 JdkSerializationRedisSerializer 序列化 Redis 的value当然也可以根据需要去选择甚至是自定义序列化器。
注意以上的使用都是基于 RedisTemplate、基于连接池的操作换句话说并不能保证每次使用 RedisTemplate 是操作同一个对 Redis 的连接比如上面代码中的下面两行代码。set 和 get 方法看起来很简单它可能就来自于同一个 Redis 连接池的不同 Redis 的连接。
为了使得所有的操作都来自于同一个连接可以使用 SessionCallback 或者 RedisCallback 这两个接口而 RedisCallback 是比较底层的封装其使用不是很友好所以更多的时候会使用 SessionCallback 这个接口通过这个接口就可以把多个命令放入到同一个 Redis 连接中去执行代码如下所示它主要是实现了上面代码中的功能。
package redisDemo;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;import com.pojo.Role;public class Test {public static void main(String[] args) {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);RedisTemplateString, Role redisTemplate applicationContext.getBean(RedisTemplate.class);Role role new Role();role.setId(1L);role.setRoleName(role_name_1);role.setNote(role_note_1);SessionCallback callBack new SessionCallbackRole() {Overridepublic Role execute(RedisOperations ops) throws DataAccessException {ops.boundValueOps(role_1).set(role);return (Role) ops.boundValueOps(role_1).get();}};Role savedRole (Role) redisTemplate.execute(callBack);System.out.println(savedRole.getId());}
}这样 set 和 get 命令就能够保证在同一个连接池的同一个 Redis 连接进行操作。