Skip to content

Support CLIENT TRACKING #1612

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

Merged
merged 3 commits into from
Dec 22, 2021
Merged
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
98 changes: 98 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,104 @@ def client_id(self, **kwargs):
"""
return self.execute_command("CLIENT ID", **kwargs)

def client_tracking_on(
self,
clientid=None,
prefix=[],
bcast=False,
optin=False,
optout=False,
noloop=False,
):
"""
Turn on the tracking mode.
For more information about the options look at client_tracking func.

See https://redis.io/commands/client-tracking
"""
return self.client_tracking(
True, clientid, prefix, bcast, optin, optout, noloop
)

def client_tracking_off(
self,
clientid=None,
prefix=[],
bcast=False,
optin=False,
optout=False,
noloop=False,
):
"""
Turn off the tracking mode.
For more information about the options look at client_tracking func.

See https://redis.io/commands/client-tracking
"""
return self.client_tracking(
False, clientid, prefix, bcast, optin, optout, noloop
)

def client_tracking(
self,
on=True,
clientid=None,
prefix=[],
bcast=False,
optin=False,
optout=False,
noloop=False,
**kwargs,
):
"""
Enables the tracking feature of the Redis server, that is used
for server assisted client side caching.

``on`` indicate for tracking on or tracking off. The dafualt is on.

``clientid`` send invalidation messages to the connection with
the specified ID.

``bcast`` enable tracking in broadcasting mode. In this mode
invalidation messages are reported for all the prefixes
specified, regardless of the keys requested by the connection.

``optin`` when broadcasting is NOT active, normally don't track
keys in read only commands, unless they are called immediately
after a CLIENT CACHING yes command.

``optout`` when broadcasting is NOT active, normally track keys in
read only commands, unless they are called immediately after a
CLIENT CACHING no command.

``noloop`` don't send notifications about keys modified by this
connection itself.

``prefix`` for broadcasting, register a given key prefix, so that
notifications will be provided only for keys starting with this string.

See https://redis.io/commands/client-tracking
"""

if len(prefix) != 0 and bcast is False:
raise DataError("Prefix can only be used with bcast")

pieces = ["ON"] if on else ["OFF"]
if clientid is not None:
pieces.extend(["REDIRECT", clientid])
for p in prefix:
pieces.extend(["PREFIX", p])
if bcast:
pieces.append("BCAST")
if optin:
pieces.append("OPTIN")
if optout:
pieces.append("OPTOUT")
if noloop:
pieces.append("NOLOOP")

return self.execute_command("CLIENT TRACKING", *pieces)

def client_trackinginfo(self, **kwargs):
"""
Returns the information about the current client connection's
Expand Down
22 changes: 22 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,28 @@ def test_client_trackinginfo(self, r):
assert len(res) > 2
assert "prefixes" in res

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("6.0.0")
def test_client_tracking(self, r, r2):

# simple case
assert r.client_tracking_on()
assert r.client_tracking_off()

# id based
client_id = r.client_id()
assert r.client_tracking_on(client_id)
assert r.client_tracking_off(client_id)

# id exists
client_id = r2.client_id()
assert r.client_tracking_on(client_id)
assert r2.client_tracking_off(client_id)

# now with some prefixes
with pytest.raises(exceptions.DataError):
assert r.client_tracking_on(prefix=["foo", "bar", "blee"])

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("5.0.0")
def test_client_unblock(self, r):
Expand Down