springboot2.x实现redisGeo位置信息

@Test
public void boundGeoOperationsTest() {
    BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps("CHINA:CITY");
    //南京市 118.803805,32.060168
    Point nanjing = new Point(118.803805,32.060168);
    //当redis里面已经存在了相同的member的时候,则仅仅更新经纬度
    //geoadd:增加某个地理位置的坐标
    System.out.println(boundGeoOperations.geoAdd(nanjing, "南京市"));
    Point beijing = new Point(116.397039,39.9077);
    System.out.println(boundGeoOperations.geoAdd(beijing, "北京市"));
    //geodist:获取两个地理位置的距离
    Distance distance = boundGeoOperations.geoDist("南京市", "北京市", Metrics.KILOMETERS);
    System.out.println("南京市到北京市之间的距离是:" + distance.getValue() + "km");
    //geohash:获取某个地理位置的geohash值
    List<String> list = boundGeoOperations.geoHash("南京市");
    System.out.println("南京市的geoHash = " + list.get(0));
    //geopos:获取某个地理位置的坐标
    List<Point> pointList = boundGeoOperations.geoPos("南京市");
    System.out.println("南京市的经纬度为 = " + pointList.get(0));
    //georadius:根据给定地理位置坐标获取指定范围内的地理位置集合
    //查询南京市1000KM范围内的城市
    Circle within = new Circle(nanjing,1000000);
    //设置geo查询参数
    RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
    //查询返回结果包括距离和坐标
    geoRadiusArgs = geoRadiusArgs.includeCoordinates().includeDistance();
    //按查询出的坐标距离中心坐标的距离进行排序
    geoRadiusArgs.sortAscending();
    //限制查询返回的数量
    geoRadiusArgs.limit(2);
    GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = boundGeoOperations.geoRadius(within,geoRadiusArgs);
    List<GeoResult<RedisGeoCommands.GeoLocation<String>>> geoResultList = geoResults.getContent();
    for (GeoResult geoResult : geoResultList) {
        System.out.println("geoRadius  " + geoResult.getContent());
    }
    //georadiusbymember:根据给定地理位置获取指定范围内的地理位置集合
    geoRadiusArgs.limit(1);
    geoResults = boundGeoOperations.geoRadiusByMember("南京市",new Distance(1000000), geoRadiusArgs);
    geoResultList = geoResults.getContent();
    for (GeoResult geoResult : geoResultList) {
        System.out.println("geoRadiusByMember  " + geoResult.getContent());
    }
    //删除位置信息,此命令不是geo提供的,是使用zrem命令删除的
    System.out.println(boundGeoOperations.geoRemove("南京市"));
}
 
输出结果:
1
1
南京市到北京市之间的距离是:899.2222km
南京市的geoHash = wtsqrkqk120
南京市的经纬度为 = Point [x=118.803805, y=32.060168]
geoRadius  RedisGeoCommands.GeoLocation(name=南京市, point=Point [x=118.803805, y=32.060168])
geoRadius  RedisGeoCommands.GeoLocation(name=北京市, point=Point [x=116.397038, y=39.907701])
geoRadiusByMember  RedisGeoCommands.GeoLocation(name=南京市, point=Point [x=118.803805, y=32.060168])
1
        </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Spring Boot 2.x搭建多级缓存案例,结合Caffeine和Redis,可以提供更高效的缓存机制。下面是一个简单的示例: 1. 首先,确保在项目的pom.xml文件中添加以下依赖: ```xml <!-- Caffeine --> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.9.0</version> </dependency> <!-- Spring Boot Data Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties文件中配置Redis连接信息: ```properties spring.redis.host=your_redis_host spring.redis.port=your_redis_port ``` 3. 创建一个缓存配置类,例如CacheConfig: ```java import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; @Configuration @EnableCaching public class CacheConfig extends CachingConfigurerSupport { @Bean @Override public CacheManager cacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager("myCache"); cacheManager.setCaffeine(caffeineCacheBuilder()); return cacheManager; } Caffeine<Object, Object> caffeineCacheBuilder() { return Caffeine.newBuilder() .initialCapacity(100) .maximumSize(1000) .expireAfterWrite(10, TimeUnit.MINUTES) .recordStats(); } } ``` 4. 创建一个Service类,例如DemoService,使用@Cacheable注解进行缓存: ```java import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class DemoService { @Cacheable(cacheNames = "myCache") public String getData(String key) { // 从数据库或其他数据源获取数据 return "Data for key: " + key; } } ``` 5. 在Controller中使用DemoService类: ```java import org.springframework.beans.factory.annotation.Autowired;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值