springboot 整合redis geo 实现查找附近信息功能

private final StringRedisTemplate redisTemplate;

/**
 * 向指定key中添加经纬度信息
 *
 * @param key       存储的键值
 * @param longitude 经度
 * @param latitude  纬度
 * @param member    成员名称(数据的标识 比如userID)
 * @return 添加成功的条数
 */
public long geoAdd(String key, Double longitude, Double latitude, String member) {
	return redisTemplate.opsForGeo().add(key, new Point(longitude, latitude), member);
}

/**
 * 向指定key中添加经纬度信息
 *
 * @param key
 * @param locations
 * @return
 */
public long geoAddList(String key, List<RedisGeoCommands.GeoLocation<String>> locations) {
	return redisTemplate.opsForGeo().add(key, locations);
}

/**
 * 获取成员之间的距离
 *
 * @param key     存储的键值
 * @param member1 成员名称
 * @param member2 成员名称
 * @return
 */
public Distance geoDist(String key, String member1, String member2) {
	return redisTemplate.opsForGeo().distance(key, member1, member2);
}

/**
 * 获取指定key值的所有成员列表
 *
 * @param key
 * @return
 */
public List<String> geoHash(String key) {
	return redisTemplate.opsForGeo().hash(key);
}

/**
 * 获取指定key值的所有成员列表
 *
 * @param key
 * @return
 */
public List<String> geoHashList(String key, String[] serviceStations) {
	GeoOperations<String, String> ops = redisTemplate.opsForGeo();
	return ops.hash(key, serviceStations);
}

/**
 * 获取指定成员的位置信息
 *
 * @param key    存储的键值
 * @param member 成员名称
 * @return
 */
public Point geoPos(String key, String member) {
	return (redisTemplate.opsForGeo().position(key, member)).get(0);
}

/**
 * 获取指定成员 范围内的位置信息
 *
 * @param key      存储的键值
 * @param member   成员名称
 * @param distance 距离
 * @return
 */
public GeoResults<RedisGeoCommands.GeoLocation<String>> geoRadius(String key, String member, double distance) {
	RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates()
		.sortAscending();
	return redisTemplate.opsForGeo().radius(key, member, new Distance(distance, Metrics.KILOMETERS), args);
}


/**
 * 根据给定地理位置坐标获取指定范围内的地理位置集合
 *
 * @param key      存储的键值
 * @param lng      经度
 * @param lat      纬度
 * @param distance 距离(公里)
 * @return {@link RedisGeoCommands.GeoLocation}
 */
public GeoResults<RedisGeoCommands.GeoLocation<String>> getPointRadius(String key, Double lng, Double lat, int distance) {
	Circle within = new Circle(new Point(lng, lat), new Distance(distance, Metrics.KILOMETERS));
	RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
		.includeDistance() //包含距离
		.includeCoordinates() //包含经纬度
		.sortAscending()//正序排序
		.limit(50); //条数
	return redisTemplate.opsForGeo().radius(key, within, args);
}


/**
 * 批量删除
 *
 * @param key  存储的键值
 * @param name 成员名称
 * @return
 */
public Long deleteBatchRedisGeo(String key, String[] name) {
	return redisTemplate.boundZSetOps(key).remove(name);
}

/**
 * 单个删除
 *
 * @param key  存储的键值
 * @param name 成员名称
 * @return
 */
public Long deleteRedisGeo(String key, String name) {
	return redisTemplate.boundZSetOps(key).remove(name);
}

/**

  • GEO key 常量
    */
    public class RedisGeo {

    /**

    • communicode+租户号
      */
      public static String redisKeyCommunityCode = “REDIS_GEO_COMMUNITY_CODE:”;

    /**

    • communicode+柜子id
      */
      public static String redisGeoCabinetCommunity = “REDIS_GEO_CABINET_COMMUNITY:”;
      }

@Data
public class QueryNearDTO {

/**
 * 距离 (公里)
 */
@ApiModelProperty(value = "距离 (公里)", required = true)
@NotNull
private Integer distance;
/**
 * 租户id
 */
@ApiModelProperty(value = "租户id", hidden = true)
private String tenantId;

/**
 * 经度
 */
@ApiModelProperty(value = "经度", required = true)
@NotNull
private Double lng;

/**
 * 纬度
 */
@ApiModelProperty(value = "纬度", required = true)
@NotNull
private Double lat;

/**
 * 获取条数
 */
@ApiModelProperty(value = "获取条数")
private Integer limit;

}

public class TestController {

private final RedisGeoUtil redisGeoUtil;



@PostMapping("a_geo")
@ApiOperation(value = "批量添加附近坐标", notes = "添加附近坐标")
public R addGeo() {
	List<RedisGeoCommands.GeoLocation<String>> list = new ArrayList<>();
	double[] points = {113.674755, 34.743285,
		113.665556, 34.753013,
		113.731959, 34.74376,
		113.662538, 34.720501,
		113.668287, 34.768197,
		113.668287, 34.768197,
		113.671881, 34.749158,
		113.671881, 34.749158, 113.668287, 34.768197,
		113.668287, 34.768197,
		113.671881, 34.749158,
		113.671881, 34.749158};
	for (int i = 0; i < 20; i += 2) {
		RedisGeoCommands.GeoLocation location = new RedisGeoCommands.GeoLocation("name_" + i / 2, new Point(points[i], points[i + 1]));
		list.add(location);
	}
	long count = redisGeoUtil.geoAddList(RedisGeo.redisKeyCommunityCode + "123456", list);
	return R.data(count);
}



@GetMapping("q_geo_add")
@ApiOperation(value = "单个新增坐标")
public R<Long> q_geo_add() {
	return R.data(redisGeoUtil.geoAdd( RedisGeo.redisKeyCommunityCode + "000000",  new Double(113.543099),  new Double(34.743285),  "666666"));
}


@GetMapping("q_geo_if")
@ApiOperation(value = "查询某点附近距离信息")
public R getGeo1(String member, double distance) {
	GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisGeoUtil.geoRadius(RedisGeo.redisKeyCommunityCode + "000000", member, distance);
	return R.data(geoResults);
}

@GetMapping("q_geo_local")
@ApiOperation(value = "查询坐标范围内数据", notes = "查询范围内数据")
public R<GeoResults<RedisGeoCommands.GeoLocation<String>>> q_geo_local(QueryNearDTO queryNearDTO) {
	return R.data(redisGeoUtil.getPointRadius(RedisGeo.redisKeyCommunityCode + "000000", queryNearDTO.getLng(), queryNearDTO.getLat(), queryNearDTO.getDistance(),1));
}



@GetMapping("q_geo_remove")
@ApiOperation(value = "删除坐标信息", notes = "删除坐标信息")
public R q_geo_remove(String member) {
	String[] aa=Arrays.asList("中石化", "666666", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
	return R.data(redisGeoUtil.deleteBatchRedisGeo(RedisGeo.redisKeyCommunityCode + "000000", aa));
}


/**
 * 测试 获取某个地理位置的 geohash 值
 * */
@GetMapping("q_hash_list")
@ApiOperation(value = "获取地理位置的 geohash 值")
public R<List<String>> testGetServiceStationGeoHash() {
	String[] aa=Arrays.asList("中石化", "中石油", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
	List<String> hashList=	redisGeoUtil.geoHashList(RedisGeo.redisKeyCommunityCode + "000000",aa);
	return R.data(hashList);
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我来回答你的问题。在SpringBoot中,实现点赞功能通常会使用Redis进行缓存,可以将点赞数存储到Redis中,每当一个用户进行点赞操作时,就会将点赞数从Redis中读取,进行加1操作,再将结果存储回Redis中。具体实现步骤如下: 1. 首先,在pom.xml文件中添加Redis的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties文件中配置Redis连接信息: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 3. 创建一个Redis工具类,用于封装一些Redis操作: ``` @Component public class RedisUtil { @Autowired private StringRedisTemplate redisTemplate; public void increment(String key) { redisTemplate.opsForValue().increment(key); } public String get(String key) { return redisTemplate.opsForValue().get(key); } public void set(String key, String value) { redisTemplate.opsForValue().set(key, value); } } ``` 4. 在点赞接口中,调用RedisUtil中封装的方法,完成点赞功能实现: ``` @RestController public class LikeController { @Autowired private RedisUtil redisUtil; @PostMapping("/like") public void like(@RequestParam("id") Integer id) { String key = "like:" + id; redisUtil.increment(key); } @GetMapping("/like") public String getLike(@RequestParam("id") Integer id) { String key = "like:" + id; return redisUtil.get(key); } } ``` 以上就是SpringBoot整合Redis实现点赞功能的具体步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值