Line data Source code
1 : #ifndef SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_REDIS_H_
2 : #define SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_REDIS_H_
3 :
4 : #include <sw/redis++/redis++.h>
5 : #include <chrono>
6 :
7 : using namespace sw::redis;
8 : namespace social_network {
9 :
10 1 : Redis init_redis_client_pool(
11 : const json &config_json,
12 : const std::string &service_name
13 : ) {
14 2 : ConnectionOptions connection_options;
15 1 : connection_options.host = config_json[service_name + "-redis"]["addr"];
16 1 : connection_options.port = config_json[service_name + "-redis"]["port"];
17 :
18 1 : if (config_json["ssl"]["enabled"]) {
19 0 : std::string ca_file = config_json["ssl"]["caPath"];
20 :
21 0 : connection_options.tls.enabled = true;
22 0 : connection_options.tls.cacert = ca_file.c_str();
23 : }
24 :
25 1 : ConnectionPoolOptions pool_options;
26 1 : pool_options.size = config_json[service_name + "-redis"]["connections"];
27 1 : pool_options.wait_timeout = std::chrono::milliseconds(config_json[service_name + "-redis"]["timeout_ms"]);
28 1 : pool_options.connection_lifetime = std::chrono::milliseconds(config_json[service_name + "-redis"]["keepalive_ms"]);
29 :
30 2 : return Redis(connection_options, pool_options);
31 : }
32 :
33 0 : RedisCluster init_redis_cluster_client_pool(
34 : const json &config_json,
35 : const std::string &service_name
36 : ) {
37 0 : ConnectionOptions connection_options;
38 0 : connection_options.host = config_json[service_name + "-redis"]["addr"];
39 0 : connection_options.port = config_json[service_name + "-redis"]["port"];
40 :
41 0 : if (config_json["ssl"]["enabled"]) {
42 0 : std::string ca_file = config_json["ssl"]["caPath"];
43 :
44 0 : connection_options.tls.enabled = true;
45 0 : connection_options.tls.cacert = ca_file.c_str();
46 : }
47 :
48 0 : ConnectionPoolOptions pool_options;
49 0 : pool_options.size = config_json[service_name + "-redis"]["connections"];
50 0 : pool_options.wait_timeout = std::chrono::milliseconds(config_json[service_name + "-redis"]["timeout_ms"]);
51 0 : pool_options.connection_lifetime = std::chrono::milliseconds(config_json[service_name + "-redis"]["keepalive_ms"]);
52 :
53 0 : return RedisCluster(connection_options, pool_options);
54 : }
55 :
56 0 : Redis init_redis_replica_client_pool(
57 : const json& config_json,
58 : const std::string& service_name
59 : ) {
60 0 : ConnectionOptions connection_options;
61 0 : connection_options.host = config_json[service_name]["addr"];
62 0 : connection_options.port = config_json[service_name]["port"];
63 :
64 0 : if (config_json["ssl"]["enabled"]) {
65 0 : std::string ca_file = config_json["ssl"]["caPath"];
66 :
67 0 : connection_options.tls.enabled = true;
68 0 : connection_options.tls.cacert = ca_file.c_str();
69 : }
70 :
71 0 : ConnectionPoolOptions pool_options;
72 0 : pool_options.size = config_json[service_name]["connections"];
73 0 : pool_options.wait_timeout = std::chrono::milliseconds(config_json[service_name]["timeout_ms"]);
74 0 : pool_options.connection_lifetime = std::chrono::milliseconds(config_json[service_name]["keepalive_ms"]);
75 :
76 0 : return Redis(connection_options, pool_options);
77 : }
78 :
79 :
80 : } // namespace social_network
81 :
82 : #endif //SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_REDIS_H_
|