TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
redisclient.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <hiredis/hiredis.h>
11 
12 #include <string_view>
13 #include <vector>
14 
15 namespace cpid {
16 
17 class RedisReply;
18 
19 /**
20  * Simple, synchronous C++ wrapper for the Hiredis Redis client.
21  *
22  * Functionality is provided to:
23  * - format commands according to the Redis protocol
24  * - send commands and retrieve replies (a blocking operation)
25  * - pipelining of commands
26  *
27  * Whenever an error is encountered, functions will throw runtime_errors.
28  * Note that this class is *not* thread-safe.
29  */
30 class RedisClient {
31  public:
33  std::string_view host,
34  int port = 6379,
35  std::string_view name = std::string_view());
36  ~RedisClient();
37 
38  std::string_view host() const;
39  int port() const;
40  bool isConnected() const;
41  void reconnect() const;
42 
43  std::string format(char const* fmt, ...);
44  std::string format(std::initializer_list<std::string_view> args);
45  std::string format(std::vector<std::string_view> const& args);
46  RedisReply command(char const* fmt, ...);
47  RedisReply command(std::initializer_list<std::string_view> args);
48  RedisReply command(std::vector<std::string_view> const& args);
49  RedisReply command(std::string_view cmd);
50  std::vector<RedisReply> commands(std::vector<std::string> const& cmds);
52 
53  // Convenience wrappers
54  bool ping();
55  RedisReply set(std::string_view key, std::string_view value);
56  RedisReply get(std::string_view key);
57 
58  redisContext* ctx();
59 
60  private:
61  redisContext* redis_;
62 };
63 
64 /**
65  * Wrapper class for redisReply from Hiredis.
66  *
67  * This class provides a typed interface to replies from a Redis server.
68  * Replies can be nested, so there's some basic functionality to access data in
69  * array replies (size()/at()/begin()/end()). Nested replies can only be
70  * accessed by reference; if the top-level reply is destructed the nested
71  * replies will be destructed as well.
72  *
73  * If the type of the underlying reply is not the type expected by a function
74  * (e.g. calling string() on a reply holding an integer), you'll get
75  * runtime_error exceptions.
76  */
77 class RedisReply {
78  public:
79  using Iterator = std::vector<RedisReply>::iterator;
80 
81  RedisReply() = default;
82  RedisReply(RedisReply const&) = delete;
83  RedisReply(RedisReply&& other);
84  ~RedisReply();
85  RedisReply& operator=(RedisReply const&) = delete;
86  RedisReply& operator=(RedisReply&& other);
87 
88  bool isString() const;
89  bool isArray() const;
90  bool isInteger() const;
91  bool isNil() const;
92  bool isStatus() const;
93  bool isError() const;
94 
95  std::string string() const;
96  std::string_view stringv() const;
97  std::vector<std::string_view> stringvs() const;
98  int64_t integer() const;
99  std::string status() const;
100  std::string_view statusv() const;
101  std::string error() const;
102  bool ok() const;
103 
104  size_t size() const;
105  RedisReply& at(size_t index);
106  Iterator begin();
107  Iterator end();
108 
109  private:
110  RedisReply(void* reply, bool own = true);
111 
112  void ensureType(int type) const;
113 
114  redisReply* reply_ = nullptr;
115  bool owns_ = true;
116  std::vector<RedisReply> elements_;
117  friend class RedisClient;
118 };
119 
120 } // namespace cpid
~RedisClient()
Definition: redisclient.cpp:69
int port() const
Definition: redisclient.cpp:79
redisContext * ctx()
Definition: redisclient.cpp:203
std::vector< RedisReply >::iterator Iterator
Definition: redisclient.h:79
std::string_view host() const
Definition: redisclient.cpp:75
RedisReply getReply()
Definition: redisclient.cpp:180
bool isConnected() const
Definition: redisclient.cpp:83
void reconnect() const
Definition: redisclient.cpp:92
Wrapper class for redisReply from Hiredis.
Definition: redisclient.h:77
RedisClient(std::string_view host, int port=6379, std::string_view name=std::string_view())
Definition: redisclient.cpp:46
Simple, synchronous C++ wrapper for the Hiredis Redis client.
Definition: redisclient.h:30
The TorchCraftAI training library.
Definition: batcher.cpp:15
std::string format(char const *fmt,...)
Definition: redisclient.cpp:99
RedisReply command(char const *fmt,...)
Definition: redisclient.cpp:127
Definition: basetypes.h:349
bool ping()
Definition: redisclient.cpp:189
std::vector< RedisReply > commands(std::vector< std::string > const &cmds)
Sends a list of formatted commands in a single request.
Definition: redisclient.cpp:164