zoukankan      html  css  js  c++  java
  • Redis_05_项目记录附近人搜索和热点数据

    1.实现效果,获得用户登录经纬度地址,找出用户周围的小区。

    实现思路:使用Redis的geospatial类型存储小区地址,通过georadius 方法实现附近人搜索

    2.代码实现:

    controller

    /**
         *
         * @Description:获取当前位置50km范围内的坐标
         * @Author: hdh
         * @Date: 2021/1/12
         * @param mapX: 经度
         * @param mapY: 维度
         * @return: org.springframework.http.ResponseEntity<java.lang.String>
         **/
        @GetMapping("getNearByXY/{mapX}/{mapY}")
        public ResponseEntity<String> getNearByXY(@PathVariable("mapX") Double mapX, @PathVariable("mapY") Double mapY) {
    
            ResponseEntity<String> res = scommunityService.getNearByXY(mapX, mapY);
    
            return res;
        }

     service

    /**
         * @param mapX: 经度
         * @param mapY: 维度
         * @Description:
         * @Author: hdh
         * @Date: 2021/1/12
         * @return: org.springframework.http.ResponseEntity<java.lang.String>
         **/
        @Override
        public ResponseEntity<String> getNearByXY(Double mapX, Double mapY) {
    
            //获取当前位置50km范围内的坐标
            Circle circle = new Circle(mapX, mapY, 50000);
            RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                    .includeDistance().includeCoordinates().sortAscending();
    
            GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo()
                    .radius(CommonStatic.CITY_NJ, circle, args);
    
            return ResultVo.createResponseEntity(ResultVo.SUCCESS, "查选成功", results);
        }

    3.热点数据实现:用户点击的小区次数越多,在选择时该小区越靠上面

    实现思路:使用Redis的Zset类型存储小区,每次选择该小区的时候score自增1

    代码实现:

     /**
         * @Description:获取所有的车品牌
         * @Author: hdh
         * @Date: 2021/1/13
         * @return: org.springframework.http.ResponseEntity<java.lang.String>
         **/
        @Override
        public ResponseEntity<String> getAllEsDict() {
    
            //判断redis中是否存在
            Boolean isExists = redisUtil.hasKey(CommonStatic.ES_DICT_NAME);
            if (isExists) {
                //redis中存在热点数据
                Set<Object> esDicts = redisUtil.zReverseRangeByScore(CommonStatic.ES_DICT_NAME, Integer.MIN_VALUE, Integer.MAX_VALUE);
                return ResultVo.createResponseEntity(ResultVo.SUCCESS, "查选成功", esDicts);
            }
            //redis中不存在的热点数据
            List<EsDict> esDicts = this.esDictMapper.getAllEsDict();
            esDicts.forEach(esDict -> {
                redisUtil.zAdd(CommonStatic.ES_DICT_NAME, esDict.getName(), 0);
            });
    
            return ResultVo.createResponseEntity(ResultVo.SUCCESS, "查选成功", esDicts);
    
        }

    4.定时任务:更新Redis中的数据(保证redis和数据库数据一致)

    /**
         * @Description:更新地址CITY_NJ = "city:NJ";和ES_DICT_NAME = "esDictName";  每小时触发
         * @Author: hdh
         * @Date: 2021/1/13
         * @return: void
         **/
        @Scheduled(cron = "0 0 * * * ?")
        private void updateRedis() {
            QueryWrapper<Scommunity> queryWrapperScommunities = new QueryWrapper<>();
            List<Scommunity> scommunities = scommunityMapper.selectList(queryWrapperScommunities);
            //强制更新redis  redis地址
            scommunities.forEach(scommunity -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("communityId", scommunity.getCommunityId());
                jsonObject.put("name", scommunity.getName());
                String scommunityStr = jsonObject.toJSONString();
                redisGeoUtils.geoAdd(CommonStatic.CITY_NJ, new Point(Double.valueOf(scommunity.getMapX()), Double.valueOf(scommunity.getMapY())), scommunityStr);
            });
    
            //强制更新redis  redis车品牌
            QueryWrapper<EsDict> queryWrapperEsDict = new QueryWrapper<>();
            List<EsDict> esDicts = esDictMapper.selectList(queryWrapperEsDict);
            esDicts.forEach(esDict -> {
                redisUtil.zAdd(CommonStatic.ES_DICT_NAME, esDict.getName(), 0);
            });
            log.info("updateRedis定时任务更新成功时间:{}", LocalDateTime.now());
        }

    工具类见work_35_javautils,总会用到的

  • 相关阅读:
    USACO 3.3 A Game
    USACO 3.3 Camelot
    USACO 3.3 Shopping Offers
    USACO 3.3 TEXT Eulerian Tour中的Cows on Parade一点理解
    USACO 3.3 Riding the Fences
    USACO 3.2 Magic Squares
    USACO 3.2 Stringsobits
    USACO 3.2 Factorials
    USACO 3.2 Contact
    USACO 3.1 Humble Numbers
  • 原文地址:https://www.cnblogs.com/asndxj/p/14273422.html
Copyright © 2011-2022 走看看