java实现lbs_在Java中利用redis实现LBS服务

package com.x9710.common.redis.test;

import com.x9710.common.redis.RedisConnection;

import com.x9710.common.redis.domain.GeoCoordinate;

import com.x9710.common.redis.domain.Postion;

import com.x9710.common.redis.impl.CacheServiceRedisImpl;

import com.x9710.common.redis.impl.LBSServiceRedisImpl;

import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import java.util.List;

/**

* LBS服务测试类

*

* @author 杨高超

* @since 2017-12-28

*/

public class RedisLBSTest {

private CacheServiceRedisImpl cacheService;

private LBSServiceRedisImpl lbsServiceRedis;

private String type = "SHOP";

private GeoCoordinate center;

@Before

public void before() {

RedisConnection redisConnection = RedisConnectionUtil.create();

lbsServiceRedis = new LBSServiceRedisImpl();

lbsServiceRedis.setDbIndex(15);

lbsServiceRedis.setRedisConnection(redisConnection);

Postion postion = new Postion("2017122801", type, 91.118970, 29.654210);

lbsServiceRedis.addPostion(postion);

postion = new Postion("2017122802", type, 116.373472, 39.972528);

lbsServiceRedis.addPostion(postion);

postion = new Postion("2017122803", type, 116.344820, 39.948420);

lbsServiceRedis.addPostion(postion);

postion = new Postion("2017122804", type, 116.637920, 39.905460);

lbsServiceRedis.addPostion(postion);

postion = new Postion("2017122805", type, 118.514590, 37.448150);

lbsServiceRedis.addPostion(postion);

postion = new Postion("2017122806", type, 116.374766, 40.109508);

lbsServiceRedis.addPostion(postion);

center = new GeoCoordinate();

center.setLongitude(116.373472);

center.setLatitude(39.972528);

}

@Test

public void test10KMRadious() {

List postions = lbsServiceRedis.radious(type, center, 1000 * 10L, true);

Assert.assertTrue(postions.size() == 2 && exist(postions, "2017122802") && exist(postions, "2017122803"));

}

@Test

public void test50KMRadious() {

List postions = lbsServiceRedis.radious(type, center, 1000 * 50L, true);

Assert.assertTrue(postions.size() == 4

&& exist(postions, "2017122802")

&& exist(postions, "2017122803")

&& exist(postions, "2017122806")

&& exist(postions, "2017122804"));

}

private boolean exist(List postions, String key) {

if (postions != null) {

for (Postion postion : postions) {

if (postion.getId().equals(key)) {

return true;

}

}

}

return false;

}

@Before

public void after() {

RedisConnection redisConnection = RedisConnectionUtil.create();

cacheService = new CacheServiceRedisImpl();

cacheService.setDbIndex(15);

cacheService.setRedisConnection(redisConnection);

cacheService.delObject(type);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Redis可以非常方便地实现排行榜功能。以下是一个使用Java实现Redis排行榜的示例代码: ```java import redis.clients.jedis.Jedis; import redis.clients.jedis.Tuple; import java.util.Set; public class RedisRankList { private Jedis jedis; private static final String RANK_LIST_KEY = "rank_list"; public RedisRankList() { jedis = new Jedis("localhost"); } public void addScore(String member, double score) { jedis.zadd(RANK_LIST_KEY, score, member); } public void removeMember(String member) { jedis.zrem(RANK_LIST_KEY, member); } public void incrementScore(String member, double increment) { jedis.zincrby(RANK_LIST_KEY, increment, member); } public Set<Tuple> getRankList() { return jedis.zrevrangeWithScores(RANK_LIST_KEY, 0, -1); } public static void main(String[] args) { RedisRankList rankList = new RedisRankList(); rankList.addScore("张三", 90); rankList.addScore("李四", 80); rankList.incrementScore("李四", 10); rankList.addScore("王五", 70); Set<Tuple> topScores = rankList.getRankList(); for (Tuple tuple : topScores) { System.out.println(tuple.getElement() + " : " + tuple.getScore()); } } } ``` 在上述示例代码,我们使用了Jedis客户端连接到本地Redis服务器,并实现了添加成员分数、移除成员、增加成员分数、获取排行榜等功能。其,`zadd`方法用于添加成员分数,`zrem`方法用于移除成员,`zincrby`方法用于增加成员分数,`zrevrangeWithScores`方法用于获取排行榜。通过运行该示例代码,我们可以得到以下结果: ``` 张三 : 90.0 李四 : 90.0 王五 : 70.0 ``` 可以看到,排行榜按照成员分数从高到低排序,且李四的分数已经增加了10分。这样,我们就成功地使用Java实现Redis排行榜功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值