StringRedisConnection的Spring Bean范围

时间:2015-06-18 09:45:19

标签: spring-data spring-data-redis

我有以下两个Spring Data Redis的bean定义。我似乎无法找到相关文档来确定Web应用程序的这些bean的范围(单例,请求或会话)。

@Bean
public StringRedisTemplate redisTemplate() throws Exception {
    StringRedisTemplate redisTemplate = new StringRedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

@Bean
public StringRedisConnection stringRedisConnection() throws Exception {
    return new DefaultStringRedisConnection(redisTemplate().getConnectionFactory().getConnection());
}

1 个答案:

答案 0 :(得分:0)

感谢@Christoph Strobl推荐,这是Iam目前使用的实现

  public  List<String> testAutoComplete(String key,String query, int limitCount){
        StringRedisSerializer serializer = new StringRedisSerializer();
        RedisZSetCommands.Range range = Range.range();
        range.gt(query);
        RedisZSetCommands.Limit limit = new RedisZSetCommands.Limit();
        limit.count(limitCount);
       return template.execute(new RedisCallback< List<String>>() {
            public  List<String> doInRedis(RedisConnection connection) {
                Set<byte[]> results = connection.zRangeByLex(serializer.serialize(key), range,limit);
                List<String> resultAsString = new ArrayList<String>();
                for(byte[] result : results){
                    resultAsString.add(serializer.deserialize(result));
                }
                return resultAsString;
            }
        },false); 
    }