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功能,缺乏必要的设计模式支持,对于开发者而言需要关注的太多。
1. redis使用场景
redis是一个开源的使用ansi c语言编写、支持网络、可基于内存亦可持久化的日志型、key-value数据库,并提供多种语言的api。
我们都知道,在日常的应用中,数据库瓶颈是最容易出现的。数据量太大和频繁的查询,由于磁盘io性能的局限性,导致项目的性能越来越低。
这时候,基于内存的缓存框架,就能解决我们很多问题。例如memcache,redis等。将一些频繁使用的数据放入缓存读取,大大降低了数据库的负担。提升了系统的性能。其实,对于hibernate以及mybatis的二级缓存,是同样的道理。利用内存高速的读写速度,来解决硬盘的瓶颈。
2. 配置使用redis
在applicationcontext-dao.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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:property-placeholder location="classpath:database.properties" />
<bean id="poolconfig" class="redis.clients.jedis.jedispoolconfig">
<property name="maxidle" value="${redis.maxidle}" />
<property name="maxtotal" value="${redis.maxactive}" />
<property name="maxwaitmillis" value="${redis.maxwait}" />
<property name="testonborrow" value="${redis.testonborrow}" />
</bean>
<bean id="connectionfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory">
<property name="hostname" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="poolconfig" ref="poolconfig"/>
</bean>
<bean id="stringserializer" class="org.springframework.data.redis.serializer.stringredisserializer"/>
<bean id="hashserializer" class="org.springframework.data.redis.serializer.jdkserializationredisserializer"/>
<bean id="redistemplate" class="org.springframework.data.redis.core.redistemplate">
<property name="connectionfactory" ref="connectionfactory" />
<property name="keyserializer" ref="stringserializer"/>
<property name="valueserializer" ref="stringserializer"/>
<property name="hashkeyserializer" ref="stringserializer" />
<property name="hashvalueserializer" ref="hashserializer"/>
</bean>
</beans>
database.properties配置文件如下:
redis.maxidle=10
redis.maxactive=20
redis.maxwait=10000
redis.testonborrow=true
redis.host=192.168.1.76
redis.port=6379
redis.pass=password1
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷。sdr提供了4种内置的serializer:
jdkserializationredisserializer:使用jdk的序列化手段(serializable接口,objectinputstrean,objectoutputstream),数据以字节流存储,pojo对象的存取场景,使用jdk本身序列化机制,将pojo类通过objectinputstream/objectoutputstream进行序列化操作,最终redis-server中将存储字节序列,是目前最常用的序列化策略。
stringredisserializer:字符串编码,数据以string存储,key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new string(bytes, charset)”和“string.getbytes(charset)”的直接封装。是最轻量级和高效的策略。
jacksonjsonredisserializer:json格式存储,jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时,需要明确指定class类型,因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】
oxmserializer:xml格式存储,提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。不过使用此策略,编程将会有些难度,而且效率最低;不建议使用。【需要spring-oxm模块的支持】
其中jdkserializationredisserializer和stringredisserializer是最基础的序列化策略,其中“jacksonjsonredisserializer”与“oxmserializer”都是基于stirng存储,因此它们是较为“高级”的序列化(最终还是使用string解析以及构建java对象)。 针对“序列化和发序列化”中jdkserializationredisserializer和stringredisserializer是最基础的策略,原则上,我们可以将数据存储为任何格式以便应用程序存取和解析(其中应用包括app,hadoop等其他工具),不过在设计时仍然不推荐直接使用“jacksonjsonredisserializer”和“oxmserializer”,因为无论是json还是xml,他们本身仍然是string。如果你的数据需要被第三方工具解析,那么数据应该使用stringredisserializer而不是jdkserializationredisserializer。
redistemplate中需要声明4种serializer,默认为“jdkserializationredisserializer”:
1) keyserializer :对于普通k-v操作时,key采取的序列化策略
2) valueserializer:value采取的序列化策略
3) hashkeyserializer: 在hash数据结构中,hash-key的序列化策略
4) hashvalueserializer:hash-value的序列化策略
无论如何,建议key/hashkey采用stringredisserializer。
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
3. redistemplate的使用
这个类作为一个模版类,提供了很多快速使用redis的api,而不需要自己来维护连接,事务。最初的时候,我创建的baseredisdao是继承自这个类的。继承的好处是我的每个dao中,都可以自由的控制序列化器,自由的控制自己是否需要事务,这个先不需要了解,跟着我目前的这种配置方法来即可。template提供了一系列的operation,比如valueoperation,hashoperation,listoperation,setoperation等,用来操作不同数据类型的redis。并且,redistemplate还提供了对应的*operationseditor,用来通过redistemplate直接注入对应的operation。
核心代码:
package com.npf.dao.impl;
import java.util.arraylist;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import javax.annotation.resource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.hashoperations;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.stereotype.repository;
import com.npf.dao.studentdao;
import com.npf.model.student;
@repository
public class studentdaoimpl implements studentdao{
@autowired
private redistemplate<string,student> redistemplate;
@resource(name=redistemplate)
private hashoperations<string,string,student> opsforhash;
public static final string student = student;
@override
public void save(student student) {
opsforhash.put(student, student.getid(), student);
}
@override
public student find(string id) {
student student = opsforhash.get(student, id);
return student;
}
@override
public void delete(string id) {
opsforhash.delete(student, id);
}
@override
public void update(student student) {
opsforhash.put(student, student.getid(), student);
}
@override
public list<student> findall() {
map<string, student> entries = opsforhash.entries(student);
list<student> stulist = new arraylist<student>();
for(entry<string, student> entry : entries.entryset()){
stulist.add(entry.getvalue());
}
return stulist;
}
}
控制层代码如下:
package com.npf.controller;
import java.util.list;
import java.util.uuid;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;
import com.npf.model.student;
import com.npf.service.studentservice;
@controller
public class studentcontroller {
@autowired
private studentservice studentservice;
@requestmapping(/student/save)
public string savestudent(student student){
string id = uuid.randomuuid().tostring();
system.out.println(id);
student.setid(id);
studentservice.save(student);
return redirect:/student/find/all;
}
@requestmapping(/student/update)
public string updatestudent(student student){
studentservice.update(student);
return redirect:/student/find/all;
}
@requestmapping(/student/to/save/form)
public string tosavestudentform(){
return save;
}
@requestmapping(/student/delete)
public string deletestudent(@requestparam(id) string id){
studentservice.delete(id);
return redirect:/student/find/all;
}
@requestmapping(/student/to/update/form)
public string toupdatestudentform(@requestparam(id) string id,model model){
student stu = studentservice.find(id);
model.addattribute(stu, stu);
return update;
}
@requestmapping(/student/find/all)
public string findstudents(model model){
list<student> stulist = studentservice.findall();
model.addattribute(stulist, stulist);
return list;
}
}
以上就是怎么配置使用redis的详细内容。