- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在这里尝试通过做一些例子来学习 Redis。我有一个名为 DriverLocation
的实体,其中有一个名为 updatedAt
的时间戳,这是一个以毫秒表示的纪元时间。关键是我想要一个 SortedSet,这样我就可以执行 zrangebyscore
查询,以获取按时间戳排序的最后 N 条记录。
sortedset 基本上会有timestamp --> hash_id
结构。如果我想获取最后 10 分钟添加的记录,那么我将执行 zrangebyscore
查询以按排序方式获取所有 hash_id。然后使用带有 hash_ids 的 hmget
获取所有哈希对象。
这是关于 redis-cli 的一个非常简单的工作演示,您可以假设我使用了 3 位数字而不是毫秒。
localhost:6379> zadd locations_0 213 hash_id_1
(integer) 1
localhost:6379> zadd locations_0 214 hash_id_2
(integer) 1
localhost:6379> zadd locations_0 215 hash_id_3
(integer) 1
localhost:6379> zrangebyscore locations_0 212 214
1) "hash_id_1"
2) "hash_id_2"
redis cli 上一切正常。但是,在 Spring 方面,我无法实现我的目标。
DriverLocation.java
@RedisHash("driverLocation")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DriverLocationEntity {
@Id
private Long id;
@Indexed
private Long driverId;
@GeoIndexed
private Point point;
private Date updatedAt;
}
RedisConfiguration.java
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private int redisPort;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisHost, redisPort);
}
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
@Bean
public RedisAtomicLong redisAtomicLong() {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong("DriverLocationIdCounter", redisConnectionFactory(), 0L);
return redisAtomicLong;
}
}
Controller.java
@RestController
@RequestMapping("/drivers")
@Slf4j
public class DriverLocationController {
@Autowired
private DriverLocationRepository driverLocationRepository;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Autowired
private RedisAtomicLong redisAtomicLong;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping("/{id}")
public ResponseEntity<List<DriverLocationEntity>> getDriver(@PathVariable("id") Long driverId) {
long now = Instant.now().getEpochSecond();
Set<Object> ids = redisTemplate.opsForZSet().rangeByScore(
"locations_" + driverId,
Instant.ofEpochSecond(now).minusSeconds(300).toEpochMilli(),
Instant.ofEpochSecond(now).toEpochMilli());
List<Object> driverLocations = redisTemplate.opsForHash().multiGet("driverLocations", ids.stream().map(id -> (Long) id).collect(Collectors.toList()));
return ResponseEntity.status(HttpStatus.OK).body(driverLocations.stream().map(dLoc -> (DriverLocationEntity) dLoc).collect(Collectors.toList()));
}
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public ResponseEntity<String> addDriverLocation(@RequestBody DriverLocationMessageEntity messageEntity,
@PathVariable("id") Long driverId) throws JsonProcessingException {
long now = Instant.now().toEpochMilli();
DriverLocationEntity driverLocationEntity = new DriverLocationEntity();
driverLocationEntity.setDriverId(driverId);
driverLocationEntity.setPoint(new Point(messageEntity.getLongitude(), messageEntity.getLatitude()));
driverLocationEntity.setUpdatedAt(new Date(now));
driverLocationEntity.setId(redisAtomicLong.getAndIncrement());
String strVal = objectMapper.writeValueAsString(driverLocationEntity);
// save driver location entity
driverLocationRepository.save(driverLocationEntity);
// save timestamp -> hash_id
redisTemplate.opsForZSet().add("locations_" + driverLocationEntity.getDriverId(), now, driverLocationEntity.getId());
return ResponseEntity.status(HttpStatus.OK).body("done");
}
}
感谢您的帮助。
最佳答案
我基本上是通过使用 Redisson 来管理的客户端及其 StringCodec
DriverLocationEntity driverLocationEntity = new DriverLocationEntity();
driverLocationEntity.setDriverId(messageEntity.getDriverId());
driverLocationEntity.setPoint(new Point(messageEntity.getLongitude(), messageEntity.getLatitude()));
driverLocationEntity.setUpdatedAt(new Date(now));
driverLocationEntity.setId(UUID.randomUUID().toString());
// add hash_id into geoset for distance calculations
redissonClient.getGeo(GEO_SET_KEY_NAME, new StringCodec()).add(messageEntity.getLongitude(), messageEntity.getLatitude(), HASH_ID_PREFIX + driverLocationEntity.getId());
关于java - 带有 Long 类型键的 spring data redis zadd 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918372/
我找到了long int long和 int long long可以编译为变量类型。 long int long有什么区别吗, int long long , long long和 long long
我无法找出为什么“加密”函数仍然将“消息”读取为字符串,尽管我已经使用不同的方法将数据类型更改为字节。 错误消息是“Prince 类型中的方法 Encrypt(long, long, long, lo
这个问题在这里已经有了答案: Is "long long" = "long long int" = "long int long" = "int long long"? (4 个答案) 关闭 6 年
我正在从 Java 过渡到 C++,并且对 long 数据类型有一些疑问。在 Java 中,要保存大于 232 的整数,您只需编写 long x;。但是,在 C++ 中,long 似乎既是数据类型又是
clang-tidy 12.0.1 报告了一个相当奇怪的警告。在以下代码中: #include int main() { std::vector v1; const auto a =
我创建了一个 pair 和 long long int 的映射 - map,long long int >; 和一个交互器 - map, long long int >::iterator it1;
我想知道 unsigned long long 和 unsigned long long int 的主要区别。它们可以互换使用吗? 对于像 9223372036854775807 这样的大十进制数的计
我看到的大多数代码都使用缩写类型来声明变量,例如 long long x; // long long int x short y; // short int y 我浏览了 C++11 标准(第 3.9
common_type::type是 unsigned long因为关于积分提升后的操作数,标准说... [...] if the operand that has unsigned integer
long long int A = 3289168178315264; long long int B = 1470960727228416; double D = sqrt(5); long lon
这些新数据类型的目的是什么?我通常只使用“int”或“long”,但为什么会存在这些呢?它们带来了什么新功能或用途? 最佳答案 long int一直是long的全称,只是很少用而已。 long lon
我正在运行以下for循环 for(unsigned long long int i = N-1; i >= 0; i--){ cin>>L[i]; } 当程序到达这个代码段时,它停止响应。但是
最近问了一个关于递归导致这个问题的问题 注意-> count() 函数返回键 K 在 map 容器中出现的次数。如果键存在于容器中,则返回 1,因为映射仅包含唯一键。如果 map 容器中不存在键,则返
好的,所以我正在尝试实现客户端 - 服务器程序(套接字编程)。 我的客户发送一个嵌入字符串中的 long long int,如下所示: char copy[10]; sprintf(send_data
如果我有任务 Long c = a + b; 有没有一种简单的方法来检查 a + b 不大于/小于 Long.MAX_VALUE/Long.MIN_VALUE? 最佳答案 使用 Guava , 就这么
我需要制作一个 Comparator 来根据它的 long 类型的变量之一对我的对象列表进行排序。 public class ParticipantIndexComparator implements
假设我有这两种类型: typedef unsigned long long uint64; typedef signed long long sint64; 我有这些变量: uint64 a = ..
long long 和 long 有什么区别?而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么? #include using namespace std; int
当结果将大于 C 中的 long long int 时,是否有可能对两个不同的 long long int 变量求和? 最佳答案 由于 OP 想要“在屏幕上打印结果”,因此将数字分成两部分:Most-
实际上我必须找到从源顶点到所有其他顶点的最短路径。为此,我获得了下面给出的代码模板。我想实现“Bellman–Ford algorithm”。 #include #include #include
我是一名优秀的程序员,十分优秀!