GitHub:https://github.com/asd821300801/Redis-Java-Client-Jedis/tree/spring-data-redis
spring-data-redis针对jedis提供了如下功能:
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作
BoundValueOperations
BoundSetOperations
BoundListOperations
BoundSetOperations
BoundHashOperations
将事务操作封装,有容器控制。
针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
基于设计模式,和JMS开发思路,将pub/sub的API设计进行了封装,使开发更加便捷。
spring-data-redis中,并没有对sharding提供良好的封装,如果你的架构是基于sharding,那么你需要自己去实现,这也是sdr和jedis相比,唯一缺少的特性。
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.0version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.5.RELEASEversion>
dependency>
<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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.lingdu.spring.redis"
annotation-config="true" />
<context:property-placeholder location="classpath:redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}">property>
<property name="maxIdle" value="${redis.pool.maxIdle}">property>
<property name="minIdle" value="${redis.pool.minIdle}">property>
<property name="maxWaitMillis" value="${redis.pool.maxTotal}">property>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}">property>
<property name="testOnReturn" value="${redis.pool.testOnReturn}">property>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}">property>
<property name="minEvictableIdleTimeMillis" value="300000">property>
<property name="numTestsPerEvictionRun" value="3">property>
<property name="timeBetweenEvictionRunsMillis" value="60000">property>
bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.auth}" />
<property name="timeout" value="15000" />
<property name="database" value="0" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
bean>
beans>
#最大连接数, 默认10个
redis.pool.maxTotal=10
#最小空闲连接数,默认2个
redis.pool.minIdle=2
#最大空闲连接数,默认10个
redis.pool.maxIdle=10
#最大的等待时间
redis.pool.maxWait=1000
#如果为true,表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉;
#这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
redis.pool.testWhileIdle=true
#获得一个jedis实例的时候是否检查连接可用性(ping());如果为true,则得到的jedis实例均是可用的;
redis.pool.testOnBorrow=true
#return 一个jedis实例给pool时,是否检查连接可用性(ping());
redis.pool.testOnReturn=true
#host
redis.ip=127.0.0.1
#port
redis.port=6379
#auth
redis.auth=123456
SpringUtil.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware{
private static ApplicationContext ctx;
public static void init(String path){
ctx=new ClassPathXmlApplicationContext(path);
}
public static T getBean(Class type){
return ctx.getBean(type);
}
public static Object getBean(String id){
return ctx.getBean(id);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx=applicationContext;
}
}
RedisTemplate中需要声明4种serializer,默认为“JdkSerializationRedisSerializer”:
1) keySerializer :对于普通K-V操作时,key采取的序列化策略
2) valueSerializer:value采取的序列化策略
3) hashKeySerializer: 在hash数据结构中,hash-key的序列化策略
4) hashValueSerializer:hash-value的序列化策略
建议key/hashKey采用StringRedisSerializer。
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
property>
bean>
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import com.lingdu.common.util.SpringUtil;
/**
* RedisTemplate 的简单使用
* @author LingDu
*/
@SuppressWarnings("unchecked")
public class SpringRedisTemp {
private static RedisTemplate redisTemplate;
private static ValueOperations valueOper;
static{
SpringUtil.init("classpath:application.xml");
redisTemplate = (RedisTemplate) SpringUtil.getBean("redisTemplate");
valueOper = redisTemplate.opsForValue();
}
//设置K-V
public void set(String key,String value){
valueOper.set(key, value);
}
//获取V
public String get(String key){
return (String) valueOper.get(key);
}
//通过K删除对应的V
public void remove(String key){
redisTemplate.delete(key);
}
public static void main(String[] args) {
SpringRedisTemp springRedisTemp = new SpringRedisTemp();
springRedisTemp.set("key", "value1");
System.out.println(springRedisTemp.get("key"));
//删除
springRedisTemp.remove("key");
System.out.println("删除之后:" + springRedisTemp.get("key"));
}
}
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷。
SDR提供了4种内置的serializer:
1、JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),数据以字节流存储
2、StringRedisSerializer:字符串编码,数据以string存储
3、JacksonJsonRedisSerializer:json格式存储
4、OxmSerializer:xml格式存储
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的序列化策略
import java.io.Serializable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import com.lingdu.common.util.SpringUtil;
/**
* 存储对象到Redis
*
* @author LingDu
*/
@SuppressWarnings("unchecked")
public class ObjectToRedis {
private static RedisTemplate redisTemplate;
private static ValueOperations valueOper;
static {
SpringUtil.init("classpath:application.xml");
redisTemplate = (RedisTemplate) SpringUtil.getBean("redisTemplate");
valueOper = redisTemplate.opsForValue();
}
// 设置K-V
public void setObject(String key, Object value) {
valueOper.set(key, value);
}
// 获取V
public String getObject(String key) {
return (String) valueOper.get(key);
}
// 通过K删除对应的V
public void removeObject(String key) {
redisTemplate.delete(key);
}
public static void main(String[] args) {
User u = new User();
u.setUserId("x101");
u.setUsername("LingDu");
u.setPassword("123456");
valueOper.set("user", u);
System.out.println(valueOper.get("user"));
ObjectToRedis objectToRedis = new ObjectToRedis();
objectToRedis.removeObject("user");
System.out.println("删除之后的对象:" + valueOper.get("user"));
}
}
/**
* 用于测试的对象,必须实现序列化接口
*
* @author LingDu
*/
class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userId;
private String username;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userId=" + userId + ", username=" + username + ", password=" + password + "]";
}
}
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" />
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import com.lingdu.common.util.SpringUtil;
/**
* Redis 操作 String
* @author LingDu
*/
public class StringRedisTemp {
private static StringRedisTemplate stringRedisTemplate;
private static ValueOperations stringOFV;
static{
SpringUtil.init("classpath:application.xml");
stringRedisTemplate = (StringRedisTemplate) SpringUtil.getBean("stringRedisTemplate");
stringOFV = stringRedisTemplate.opsForValue();
}
public void set(String key,String value){
stringOFV.set(key, value);
}
public String get(String key){
return stringOFV.get(key);
}
public static void main(String[] args) {
StringRedisTemp stringRedisTemp = new StringRedisTemp();
stringRedisTemp.set("string_key", "string_value");
System.out.println(stringRedisTemp.get("string_key"));
}
}