userver: userver/storages/redis/ttl_reply.hpp Source File
Loading...
Searching...
No Matches
ttl_reply.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/ttl_reply.hpp
4/// @brief Redis TTL reply value and exception for invalid TTL queries
5
6#include <chrono>
7#include <cstdint>
8#include <string>
9
10#include <userver/storages/redis/exception.hpp>
11#include <userver/storages/redis/fwd.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace storages::redis {
16
17/// @brief Parsed TTL / HTTL reply (seconds precision).
18///
19/// Also used for HTTL replies — in which case the value semantics mirror
20/// "field" instead of "key" (kKeyDoesNotExist=-2 means the field does not exist
21/// or the parent hash does not exist; kKeyHasNoExpiration=-1 means the field has
22/// no associated TTL).
23///
24/// For millisecond-precision commands (PTTL / HPTTL) use @c PttlReply.
25class TtlReply final {
26public:
27 enum class TtlReplyValue { kKeyDoesNotExist = -2, kKeyHasNoExpiration = -1 };
28
29 static constexpr TtlReplyValue kKeyDoesNotExist = TtlReplyValue::kKeyDoesNotExist;
30 static constexpr TtlReplyValue kKeyHasNoExpiration = TtlReplyValue::kKeyHasNoExpiration;
31
32 explicit TtlReply(int64_t value);
33 TtlReply(TtlReplyValue value);
34
35 static TtlReply Parse(ReplyData&& reply_data, const std::string& request_description = {});
36
37 bool KeyExists() const;
38 bool KeyHasExpiration() const;
39 [[deprecated("Use GetExpire() instead")]] size_t GetExpireSeconds() const { return GetExpire().count(); }
40
41 /// @brief Returns the expiration as `std::chrono::seconds`. Throws
42 /// `KeyHasNoExpirationException` if the key/field has no expiration.
43 std::chrono::seconds GetExpire() const;
44
45private:
46 int64_t value_;
47};
48
49/// @brief Thrown when reading TTL for a missing or persistent key
51public:
52 using Exception::Exception;
53};
54
55} // namespace storages::redis
56
57USERVER_NAMESPACE_END