如何在spring-boot-starter-data-redis中获取Jedis实例?

时间:2018-06-09 12:10:24

标签: java spring-boot redis jedis

此示例是否正确?

连接池是否会自动关闭? 需要获得jedis实例来操作GEO

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

@Service
public class RedisGeo {

@Autowired
private RedisConnectionFactory redisConnectionFactory;

private void test() {
    RedisConnection redisConnection = redisConnectionFactory.getConnection();
    Jedis jedis = (Jedis) redisConnection.getNativeConnection();

    System.out.println(jedis);
    jedis.geoadd("demo",
            91.118970,
            29.654210,
            "dc");
   }
}

1 个答案:

答案 0 :(得分:0)

您不需要本地Jedis实例进行地理操作。 RedisTemplate有opsForGeo()方法返回GeoOperations接口。

所以你可以做到

redistTemplate.opsForGeo().add("demo", new Point(91.118970,29.654210), "dc");