ifw-core 7.0.0
 
Loading...
Searching...
No Matches
dbInterfaceRedis.hpp
Go to the documentation of this file.
1
8
9#ifndef CORE_UTILS_BAT_DB_INTERFACE_REDIS_HPP_
10#define CORE_UTILS_BAT_DB_INTERFACE_REDIS_HPP_
11
12// System headers
13#include <string>
14#include <fmt/format.h>
15
16#pragma GCC diagnostic push
17#pragma GCC diagnostic ignored "-Wpedantic"
18
19#include <sw/redis++/redis++.h>
20
21#pragma GCC diagnostic pop
22
23// Local headers
25
26
27namespace ifw::core::utils::bat {
28
29 // These parameters are required by the Redis++ library.
31 const std::size_t REDIS_CONNECTION_POOL_SIZE = 15;
35 const int64_t REDIS_WAIT_TIMEOUT_MS = 10000;
39 const int64_t REDIS_SOCKET_TIMEOUT_MS = 15000;
40
45 public:
51 DbInterfaceRedis(const std::string& prefix,
52 const std::string& db_endpoint,
53 const timeval& db_timeout);
54
55 inline bool IsConnected() const {return m_connected;}
56 void SetStates(const std::string& prefix,
57 const std::string& state,
58 const std::string& substate);
60 template<typename T>
61 void Set(const std::string& key,
62 const T& value);
63
66
67 private:
68 std::string m_prefix;
69 std::string m_db_endpoint;
70 timeval m_db_timeout;
71 bool m_connected{false};
72 std::unique_ptr<::sw::redis::Redis> m_runtime_db;
73
74 log4cplus::Logger m_logger;
75 };
76
77 template<typename T>
78 void DbInterfaceRedis::Set(const std::string& key, const T& value) {
79 std::ostringstream os;
80 os << value;
81
82 try {
83 LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}> ...", m_prefix + key));
84 m_runtime_db->set(m_prefix + key, os.str());
85 LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}> finished !", key));
86 } catch (const ::sw::redis::Error& err) {
87 LOG4CPLUS_ERROR(m_logger, fmt::format("Problems writing attribute <{}>: <> !",
88 key, err.what()));
89 throw std::runtime_error (fmt::format("Problems writing attribute <{}>: <> !", key,
90 err.what()));
91 }
92 }
93
94} // namespace
95
96#endif // CORE_UTILS_BAT_DB_INTERFACE_REDIS_HPP_
DbInterfaceRedis(const std::string &prefix, const std::string &db_endpoint, const timeval &db_timeout)
Definition dbInterfaceRedis.cpp:19
DbInterfaceRedis(const DbInterfaceRedis &)=delete
Disable copy constructor.
void BatchSet(const ifw::core::utils::bat::DbVector &vec)
Definition dbInterfaceRedis.cpp:88
void Set(const std::string &key, const T &value)
Definition dbInterfaceRedis.hpp:78
bool IsConnected() const
Definition dbInterfaceRedis.hpp:55
DbInterfaceRedis & operator=(const DbInterfaceRedis &)=delete
Disable assignment operator.
void SetStates(const std::string &prefix, const std::string &state, const std::string &substate)
Definition dbInterfaceRedis.cpp:70
Logger header file.
Definition config.cpp:27
const int64_t REDIS_WAIT_TIMEOUT_MS
Definition dbInterfaceRedis.hpp:35
const int64_t REDIS_CONNECTION_POOL_CONN_LIFETIME_MS
Definition dbInterfaceRedis.hpp:33
const int64_t REDIS_SOCKET_TIMEOUT_MS
Definition dbInterfaceRedis.hpp:39
const std::size_t REDIS_CONNECTION_POOL_SIZE
Definition dbInterfaceRedis.hpp:31
std::vector< DbPair > DbVector
Definition config.hpp:79