Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support client side caching when using sync interface #509

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/sw/redis++/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ inline void hello(Connection &connection, long long version) {
connection.send("HELLO %lld", version);
}

inline void client_tracking(Connection &connection, bool on) {
if (on) {
connection.send("CLIENT TRACKING ON");
} else {
connection.send("CLIENT TRACKING OFF");
}
}

inline void echo(Connection &connection, const StringView &msg) {
connection.send("ECHO %b", msg.data(), msg.size());
}
Expand Down
19 changes: 19 additions & 0 deletions src/sw/redis++/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ void Connection::_set_options() {
if (_opts.readonly) {
_enable_readonly();
}

#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
if (_opts.resp > 2 && _opts.client_tracking && _opts.push_cb) {
_context()->privdata = _opts.privdata;
_context()->free_privdata = _opts.privdata_dtor;
set_push_callback(_opts.push_cb);
_set_client_tracking(true);
}
#endif
}

void Connection::_enable_readonly() {
Expand All @@ -297,6 +306,16 @@ void Connection::_set_resp_version() {
// TODO: parse hello reply.
}

void Connection::_set_client_tracking(bool on) {
cmd::client_tracking(*this, on);

auto reply = recv();

assert(reply);

// TODO: parse reply.
}

void Connection::_auth() {
const std::string DEFAULT_USER = "default";

Expand Down
14 changes: 14 additions & 0 deletions src/sw/redis++/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ struct ConnectionOptions {
// RESP version.
int resp = 2;

#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
// https://redis.io/docs/manual/client-side-caching/
bool client_tracking = false;

// Optional user defined data/destructor
void *privdata = nullptr;
void (*privdata_dtor)(void *) = nullptr;

// A user defined PUSH message callback
redisPushFn *push_cb = nullptr;
#endif

// For internal use, and might be removed in the future. DO NOT use it in client code.
std::string _server_info() const;
};
Expand Down Expand Up @@ -182,6 +194,8 @@ class Connection {

void _set_resp_version();

void _set_client_tracking(bool);

redisContext* _context();

ContextUPtr _ctx;
Expand Down
6 changes: 6 additions & 0 deletions src/sw/redis++/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ ConnectionPool::ConnectionPool(const ConnectionPoolOptions &pool_opts,
throw Error("CANNOT create an empty pool");
}

#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
if (_opts.client_tracking && _pool_opts.size > 1) {
throw Error("CANNOT enable client tracking with pool size greater than 1");
}
#endif

// Lazily create connections.
}

Expand Down