userver: userver/storages/redis/request_evalsha.hpp Source File
Loading...
Searching...
No Matches
request_evalsha.hpp
1#pragma once
2
3#include <userver/storages/redis/parse_reply.hpp>
4#include <userver/storages/redis/reply.hpp>
5#include <userver/storages/redis/request.hpp>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace storages::redis {
10
11template <typename ScriptResult, typename ReplyType = ScriptResult>
12class [[nodiscard]] RequestEvalSha final {
13public:
14 class EvalShaResult final {
15 public:
16 bool IsNoScriptError() const { return no_script_error_; }
17 bool HasValue() const { return reply_.has_value(); }
18 const ReplyType& Get() const { return *reply_; }
19 ReplyType Extract() { return std::move(*reply_); }
20
21 private:
22 friend class RequestEvalSha;
23 EvalShaResult() = default;
24 explicit EvalShaResult(bool no_script) : no_script_error_{no_script} {}
25 EvalShaResult(ReplyType&& reply) : reply_(std::move(reply)) {}
26 std::optional<ReplyType> reply_;
27 bool no_script_error_ = false;
28 };
29
30 explicit RequestEvalSha(RequestEvalShaCommon&& request) : request_(std::move(request)) {}
31
32 void Wait() { request_.Wait(); }
33
34 void IgnoreResult() const { request_.IgnoreResult(); }
35
36 EvalShaResult Get(const std::string& request_description = {}) {
37 auto reply_ptr = request_.GetRaw();
38 const auto& reply_data = reply_ptr->data;
39 if (reply_data.IsError()) {
40 const auto& msg = reply_data.GetError();
41 if (msg.find("NOSCRIPT", 0) == 0) {
42 return EvalShaResult(true);
43 }
44 }
45 /// no error try treat as usual eval
46 return ParseReply<ScriptResult, ReplyType>(std::move(reply_ptr), request_description);
47 }
48
49private:
50 RequestEvalShaCommon request_;
51};
52
53} // namespace storages::redis
54
55USERVER_NAMESPACE_END