Redis在互联网公司一般有以下应用:
String:缓存、限流、计数器、分布式锁、分布式Session
Hash:存储用户信息、用户主页访问量、组合查询
List:微博关注人时间轴列表、简单队列
Set:赞、踩、标签、好友关系
Zset:排行榜
-
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
-
添加配置
#redis数据库索引(默认为0),默认16个数据库,0-15.
spring.redis.database=0
#redis服务器地址
spring.redis.host=localhost
#redis服务器端口
spring.redis.port=6379
#redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
#连接池最大阻塞时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
#连接池最大空闲时间
spring.redis.jedis.pool.max-idle=8
#连接池最小空闲时间
spring.redis.jedis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=1000
-
定义对象序列化的方式
package com.zqz.springboot_layui.configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.lang.Nullable;
/** * @Author: Tom_Z
* @Data: 2019/1/24 11:22
* @Description: 定义redis序列化对象的方式
**/
public class RedisObjectSerializer implements RedisSerializer<Object>{
// 为了方便进行对象与字节数组的转换,所以应该首先准备出两个转换器
private Converter<Object, byte[]> serializingConverter = new SerializingConverter();
private Converter<byte[], Object> deserializingConverter = new DeserializingConverter();
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; // 做一个空数组,不是null
@Nullable
@Override
public byte[] serialize(@Nullable Object obj) throws SerializationException {
if (obj == null) {
// 这个时候没有要序列化的对象出现,所以返回的字节数组应该就是一个空数组
return EMPTY_BYTE_ARRAY ;
}
return serializingConverter.convert(obj);
}
@Nullable
@Override
public Object deserialize(@Nullable byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
// 此时没有对象的内容信息 return null ;
}
return deserializingConverter.convert(bytes);
}
}
-
配置redisTemplate模板
package com.zqz.springboot_layui.configuration;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Author: Tom_Z
* @Data: 2019/1/24 10:23
* @Description:
**/
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
-
创建实体对象
package com.zqz.springboot_layui.configuration;
import java.io.Serializable;
/**
* @Author: Tom_Z
* @Data: 2019/1/24 11:30
* @Description:
**/
public class RedisVO_User implements Serializable{
private String mid;
private Integer age;
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public Integer getAge() { return age; } public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "RedisVO_User{" + "mid='" + mid + '\'' + ", age=" + age + '}';
}
}
-
开始使用(测试)
package com.zqz.springboot_layui.configuration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisVO_UserTest {
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Test
public void testSet(){
RedisVO_User user = new RedisVO_User();
user.setMid("HELLO!");
user.setAge(18);
redisTemplate.opsForValue().set("vo",user);
}
@Test
public void testGet(){
System.out.println(redisTemplate.opsForValue().get("vo"));
}
}