微服务 第九章 springboot 使用NoSql数据库:redis

1、pom.xml添加依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.3.2.RELEASE</version>
        </dependency>    

2、在application.properties中配置redis:

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

3、配置类:

@Configuration
public class RedisConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }
    @Bean
    public RedisTemplate<String, Primary> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Primary> template = new RedisTemplate<String, Primary>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }
}

  

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);
  }
}

4、添加controller类测试:

@RestController
@RequestMapping(value="redisprimary")
public class PrimaryController {
    @Autowired
    PrimaryRepository repository;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, Primary> redisTemplate;

    @RequestMapping("getall")
    public List<Primary> getAll(){

        stringRedisTemplate.opsForValue().set("stringset", "111");
        System.out.println(stringRedisTemplate.opsForValue().get("stringset"));
        System.out.println(redisTemplate.opsForValue().get("yy"));
        List<Primary> primaryList=repository.findAll();
        for (Primary p:primaryList){
            System.out.println(p);
        }
        Primary primary=primaryList.get(0);
        redisTemplate.opsForValue().set("yy", primary);
        System.out.println(primary);
        System.out.println(redisTemplate.opsForValue().get("yy"));
        return primaryList;
    }
}

  5、注意:
             因为向redis保存对象,需要对象时序列化对象,故需要实现接口Serializable。
             运行项目测试的时候,必须启动redis

原文地址:https://www.cnblogs.com/yaohuiqin/p/9413880.html