springdata redis实现的简单demo

转自:http://www.zuidaima.com/share/1550463673076736.htm

麻雀虽小,五脏俱全,无止境的索取永远不能成为高手的。 运行test下的ShopDaoTest的main函数得到如下输出:

核心是key的设计,要考虑到以后的扩展。

列表类型

key:model_name list_name page count

如用户的shop列表:user1_shop2_1_10

单个对象类型的

key:model_name

如某个shop对象:shop1

好的,以下是在 Ubuntu 上使用 Spring Boot 和 Redis 执行 Geo 相关操作的示例: 1. 安装 Redis 和 Java 在 Ubuntu 上安装 Redis 和 Java,可以使用以下命令: ``` sudo apt-get update sudo apt-get install redis-server sudo apt-get install default-jre ``` 2. 创建 Spring Boot 项目 使用以下命令创建一个 Spring Boot 项目: ``` spring init --dependencies=web,data-redis myproject ``` 这将创建一个名为 `myproject` 的 Spring Boot 项目,并添加 Redis 和 Web 依赖项。 3. 添加 Redis 配置 在 `src/main/resources` 目录下创建名为 `application.properties` 的文件,并添加以下 Redis 配置: ``` spring.redis.host=localhost spring.redis.port=6379 ``` 这将告诉 Spring Boot 使用本地 Redis 数据库。 4. 编写控制器 在 `src/main/java` 目录下创建一个名为 `MyController` 的控制器,并添加以下代码: ```java import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.geo.GeoLocation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final RedisTemplate<String, String> redisTemplate; public MyController(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @GetMapping("/addGeo") public void addGeo() { redisTemplate.opsForGeo().add("mylocations", new GeoLocation<>("New York", new RedisGeoCommands.GeoLocation<>(-74.005974, 40.712776))); redisTemplate.opsForGeo().add("mylocations", new GeoLocation<>("Paris", new RedisGeoCommands.GeoLocation<>(2.352222, 48.856614))); } } ``` 控制器中的 `addGeo` 方法将使用 RedisTemplate 实例将两个地理位置添加到名为 `mylocations` 的 Redis Geo 集合中。 5. 运行应用程序 在终端中导航到项目目录并运行以下命令: ``` ./mvnw spring-boot:run ``` 这将启动应用程序并将其绑定到默认端口(8080)。 6. 测试应用程序 使用 curl 或浏览器访问 `http://localhost:8080/addGeo`,将两个地理位置添加到 Redis Geo 集合中。 可以使用以下命令在 Redis 命令行中查看集合的成员: ``` 127.0.0.1:6379> GEORADIUS mylocations 15 37.618423 55.751244 km ``` 这将返回距离(37.618423,55.751244)坐标 15 公里内的成员列表。 这就是在 Ubuntu 上使用 Spring Boot 和 Redis 执行 Geo 相关操作的简单示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值