使用spring boot将对象列表保存到redis

时间:2017-07-04 19:37:16

标签: java spring-boot redis

我创建了RedisConfig类,其中包含Bean JedisConnectionFactoryRedisTemplateRedisTemplate看起来像这样:

@Bean
RedisTemplate<String, Object> redisTemplate() {
    final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();
    template.setConnectionFactory( jedisConnectionFactory() );
    template.setKeySerializer( new StringRedisSerializer() );
    template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
    template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
    return template;
}

我的pom.xml包含:

   <!--  Spring Boot Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
    </dependency>

当我得到一个对象列表时,我创建了该服务:

@Service
public class YandexMapsService {
    @Autowired
    private final RedisTemplate<String, Object> redisTemplate;

    // something

    public List<ResultGeoObjectDto> getGeographicCoordinates(String geoParam) {
    List<ResultGeoObjectDto> resultGeoObjectDto = new ArrayList<>();
    yandexMapsRestApiClient.getGeographicObject(geoParam).forEach((GeoObjectDto geoItem) -> {
        resultGeoObjectDto.add(geoConverterService.convertToResultGeoObjectDto(geoItem));
    });
    return resultGeoObjectDto;
    }
}

返回之前,我希望将resultGeoObjectDto保存为Redis作为值,密钥为geoParam

我怎么做?感谢

更新
我试试:

redisTemplate.opsForList(geoParam, resultGeoObjectDto);

但是,我有错误:

 Error:(33, 22) java: method opsForList in class org.springframework.data.redis.core.RedisTemplate<K,V> cannot be applied to given types;
required: no arguments
found: 
java.lang.String,java.util.List<ru.geo.converter.model.ResultGeoObjectDto>

原因:实际和正式的参数列表长度不同

0 个答案:

没有答案