ctrl-utils
Classes | Public Member Functions | List of all members
ctrl_utils::RedisClient Class Reference
Inheritance diagram for ctrl_utils::RedisClient:

Public Member Functions

void connect (const std::string &host="127.0.0.1", size_t port=6379, const std::string &password="")
 
template<typename T >
std::future< T > get (const std::string &key)
 
template<typename T >
std::future< void > get (const std::string &key, T &val)
 
template<typename T >
RedisClientget (const std::string &key, const std::function< void(T &&)> &reply_callback, const std::function< void(const std::string &)> &error_callback={})
 
template<typename T >
sync_get (const std::string &key)
 
template<typename T >
std::future< cpp_redis::reply > set (const std::string &key, const T &value)
 
template<typename T >
RedisClientset (const std::string &key, const T &value, const reply_callback_t &reply_callback)
 
template<typename T >
cpp_redis::reply sync_set (const std::string &key, const T &value)
 
template<class... Ts, class... Strings, typename = is_all_strings<Strings...>>
std::future< std::tuple< Ts... > > mget (const Strings &... keys)
 
template<typename T >
std::future< std::vector< T > > mget (const std::vector< std::string > &keys)
 
template<class... Ts, class... Strings, typename = is_all_strings<Strings...>>
std::tuple< Ts... > sync_mget (const Strings &... keys)
 
template<typename T >
std::vector< T > sync_mget (const std::vector< std::string > &keys)
 
template<class... Pairs, typename = is_all_pairs<Pairs...>>
std::future< cpp_redis::reply > mset (const Pairs &... key_vals)
 
template<typename T >
std::future< cpp_redis::reply > mset (const std::vector< std::pair< std::string, T >> &key_vals)
 
template<typename T >
RedisClientmset (const std::vector< std::pair< std::string, T >> &key_vals, const reply_callback_t &reply_callback)
 
template<class... Pairs, typename = is_all_pairs<Pairs...>>
cpp_redis::reply sync_mset (const Pairs &... key_vals)
 
template<typename T >
cpp_redis::reply sync_mset (const std::vector< std::pair< std::string, T >> &key_vals)
 
template<typename T >
RedisClienthset (const std::string &key, const std::string &field, const T &value, const reply_callback_t &reply_callback)
 
template<typename T >
std::future< cpp_redis::reply > hset (const std::string &key, const std::string &field, const T &value)
 
template<typename T >
cpp_redis::reply sync_hset (const std::string &key, const std::string &field, const T &value)
 
template<typename T >
RedisClientpublish (const std::string &key, const T &value, const reply_callback_t &reply_callback)
 
template<typename T >
std::future< cpp_redis::reply > publish (const std::string &key, const T &value)
 
template<typename T >
cpp_redis::reply sync_publish (const std::string &key, const T &value)
 
template<typename TSub , typename TPub >
RedisClientrequest (const std::string &key_pub, const TPub &value_pub, const std::string &key_sub, std::function< void(TSub &&)> &&sub_callback)
 
template<typename TSub , typename TPub >
std::future< TSub > request (const std::string &key_pub, const TPub &value_pub, const std::string &key_sub)
 
template<typename TSub , typename TPub >
TSub sync_request (const std::string &key_pub, const TPub &value_pub, const std::string &key_sub)
 
RedisClientscan (const std::string &pattern, std::function< void(std::unordered_set< std::string > &&)> &&callback)
 
std::future< std::unordered_set< std::string > > scan (const std::string &pattern)
 
std::unordered_set< std::string > sync_scan (const std::string &pattern)
 
template<class... Ts, class... Args, typename >
std::tuple< Ts... > sync_mget (const Args &... args)
 

Member Function Documentation

◆ get() [1/3]

template<typename T >
std::future< T > ctrl_utils::RedisClient::get ( const std::string &  key)

Asynchronous Redis GET command with std::future.

Values will get converted from strings with ctrl_utils::FromString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator>>(). These specializations can be defined locally for custom types in your code.

Commands are not sent until RedisClient::commit() is called.

Example

std::future<double> value = redis_client.get<double>("key");
redis_client.commit();
std::cout << "Value: " << value.get() << std::endl;
Parameters
keyRedis key.
Returns
Future Redis value.

◆ get() [2/3]

template<typename T >
RedisClient & ctrl_utils::RedisClient::get ( const std::string &  key,
const std::function< void(T &&)> &  reply_callback,
const std::function< void(const std::string &)> &  error_callback = {} 
)

Asynchronous Redis GET command with callbacks.

Values will get converted from strings with ctrl_utils::FromString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator>>(). These specializations can be defined locally for custom types in your code.

Commands are not sent until RedisClient::commit() is called.

Example

redis_client.get<double>("key", [](double&& value) {
std::cout << "Value: " << value << std::endl;
}, [](const std::string& error) {
std::cerr << "GET failed: " << error << std::endl;
});
redis_client.commit();
Parameters
keyRedis key.
reply_callbackCallback function that gets the value result passed in as an Rvalue reference the command has finished.
error_callbackCallback function that gets called with the error string when the command has failed.
Returns
RedisClient reference for command chaining.

◆ get() [3/3]

template<typename T >
std::future< void > ctrl_utils::RedisClient::get ( const std::string &  key,
T &  val 
)

Asynchronous Redis GET command with preallocated value.

Values will get converted from strings with ctrl_utils::FromString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator>>(). These specializations can be defined locally for custom types in your code.

Commands are not sent until RedisClient::commit() is called.

Example

double value;
std::future<void> fut = redis_client.get("key", value);
redis_client.commit();
fut.wait();
std::cout << "Value: " << value << std::endl;
Parameters
keyRedis key.
Returns
Future Redis value.

◆ mget() [1/2]

template<typename T >
std::future< std::vector< T > > ctrl_utils::RedisClient::mget ( const std::vector< std::string > &  keys)

Asynchronous Redis MGET command with std::future for homogeneous value types.

Commands are not sent until RedisClient::commit() is called.

Example

std::future<std::vector<int>> fut_values = redis_client.mget<int>({"key1",
"key2"}); redis_client.commit(); for (int value : fut_values.get()) {
std::cout << value << std::endl;
}
Parameters
key_valsVector of key-value pairs.
Returns
Future Redis values packed into a std::vector.

◆ mget() [2/2]

template<class... Ts, class... Strings, typename >
std::future< std::tuple< Ts... > > ctrl_utils::RedisClient::mget ( const Strings &...  keys)

Asynchronous Redis MGET command with std::future for heterogeneous value types.

Commands are not sent until RedisClient::commit() is called.

Example

std::future<std::tuple<double, int>> fut_values = redis_client.mget<double,
int>("key1", "key2"); redis_client.commit(); std::tuple<double, int> values
= fut_values.get(); std::cout << std::get<0>(values) << " " <<
std::get<1>(values) << std::endl;
Parameters
key_valsVector of key-value pairs.
Returns
Future Redis values packed into a std::tuple.

◆ mset() [1/3]

template<class... Pairs, typename >
std::future< cpp_redis::reply > ctrl_utils::RedisClient::mset ( const Pairs &...  key_vals)

Asynchronous Redis MSET command with std::future for heterogeneous value types.

Commands are not sent until RedisClient::commit() is called.

Example

// key1 and key2 will be set atomically.
redis_client.mset(std::make_pair("key1", 1234), std::make_pair("key2",
1234));
redis_client.commit();
Parameters
key_valsVector of key-value pairs.
Returns
Future Redis reply.

◆ mset() [2/3]

template<typename T >
std::future< cpp_redis::reply > ctrl_utils::RedisClient::mset ( const std::vector< std::pair< std::string, T >> &  key_vals)

Asynchronous Redis MSET command with std::future for homogeneous value types.

Commands are not sent until RedisClient::commit() is called.

Example

// key1 and key2 will be set atomically.
std::vector<std::pair<std::string, int>> key_vals;
key_vals.emplace_back("key1", 1234);
key_vals.emplace_back("key2", 1234);
redis_client.mset(key_vals);
// Brace-initialization of key-value pairs requires explicit template
specification.
// key3 and key4 will not necessarily be set atomically with key1 and key2.
redis_client.mset<int>({{"key3", 1234}, {"key4", 1234}});
redis_client.commit();
Parameters
key_valsVector of key-value pairs.
Returns
Future Redis reply.

◆ mset() [3/3]

template<typename T >
RedisClient & ctrl_utils::RedisClient::mset ( const std::vector< std::pair< std::string, T >> &  key_vals,
const reply_callback_t &  reply_callback 
)

Asynchronous Redis MSET command with callbacks.

Commands are not sent until RedisClient::commit() is called.

Example

// key1 and key2 will be set atomically.
std::vector<std::pair<std::string, int>> key_vals;
key_vals.emplace_back("key1", 1234);
key_vals.emplace_back("key2", 1234);
redis_client.mset(key_vals, [](cpp_redis::reply& reply) {
if (!reply) throw std::runtime_error("Set failed.");
});
// Brace-initialization of key-value pairs requires explicit template
specification.
// key3 and key4 will not necessarily be set atomically with key1 and key2.
redis_client.mset<int>({{"key3", 1234}, {"key4", 1234}});
redis_client.commit();
Parameters
key_valsVector of key-value pairs.
reply_callbackCallback function of type std::function<void(cpp_redis::reply&)> that gets called when the command has finished.
Returns
RedisClient reference for command chaining.

◆ set() [1/2]

template<typename T >
std::future< cpp_redis::reply > ctrl_utils::RedisClient::set ( const std::string &  key,
const T &  value 
)

Asynchronous Redis SET command with std::future.

Values will get converted to strings with ctrl_utils::ToString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator<<(). These specializations can be defined locally for custom types in your code.

Commands are not sent until RedisClient::commit() is called.

Example

redis_client.set("key1", 1234);
redis_client.set("key2", 1234);
redis_client.commit();
Parameters
keyRedis key.
valueRedis value.
Returns
Future Redis reply.

◆ set() [2/2]

template<typename T >
RedisClient & ctrl_utils::RedisClient::set ( const std::string &  key,
const T &  value,
const reply_callback_t &  reply_callback 
)

Asynchronous Redis SET command with callbacks.

Values will get converted to strings with ctrl_utils::ToString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator<<(). These specializations can be defined locally for custom types in your code.

Commands are not sent until RedisClient::commit() is called.

Example

redis_client.set("key", 1234, [](cpp_redis::reply& reply) {
if (!reply) throw std::runtime_error("Set failed.");
});
redis_client.commit();
Parameters
keyRedis key.
valueRedis value.
reply_callbackCallback function of type std::function<void(cpp_redis::reply&)> that gets called when the command has finished.
Returns
RedisClient reference for command chaining.

◆ sync_get()

template<typename T >
T ctrl_utils::RedisClient::sync_get ( const std::string &  key)

Synchronous Redis GET command with std::future.

Values will get converted from strings with ctrl_utils::FromString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator>>(). These specializations can be defined locally for custom types in your code.

Example

double value = redis_client.sync_get<double>("key");
std::cout << "Value: " << value.get() << std::endl;
Parameters
keyRedis key.
Returns
Future Redis value.

◆ sync_mget() [1/2]

template<typename T >
std::vector< T > ctrl_utils::RedisClient::sync_mget ( const std::vector< std::string > &  keys)

Synchronous Redis MGET command for a vector of a single type.

Example

std::vector<int> values = redis_client.sync_mget<int>({"key1", "key2"});
for (int value : fut_values.get()) {
std::cout << value << std::endl;
}
Parameters
key_valsVector of key-value pairs.
Returns
Redis values in a std::vector.

◆ sync_mget() [2/2]

template<class... Ts, class... Strings, typename = is_all_strings<Strings...>>
std::tuple<Ts...> ctrl_utils::RedisClient::sync_mget ( const Strings &...  keys)

Synchronous Redis MGET command.

Example

std::tuple<double, int> values = redis_client.sync_mget<double,
int>("key1", "key2"); std::cout << std::get<0>(values) << " " <<
std::get<1>(values) << std::endl;
Parameters
key_valsVector of key-value pairs.
Returns
Redis values in a std::tuple.

◆ sync_mset() [1/2]

template<class... Pairs, typename >
cpp_redis::reply ctrl_utils::RedisClient::sync_mset ( const Pairs &...  key_vals)

Synchronous Redis MSET command for heterogeneous value types.

Example

// key1 and key2 will be set atomically.
redis_client.sync_mset(std::make_pair("key1", 1234), std::make_pair("key2",
1234));
Parameters
key_valsVector of key-value pairs.
Returns
Redis reply.

◆ sync_mset() [2/2]

template<typename T >
cpp_redis::reply ctrl_utils::RedisClient::sync_mset ( const std::vector< std::pair< std::string, T >> &  key_vals)

Synchronous Redis MSET command for homogeneous value types.

Example

// key1 and key2 will be set atomically.
std::vector<std::pair<std::string, int>> key_vals;
key_vals.emplace_back("key1", 1234);
key_vals.emplace_back("key2", 1234);
redis_client.sync_mset(key_vals);
// Brace-initialization of key-value pairs requires explicit template
specification.
// key3 and key4 will not necessarily be set atomically with key1 and key2.
redis_client.sync_mset<int>({{"key3", 1234}, {"key4", 1234}});
Parameters
key_valsVector of key-value pairs.
Returns
Redis reply.

◆ sync_set()

template<typename T >
cpp_redis::reply ctrl_utils::RedisClient::sync_set ( const std::string &  key,
const T &  value 
)

Synchronous Redis SET command.

Values will get converted to strings with ctrl_utils::ToString<T>(), or if a specialization for that type doesn't exist, std::stringstream::operator<<(). These specializations can be defined locally for custom types in your code.

Example

redis_client.sync_set("key", 1234);
Parameters
keyRedis key.
valueRedis value.
Returns
Redis reply.

The documentation for this class was generated from the following file: