-
Redis概述
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它和MongoDB是当前使用最广泛的NoSQL,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)、HyperLogLog(基数),其中最常用的是字符串和哈希类型。此外,Redis还支持一些事务、发布订阅取消模式、主从复制、持久化等作为java开发人员需要知道的功能。 -
Redis的应用场景
①缓存常用的数据;
②在需要高速读/写的场合使用它快速读/写
-
Redis基本安装和使用
①打开网址https://github.com/ServiceStack/redis-windows/tree/master/downloads
②把下载的Redis文件解压,在这个目录新建一个文件startup.cmd,写入以下内容:
redis-server redis.windows.conf
③双击它就可以看到Redis的启动信息,这个时候就可以双击放在同一个文件夹下的文件redis-cli.exe,它是一个Redis自带的客户端工具。 -
在Spring中使用Redis
①在Spring中使用Redis,除了需要jedis.jar外(http://mvnrepository.com/artifact/redis.clients/jedis),
还需要下载spring-data-redis.jar
(http://mvnrepository.com/artifact/org.springframework.data/spring-data-redis)
②把下载的jar包导入到工程环境中,这样就可以在使用Spring提供的RedisTemplate操作Redis了,在大部分情况下我们都会用到连接池,于是先用Spring配置一个JedisPoolConfig对象
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50"/>
<!-- 最大连接数 -->
<property name="maxTotal" value="100"/>
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000"></property>
</bean>
③在使用Spring提供的RedisTemplate之前需要配置Spring所提供的连接工厂
- JredisConnectionFactory
- JedisConnectionFactory
- LettuceConnectionFactory
- SrpConnectionFactory
其中使用最为广泛的是JedisConnectionFactory;
在Spring中配置一个JedisConnectionFactory对象
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<!-- <property name="password" value=""/> -->
<property name="poolConfig" ref="poolConfig"/>
</bean>
④选择Spring提供的方案去处理序列化,在Spring中提供了几种RedisSerializer接口的序列化器,其中最常用的有StringRedisSerializer使用字符串进行序列化和
JdkSerializationRedisSerializer使用JDK序列化器进行转化,使用它们就能够帮助我们吧对象通过序列化存储道Redis中,可以把Redis存储的内容转化位Java对象,为此Spring提供的RedisTemplate还有两个属性:
- keySerializer——键序列器
- valueSerializer——值序列器
配置Spring RedisTamplate
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="jdkSerializationRedisSerializer" 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="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
eg:
①使用Redis保存角色类对象
import java.io.Serializable;
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private String roleName;
private String note;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
②使用RedisTemplate保存Role对象
@Test
public void Test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = ac.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("rolename");
role.setNote("note");
redisTemplate.opsForValue().set("role1", role);
Role role1 = (Role)redisTemplate.opsForValue().get("role1");
System.out.println(role1.getNote());
}
③为了使的所有的操作都来自于同一个连接,可以使用SessionCallBack接口,把多个命令放入到同一个Redis连接中去执行
@Test
public void Test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = ac.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("rolename");
role.setNote("note");
SessionCallback callback = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations ops) throws DataAccessException {
ops.boundValueOps("role1").set(role);
return (Role)ops.boundValueOps("role1").get();
}
};
Role savedRole = (Role) redisTemplate.execute(callback);
// redisTemplate.opsForValue().set("role1", role);
// Role role1 = (Role)redisTemplate.opsForValue().get("role1");
System.out.println(savedRole.getNote());
}