ifw-core 6.0.0
Loading...
Searching...
No Matches
dbInterfaceRedis.hpp
Go to the documentation of this file.
1
7#ifndef CORE_UTILS_BAT_DB_INTERFACE_REDIS_HPP_
8#define CORE_UTILS_BAT_DB_INTERFACE_REDIS_HPP_
9
10// System headers
11#include <string>
12#include <fmt/format.h>
13
14#pragma GCC diagnostic push
15#pragma GCC diagnostic ignored "-Wpedantic"
16
17#include <sw/redis++/redis++.h>
18
19#pragma GCC diagnostic pop
20
21// Local headers
23
24
25namespace ifw::core::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);
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
DbInterfaceRedis(const std::string &prefix, const std::string &db_endpoint, const timeval &db_timeout)
Definition dbInterfaceRedis.cpp:22
DbInterfaceRedis(const DbInterfaceRedis &)=delete
Disable copy constructor.
void BatchSet(const ifw::core::utils::bat::DbVector &vec)
Definition dbInterfaceRedis.cpp:91
void Set(const std::string &key, const T &value)
Definition dbInterfaceRedis.hpp:76
bool IsConnected() const
Definition dbInterfaceRedis.hpp:53
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:73
Config class source file.
Definition config.cpp:26
const int64_t REDIS_WAIT_TIMEOUT_MS
Definition dbInterfaceRedis.hpp:33
const int64_t REDIS_CONNECTION_POOL_CONN_LIFETIME_MS
Definition dbInterfaceRedis.hpp:31
const int64_t REDIS_SOCKET_TIMEOUT_MS
Definition dbInterfaceRedis.hpp:37
const std::size_t REDIS_CONNECTION_POOL_SIZE
Definition dbInterfaceRedis.hpp:29
std::vector< DbPair > DbVector
Definition config.hpp:77