美文网首页
springboot整合redis(包含存储一个对象)

springboot整合redis(包含存储一个对象)

作者: eliteTyc | 来源:发表于2019-09-26 09:35 被阅读0次

springboot整合redis

  • 1.导入依赖
    <!--设置redis缓存依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    
  • 2.设置redis连接数据
        # Redis数据库索引(默认为0)
        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=6000
    
  • 3.测试
    stringRedisTemplate.opsForValue().set("name","Tony");
        System.out.println(stringRedisTemplate.opsForValue().get("name"));
    
  • 4.如果需要存储对象,需要自行实现序列化与反序列化
    • 先实现RedisSerializer接口
    public class RedisObjectSerializer implements RedisSerializer<Object> {
    
    private Converter<Object, byte[]> serializer = new SerializingConverter();
    private Converter<byte[], Object> deserializer = new DeserializingConverter();
    
    static final byte[] EMPTY_ARRAY = new byte[0];
    
    public Object deserialize(byte[] bytes) {
        if (isEmpty(bytes)) {
            return null;
        }
    
        try {
            return deserializer.convert(bytes);
        } catch (Exception ex) {
            throw new SerializationException("Cannot deserialize", ex);
        }
    }
    
    public byte[] serialize(Object object) {
        if (object == null) {
            return EMPTY_ARRAY;
        }
    
        try {
            return serializer.convert(object);
        } catch (Exception ex) {
            return EMPTY_ARRAY;
        }
    }
    
    private boolean isEmpty(byte[] data) {
        return (data == null || data.length == 0);
    }
    }
    
    
    
    • 写一个配置类,配置存储对象时序列化与反序列化操作
    @Configuration
    public class RedisConfigration {
    
    
    
    @Bean
    public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,User> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }
    }
    
    • 测试
        @Autowired
    private RedisTemplate<String, User> redisTemplate;
    
    
    @Test
    public void contextLoads() {
        User user = new User("Tony","1234567899","四川省成都市",21,"男");
        redisTemplate.opsForValue().set(user.getName(),user);
        System.out.println(redisTemplate.opsForValue().get(user.getName()));
    
    
    }
    
    • 输出结果
    User(name=Tony, phone=1234567899, address=四川省成都市, age=21, gender=男)
    

相关文章

网友评论

      本文标题:springboot整合redis(包含存储一个对象)

      本文链接:https://www.haomeiwen.com/subject/waenectx.html