springboot_redis

2020-12-13 03:53

阅读:496

标签:row   tag   serial   compiler   unknown   set   res   actor   gen   

1.引入redis的启动器

dependency>
    groupId>org.springframework.bootgroupId>
    artifactId>spring-boot-starter-data-redisartifactId>
dependency>

2.指定redis的主机地址

技术图片

3.自动注入模板

    @Autowired
    StringRedisTemplate stringRedisTemplate;   //k v都是字符串的

    @Autowired
    RedisTemplate redisTemplate;   //k v都是对象的

 redsiAutoConfiguration:

技术图片技术图片
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.autoconfigure.data.redis;

import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
    public RedisAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean(
        name = {"redisTemplate"}
    )
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
View Code

技术图片

 @Test
    public void test01(){
        stringRedisTemplate.opsForValue().append("k2","_append_k2");
    }

技术图片


 测试保存对象:

@Test
    public void test02(){
        User user = new User();
        user.setId(36);
        user.setAddress("上海市黄浦区赵家桥285号");
        user.setAge(21);
        user.setSex(1);
        user.setName("包青天");
        redisTemplate.opsForValue().set("usr1",user);//默认以java序列化的形式保存
    }

 技术图片


 添加配置,使其能以json的数据形式存入redis中:

package ustc.anmin.springboot.service;

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.Jackson2JsonRedisSerializer;
import ustc.anmin.springboot.bean.User;

import java.net.UnknownHostException;

@Configuration
public class MyRedisConfig {

    @Bean
    public RedisTemplate userRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        //关键
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer(User.class));
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

 技术图片

技术图片

 


 将redis设为缓存中间件:

1.在pom.xml中引入redis的starter

2.将数据格式转为json形式  要修改redisManager

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置CacheManager的值序列化方式为json序列化
        RedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
        RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair
                .fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(pair);
        //设置默认超过期时间是30秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
        //初始化RedisCacheManager
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }

 

其余设置与spring-boot启动缓存的方式一样。

springboot_redis

标签:row   tag   serial   compiler   unknown   set   res   actor   gen   

原文地址:https://www.cnblogs.com/ustc-anmin/p/11093688.html


评论


亲,登录后才可以留言!