springboot整合redis,用spring-data-redis2.0的同时用jedis

最新版本的spring-data-redis很烦,很多以前用起来很方便的方法过时了。下面是2.0这个版本的搭建过程:

首先要了解这个版本

        (1)spring-data-redis是默认是用lettuce的,要用jedis需要单独出来配置

 

        (2)JedisConnectionFactory的所有set方法全部过时。

        (3)基本配置(host,port...)用redisstandaloneconfiguration,poo连接池这样的配置用jedispoolconfig

        (4)有自带的序列化类的,不需要自定义了

下面就贴出代码,注意这仅适用于jedis,如果不用jedis,用lettuce基本上啥也不用配置

pom.xml

 

<!-- 添加jedis客户端 -->

 

 

<dependency>

 

 

    <groupId>redis.clients</groupId>

 

 

    <artifactId>jedis</artifactId>

 

 

</dependency>

 

 

 

<!--redis-->

 

 

 

<dependency>

 

 

 

    <groupId>org.springframework.boot</groupId>

 

 

 

    <artifactId>spring-boot-starter-data-redis</artifactId>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在Spring Boot中使用Spring Data Redis进行Redis Geo操作,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.6.0</version> </dependency> ``` 然后在application.properties文件中配置Redis相关信息: ```properties # Redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 接下来,我们需要创建一个RedisGeoService来进行Geo操作: ```java @Service public class RedisGeoService { private final RedisTemplate<String, String> redisTemplate; public RedisGeoService(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } /** * 添加地理位置信息 * * @param key 键 * @param longitude 经度 * @param latitude 纬度 * @param member 成员 * @return Long */ public Long add(String key, double longitude, double latitude, String member) { Point point = new Point(longitude, latitude); return redisTemplate.opsForGeo().add(key, point, member); } /** * 获取两个地理位置的距离 * * @param key 键 * @param member1 成员1 * @param member2 成员2 * @param unit 距离单位 * @return Distance */ public Distance distance(String key, String member1, String member2, Metric unit) { return redisTemplate.opsForGeo().distance(key, member1, member2, unit); } /** * 获取指定成员的地理位置 * * @param key 键 * @param members 成员 * @return List<Point> */ public List<Point> position(String key, String... members) { return redisTemplate.opsForGeo().position(key, members); } /** * 获取指定地理位置附近的成员 * * @param key 键 * @param longitude 经度 * @param latitude 纬度 * @param radius 半径 * @param unit 距离单位 * @return GeoResults<GeoLocation<String>> */ public GeoResults<GeoLocation<String>> nearBy(String key, double longitude, double latitude, double radius, Metric unit) { Circle circle = new Circle(longitude, latitude, new Distance(radius, unit)); GeoRadiusCommandArgs args = GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending(); return redisTemplate.opsForGeo().radius(key, circle, args); } } ``` 上述代码中,我们使用了RedisTemplate来进行Redis操作。RedisTemplate是Spring Data Redis提供的核心组件,用于执行Redis命令。 在RedisGeoService中,我们定义了四个方法来进行Geo操作: - add方法:添加地理位置信息。 - distance方法:获取两个地理位置的距离。 - position方法:获取指定成员的地理位置。 - nearBy方法:获取指定地理位置附近的成员。 使用Spring Boot和Spring Data Redis进行Geo操作非常方便,只需要定义一个RedisGeoService,并注入RedisTemplate即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值