Redis實(shí)現(xiàn)附近商鋪的項(xiàng)目實(shí)戰(zhàn)
一、GEO數(shù)據(jù)結(jié)構(gòu)
1、入門(mén)
GEO是Geolocation的縮寫(xiě),代表地理坐標(biāo)。redis3.2中加入對(duì)GEO的支持,允許存儲(chǔ)地理坐標(biāo)信息,幫助我們根據(jù)經(jīng)緯度來(lái)檢索數(shù)據(jù)。
常見(jiàn)命令:
- GEOADD:添加一個(gè)地理空間信息,包含:經(jīng)度(longitude)、緯度(latitude)、值(member)
- GEODIST:計(jì)算指定的兩個(gè)點(diǎn)之間的距離并返回
- GEOHASH:將指定 member 的坐標(biāo)轉(zhuǎn)為 hash 字符串形式并返回
- GEOPOS:返回指定 member 的坐標(biāo)
- GEORADIUS:指定圓心、半徑,找到該圓內(nèi)包含的所有 member,并按照與圓心之間的距離排序后返回。6.2 以后已廢棄
- GEOSEARCH:在指定范圍內(nèi)搜索 member,并按照與指定點(diǎn)之間的距離排序后返回。范圍可以是圓形或矩形。6.2 新功能
- GEOSEARCHSTORE:與 GEOSEARCH 功能一致,不過(guò)可以把結(jié)果存儲(chǔ)到一個(gè)指定的 key。6.2 新功能
2、練習(xí)
需求
1、添加下面幾條數(shù)據(jù):
- 北京南站(116.378248 39.865275)
- 北京站(116.42803 39.903738)
- 北京西站(116.322287 39.893729)
2、計(jì)算北京西站到北京站的距離
3、搜索天安門(mén)(116.397904 39.909005)附近 10km 內(nèi)的所有火車(chē)站,并按照距離升序排序

搜索10km內(nèi)有哪些商鋪(搜出來(lái)的會(huì)按照距離排序)和 返回北京站的坐標(biāo)

二、附加商戶(hù)搜索
1、先批量導(dǎo)入商戶(hù)坐標(biāo)
按照商戶(hù)類(lèi)型做分組,類(lèi)型相同的商戶(hù)作為同一組,以 typeId 作為 key 存入同一個(gè) GEO 集合中。

編寫(xiě)測(cè)試類(lèi)實(shí)現(xiàn)批量導(dǎo)入redis中
@SpringBootTest
class HmDianPingApplicationTests {
@Autowired
private ShopServiceImpl shopService;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void loadShopData(){
// 1、查詢(xún)店鋪信息
List<Shop> list = shopService.list();
// 2、把店鋪分組,按照 typeId 分組,typeId 一致的放到一個(gè)集合中
Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
// 3、分批完成寫(xiě)入 Redis
for (Map.Entry<Long, List<Shop>> longListEntry : map.entrySet()) {
Long typeId = longListEntry.getKey();
List<Shop> value = longListEntry.getValue();
List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());
for (Shop shop : value) {
locations.add(new RedisGeoCommands.GeoLocation<>(
shop.getId().toString(),
new Point(shop.getX(), shop.getY())
));
}
stringRedisTemplate.opsForGeo().add(RedisConstants.SHOP_GEO_KEY + typeId, locations);
}
}
}2、實(shí)現(xiàn)附近商戶(hù)功能
SpringDataRedis 的 2.3.9 版本并不支持 Redis6.2 提供的 GEOSEARCH 命令,因此我們要把他排除掉,引入我們自己的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-data-redis</artifactId>
<groupId>org.springframework.data</groupId>
</exclusion>
<exclusion>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.1.6.RELEASE</version>
</dependency>Controller
前端不一定會(huì)傳x坐標(biāo)和y坐標(biāo),可能是按照熱度等其他條件來(lái)查詢(xún),所以x和y要required = false,表示可以沒(méi)有
@RestController
@RequestMapping("/shop")
public class ShopController {
@Resource
public IShopService shopService;
/**
* 根據(jù)商鋪類(lèi)型分頁(yè)查詢(xún)商鋪信息
* @param typeId 商鋪類(lèi)型
* @param current 頁(yè)碼
* @return 商鋪列表
*/
@GetMapping("/of/type")
public Result queryShopByType(
@RequestParam("typeId") Integer typeId,
@RequestParam(value = "current", defaultValue = "1") Integer current,
@RequestParam(value = "x", required = false) Double x,
@RequestParam(value = "y", required = false) Double y
) {
return shopService.queryShopByType(typeId, current, x, y);
}
}Service
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
// 判斷是否需要根據(jù)坐標(biāo)查詢(xún)
if(x == null || y == null){
// 根據(jù)類(lèi)型分頁(yè)查詢(xún)
Page<Shop> page = query()
.eq("type_id", typeId)
.page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
// 返回?cái)?shù)據(jù)
return Result.ok(page.getRecords());
}
// 計(jì)算分頁(yè)參數(shù)
int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;
int end = current * SystemConstants.DEFAULT_PAGE_SIZE;
// 查詢(xún) Redis,按照距離排序、分頁(yè)。
GeoResults<RedisGeoCommands.GeoLocation<String>> search = stringRedisTemplate.opsForGeo().
search(RedisConstants.SHOP_GEO_KEY + typeId,
GeoReference.fromCoordinate(x, y),
new Distance(5000),
RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));
if(search == null){
return Result.ok(Collections.emptyList());
}
// 查詢(xún) Redis,按照距離排序、分頁(yè)
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = search.getContent();
if(from >= content.size()){
return Result.ok(Collections.emptyList());
}
List<Long> ids = new ArrayList<>(content.size());
Map<String, Distance> distanceMap = new HashMap<>(content.size());
// 截取 from ~ end 的部分
content.stream().skip(from).forEach(result -> {
// 獲取店鋪 id
String shopIdStr = result.getContent().getName();
ids.add(Long.valueOf(shopIdStr));
// 獲取距離
Distance distance = result.getDistance();
distanceMap.put(shopIdStr, distance);
});
String join = StrUtil.join(",", ids);
// 根據(jù) id 查詢(xún) shop
List<Shop> shopList = query().in("id", ids).last("order by field(" + join + ")").list();
for (Shop shop : shopList) {
shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());
}
return Result.ok(shopList);
}
}到此這篇關(guān)于Redis實(shí)現(xiàn)附近商鋪的項(xiàng)目實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)Redis 附近商鋪內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis數(shù)據(jù)結(jié)構(gòu)類(lèi)型示例解析
這篇文章主要為大家介紹了Redis數(shù)據(jù)結(jié)構(gòu)類(lèi)型示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Redis入門(mén)教程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Redis是一款開(kāi)源的、高性能的鍵-值存儲(chǔ)(key-value store)。下面通過(guò)本文大家分享Redis入門(mén)教程,感興趣的朋友參考下吧2017-08-08
淺析對(duì)redis?hashtable?的sizemask理解
在?Redis?的哈希表實(shí)現(xiàn)中,index?=?hash?&?dict->ht[0].sizemask?是計(jì)算鍵值對(duì)應(yīng)存儲(chǔ)位置的核心操作,本文給大家介紹redis?hashtable?的sizemask理解,感興趣的朋友一起看看吧2025-03-03
Redis并發(fā)訪問(wèn)問(wèn)題詳細(xì)講解
本文主要介紹了Redis如何應(yīng)對(duì)并發(fā)訪問(wèn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-12-12
WINDOWS中REDIS主從配置實(shí)現(xiàn)代碼解析
這篇文章主要介紹了WINDOWS中REDIS主從配置實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Redis動(dòng)態(tài)熱點(diǎn)數(shù)據(jù)緩存策略設(shè)計(jì)
本文主要介紹了Redis動(dòng)態(tài)熱點(diǎn)數(shù)據(jù)緩存策略設(shè)計(jì),包括熱點(diǎn)數(shù)據(jù)識(shí)別、動(dòng)態(tài)緩存、多級(jí)緩存、預(yù)加載機(jī)制、更新策略以及監(jiān)控告警等,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01

