Spring-data-redis为spring-data模块中对redis的支持部分,简称为“SDR”,提供了基于jedis客户端API的高度封装以及与spring容器的整合,事实上jedis客户端已经足够简单和轻量级,而spring-data-redis反而具有“过度设计”的嫌疑。
jedis客户端在编程实施方面存在如下不足:
1) connection管理缺乏自动化,connection-pool的设计缺少必要的容器支持。
2) 数据操作需要关注“序列化”/“反序列化”,因为jedis的客户端API接受的数据类型为string和byte,对结构化数据(json,xml,pojo等)操作需要额外的支持。
3) 事务操作纯粹为硬编码
4) pub/sub功能,缺乏必要的设计模式支持,对于开发者而言需要关注的太多。
不过jedis与spring整合,也是非常的简单,参见“jedis连接池实例”.
一. spring-data-redis针对jedis提供了如下功能:
1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
- ValueOperations:简单K-V操作
- SetOperations:set类型数据操作
- ZSetOperations:zset类型数据操作
- HashOperations:针对map类型的数据操作
-
ListOperations:针对list类型的数据操作
3. 提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:
- BoundValueOperations
- BoundSetOperations
- BoundListOperations
- BoundSetOperations
- BoundHashOperations
4. 将事务操作封装,有容器控制。
5. 针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
- JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。
- StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。
- JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时,需要明确指定Class类型,因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】
- OxmSerializer:提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。不过使用此策略,编程将会有些难度,而且效率最低;不建议使用。【需要spring-oxm模块的支持】
针对“序列化和发序列化”中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的策略,原则上,我们可以将数据存储为任何格式以便应用程序存取和解析(其中应用包括app,hadoop等其他工具),不过在设计时仍然不推荐直接使用“JacksonJsonRedisSerializer”和“OxmSerializer”,因为无论是json还是xml,他们本身仍然是String。
如果你的数据需要被第三方工具解析,那么数据应该使用StringRedisSerializer而不是JdkSerializationRedisSerializer。
如果你的数据格式必须为json或者xml,那么在编程级别,在redisTemplate配置中仍然使用StringRedisSerializer,在存储之前或者读取之后,使用“SerializationUtils”工具转换转换成json或者xml,请参见下文实例。
6. 基于设计模式,和JMS开发思路,将pub/sub的API设计进行了封装,使开发更加便捷。
7.spring-data-redis中,并没有对sharding提供良好的封装,如果你的架构是基于sharding,那么你需要自己去实现,这也是sdr和jedis相比,唯一缺少的特性。
二.简单实例:
1) spring配置:
<beans xmlns="http://www.springframework.org/schema/beans" 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.xsd" default-autowire="byName"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="32"></property> <property name="maxIdle" value="6"></property> <property name="maxWait" value="15000"></property> <property name="minEvictableIdleTimeMillis" value="300000"></property> <property name="numTestsPerEvictionRun" value="3"></property> <property name="timeBetweenEvictionRunsMillis" value="60000"></property> <property name="whenExhaustedAction" value="1"></property> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="poolConfig" ref="jedisPoolConfig"></property> <property name="hostName" value="127.0.0.1"></property> <property name="port" value="6379"></property> <property name="password" value="0123456"></property> <property name="timeout" value="15000"></property> <property name="usePool" value="true"></property> </bean> <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"></property> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean> </beans>
2) 程序实例:
public class SpringDataRedisTestMain { /** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-redis-beans.xml"); RedisTemplate redisTemplate = (RedisTemplate)context.getBean("jedisTemplate"); //其中key采取了StringRedisSerializer //其中value采取JdkSerializationRedisSerializer ValueOperations<String, User> valueOper = redisTemplate.opsForValue(); User u1 = new User("zhangsan",12); User u2 = new User("lisi",25); valueOper.set("u:u1", u1); valueOper.set("u:u2", u2); System.out.println(valueOper.get("u:u1").getName()); System.out.println(valueOper.get("u:u2").getName()); } /** * 如果使用jdk序列化方式,bean必须实现Serializable,且提供getter/setter方法 * @author qing * */ static class User implements Serializable{ /** * */ private static final long serialVersionUID = -3766780183428993793L; private String name; private Date created; private int age; public User(){} public User(String name,int age){ this.name = name; this.age = age; this.created = new Date(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
3) RedisTemplate:
如果你使用过jedisPool连接池,在数据操作之前,你需要pool.getResource()即从连接池中获取“链接资源”(Jedis),在操作之后,你需要(必须)调用pool.returnResource()将资源归还个连接池。但是,spring-data-redis中,我们似乎并没有直接操作pool,那么spring是如何做到pool管理的呢??一句话:spring的“看门绝技”--callback。
- public <T> T execute(RedisCallback<T> action):这个方法是redisTemplate中执行操作的底层方法,任何基于redisTemplate之上的调用(比如,valueOperations)最终都会被封装成RedisCallback,redisTemplate在execute方法中将会直接使用jedis客户端API进行与server通信,而且在如果使用了连接池,则会在操作之后执行returnSource。
其他实例请参考如下链接:
1) serializer实例
2) operation实例
3) pub/sub实例
相关推荐
NULL 博文链接:https://fxzcollege6.iteye.com/blog/2210381
分片弹簧数据redis 增强 Spring Data Redis 以支持实例分片和弹性连接
spring-data-redis 使用spring框架实现对redis的封装访问,以便安全高效的访问 通过spring管理redis的实例等
Redis拾遗部分博客相关的实例代码,包括其中的抢红包案例,相关SQL也一并附上,导入之后,修改Redis的配置文件之后,可以直接运行,但是如果想要配置集群,也只需要修改连接配置即可
可以在以下链接中找到旧的基于非响应式servlet的spring-redis-websocket应用程序:部署到Docker播放Ctrl +单击此按钮以部署NGINX均衡的spring-redis-websocket负载的多个实例:安装与配置先决条件: 在本地或Docker...
'springboot-mybatis-redis','springboot-mybatis-redis-annotation','springboot-properties','springboot-restful','springboot-validation-over-json','springboot-webflux','spring-data-elasticsearch-crud','...
spring整合redis 简单实例搭建------------供新手学习。
Spring-Data-Redis使用的实例Demo
data-redis|[lettuce,redis,session redis,YAML配置,连接池,对象存储](https://github.com/smltq/spring-boot-demo/blob/master/data-redis/HELP.md) quartz|[Spring Scheduler,Quartz,分布式调度,集群,mysql持久化...
spring Acegi例子,很简单的一个acegi实例,容易理解
插入初始化数据:init.data.sql 3.运行。 管理员帐号:admin 密码:sojson ps:定时任务的sql会把密码改变为sojson.com 新版本说明:http://www.sojson.com/blog/164.html 和 ...
spring boot 集成redis,demo 对应博客地址:http://blog.csdn.net/woniu211111/article/details/54564308
内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观锁、SpEL表达式在SpringData里面的应用...
本课程内容包括Spring简介、Spring Boot简介、安装JDK、安装Maven、第...Spring Data JPA增删改查实例、安装Redis和可视化客户端、Redis缓存配置、Redis缓存实例、Redis消息、测试Spring Boot应用程序(Demo应用测试...
一、CacheCloud是做什么的CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少开发人员的...
主要介绍了Redis Template实现分布式锁,需要的朋友可以参考下
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
本课程内容包括Spring简介、Spring Boot简介、安装JDK、安装Maven、第一个Spring Boot...Spring Data JPA增删改查实例、安装Redis和可视化客户端、Redis缓存配置、Redis缓存实例、Redis消息、测试Spring Boot应用程序...