Spring Bean Scope for StringRedisConnection

507 views Asked by At

I have the following two bean definitions for Spring Data Redis. I cant seem to find the relevant documentation to determine the scopes(singleton,request or session) of these beans for a web app.

@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

There are 1 answers

0
Shivam Sinha On BEST ANSWER

Thanks to @Christoph Strobl recommendation here is the implementation Iam currently using

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