[Spring MVC菜鸟之旅] Spring Data Redis整合

The Spring Framework is the leading full-stack Java/JEE application framework. It provides a lightweight container and a non-invasive programming model enabled by the use of dependency injection, AOP, and portable service abstractions.
NoSQL storages provide an alternative to classical RDBMS for horizontal scalability and speed. In terms of implementation, Key Value stores represent one of the largest (and oldest) members in the NoSQL space.
The Spring Data Redis (or SDR) framework makes it easy to write Spring applications that use the Redis key value store by eliminating the redundant tasks and boiler plate code required for interacting with the store through Spring’s excellent infrastructure support.

Redis

使用Redis

此处简单介绍一下Linux下得Redis安装,windows比较麻烦没有尝试

下载、解压并编译Redis文件

1
2
3
4
$ wget http://download.redis.io/releases/redis-3.2.4.tar.gz
$ tar xzf redis-3.2.4.tar.gz
$ cd redis-3.2.4
$ make

运行redis-server

1
$ sudo ./src/redis-server

启动Redis客户端

1
$ sudo ./src/redis-cli

Redis 数据类型

Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

String(字符串)

string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value。
string类型是二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象 。
string类型是Redis最基本的数据类型,一个键最大能存储512MB。

1
2
3
4
5
127.0.0.1:6379> set name "runoob"
OK
127.0.0.1:6379> get name
"runoob"
127.0.0.1:6379>

Hash(哈希)

Redis hash 是一个键值对集合。
Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> hmset user:1 username runoob password runoob points 200
OK
127.0.0.1:6379> hgetall user:1
1) "username"
2) "runoob"
3) "password"
4) "runoob"
5) "points"
6) "200"

以上实例中 hash 数据类型存储了包含用户脚本信息的用户对象。 实例中我们使用了 Redis HMSET, HGETALL 命令,user:1 为键值。
每个 hash 可以存储 232 -1 键值对(40多亿)。

List(列表)

Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> lpush runoob redis
(integer) 1
127.0.0.1:6379> lpush runoob mongodb
(integer) 2
127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"

列表最多可存储 232 - 1 元素 (4294967295, 每个列表可存储40多亿)。

Set(集合)

Redis的Set是string类型的无序集合。
集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。

sadd命令

添加一个string元素到,key对应的set集合中,成功返回1,如果元素已经在集合中返回0,key对应的set不存在返回错误。

1
sadd key member
1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> sadd runoobs redis
(integer) 1
127.0.0.1:6379> sadd runoobs mongodb
(integer) 1
127.0.0.1:6379> sadd runoobs rabitmq
(integer) 1
127.0.0.1:6379> sadd runoobs rabitmq
(integer) 0
127.0.0.1:6379> smembers runoobs
1) "rabitmq"
2) "mongodb"
3) "redis"

注意:以上实例中 rabitmq 添加了两次,但根据集合内元素的唯一性,第二次插入的元素将被忽略。
集合中最大的成员数为 232 - 1(4294967295, 每个集合可存储40多亿个成员)。

zset(sorted set : 有序集合)

Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
zset的成员是唯一的,但分数(score)却可以重复。

zadd命令

添加元素到集合,元素在集合中存在则更新对应score

1
zadd key score member
1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> zadd runoobz 0 redis
(integer) 1
127.0.0.1:6379> zadd runoobz 0 mongodb
(integer) 1
127.0.0.1:6379> zadd runoobz 0 rabitmaq
(integer) 1
127.0.0.1:6379> zadd runoobz 0 rabitmaq
(integer) 0
127.0.0.1:6379> zrangebyscore runoobz 0 10
1) "mongodb"
2) "rabitmaq"
3) "redis"

整合Spring Data Redis

Spring Redis要求Redis的版本不低于2.6,Java SE版本不低于6.0,在语言绑定(连接器)方面,Spring Redis集成了Jedis,JRedis,SRP和Lettuce四个流行的Redis方面的Java库。

添加相关文件

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>

配置RedisConnectionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
</bean>
<!-- Jedis ConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.hostname}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="poolConfig" />
</bean>

通过RedisTemplate使用对象

Most users are likely to use RedisTemplate and its coresponding package org.springframework.data.redis.core - the template is in fact the central class of the Redis module due to its rich feature set. The template offers a high-level abstraction for Redis interactions. While RedisConnection offers low level methods that accept and return binary values (byte arrays), the template takes care of serialization and connection management, freeing the user from dealing with such details.

1
2
3
4
5
6
7
8
9
10
11
<!-- Serializer -->
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果存储其他类型的对象,将会提示错误-->
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="keySerializer"/>
<property name="valueSerializer" ref="valueSerializer"/>
</bean>

RedisTemplate uses a Java-based serializer for most of its operations. This means that any object written or read by the template will be serialized/deserialized through Java. The serialization mechanism can be easily changed on the template, and the Redis module offers several implementations available in the org.springframework.data.redis.serializer package - see Serializers for more information. You can also set any of the serializers to null and use RedisTemplate with raw byte arrays by setting the enableDefaultSerializer property to false. Note that the template requires all keys to be non-null - values can be null as long as the underlying serializer accepts them; read the javadoc of each serializer for more information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.springmvc.niklaus.dao.Impl;
@Repository
public class TestDAOImpl implements TestDAO{
@Resource
private RedisTemplate<String,String> redisTemplate;
public void rSave(String key,String value){
redisTemplate.opsForValue().set(key,value);
}
public String rGet(String key){
return redisTemplate.opsForValue().get(key);
}
}

参考&引用

Redis
Spring Data Redis
Spring Data Redis - API
Spring Data Redis - Projects

更新时间

发布时间 : 2016-07-09

看你的了!