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