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

Python: add LASTSAVE command #1509

Merged
merged 1 commit into from
May 31, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Python: Added PFCOUNT command ([#1493](https://github.com/aws/glide-for-redis/pull/1493))
* Python: Added PFMERGE command ([#1497](https://github.com/aws/glide-for-redis/pull/1497))
* Node: Added SINTER command ([#1500](https://github.com/aws/glide-for-redis/pull/1500))
* Python: Added LASTSAVE command ([#1509](https://github.com/aws/glide-for-redis/pull/1509))

## 0.4.0 (2024-05-26)

Expand Down
27 changes: 27 additions & 0 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,30 @@ async def time(self, route: Optional[Route] = None) -> TClusterResponse[List[str
TClusterResponse[List[str]],
await self._execute_command(RequestType.Time, [], route),
)

async def lastsave(self, route: Optional[Route] = None) -> TClusterResponse[int]:
"""
Returns the Unix time of the last DB save timestamp or startup timestamp if no save was made since then.

See https://valkey.io/commands/lastsave for more details.

Args:
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
in which case the client will route the command to the nodes defined by `route`.

Returns:
TClusterResponse[int]: The Unix time of the last successful DB save.
If no route is provided, or a single node route is requested, returns an int representing the Unix time
of the last successful DB save. Otherwise, returns a dict of [str , int] where each key contains the
address of the queried node and the value contains the Unix time of the last successful DB save.

Examples:
>>> await client.lastsave()
1710925775 # Unix time of the last DB save
>>> await client.lastsave(AllNodes())
{'addr1': 1710925775, 'addr2': 1710925775, 'addr3': 1710925775} # Unix time of the last DB save on each node
"""
return cast(
TClusterResponse[int],
await self._execute_command(RequestType.LastSave, [], route),
)
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,21 @@ async def time(self) -> List[str]:
List[str],
await self._execute_command(RequestType.Time, []),
)

async def lastsave(self) -> int:
"""
Returns the Unix time of the last DB save timestamp or startup timestamp if no save was made since then.

See https://valkey.io/commands/lastsave for more details.

Returns:
int: The Unix time of the last successful DB save.

Examples:
>>> await client.lastsave()
1710925775 # Unix time of the last DB save
"""
return cast(
int,
await self._execute_command(RequestType.LastSave, []),
)
11 changes: 11 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,17 @@ def echo(self: TTransaction, message: str) -> TTransaction:
"""
return self.append_command(RequestType.Echo, [message])

def lastsave(self: TTransaction) -> TTransaction:
"""
Returns the Unix time of the last DB save timestamp or startup timestamp if no save was made since then.

See https://valkey.io/commands/lastsave for more details.

Command response:
int: The Unix time of the last successful DB save.
"""
return self.append_command(RequestType.LastSave, [])

def type(self: TTransaction, key: str) -> TTransaction:
"""
Returns the string representation of the type of the value stored at `key`.
Expand Down
24 changes: 23 additions & 1 deletion python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
import time
from collections.abc import Mapping
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from typing import Any, Dict, Union, cast

import pytest
Expand Down Expand Up @@ -3271,6 +3271,28 @@ async def test_time(self, redis_client: TRedisClient):
assert int(result[0]) > current_time
assert 0 < int(result[1]) < 1000000

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_lastsave(self, redis_client: TRedisClient):
yesterday = date.today() - timedelta(1)
yesterday_unix_time = time.mktime(yesterday.timetuple())

result = await redis_client.lastsave()
assert isinstance(result, int)
assert result > yesterday_unix_time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the last time the database was saved was yesterday? Maybe include a note about this so if this does fail, someone just needs to re-run after running BGSAVE().


if isinstance(redis_client, RedisClusterClient):
# test with single-node route
result = await redis_client.lastsave(RandomNode())
assert isinstance(result, int)
assert result > yesterday_unix_time

# test with multi-node route
result = await redis_client.lastsave(AllNodes())
assert isinstance(result, dict)
for lastsave_time in result.values():
assert lastsave_time > yesterday_unix_time

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_append(self, redis_client: TRedisClient):
Expand Down
18 changes: 17 additions & 1 deletion python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

from datetime import datetime, timezone
import time
from datetime import date, datetime, timedelta, timezone
from typing import List, Union, cast

import pytest
Expand Down Expand Up @@ -548,3 +549,18 @@ async def test_transaction_object_commands(
assert cast(int, response[6]) >= 0
finally:
await redis_client.config_set({maxmemory_policy_key: maxmemory_policy})

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_transaction_lastsave(
self, redis_client: TRedisClient, cluster_mode: bool
):
yesterday = date.today() - timedelta(1)
yesterday_unix_time = time.mktime(yesterday.timetuple())
transaction = ClusterTransaction() if cluster_mode else Transaction()
transaction.lastsave()
response = await redis_client.exec(transaction)
assert isinstance(response, list)
lastsave_time = response[0]
assert isinstance(lastsave_time, int)
assert lastsave_time > yesterday_unix_time
Loading