expire
Set expiration time for Redis keys to automatically remove data after specified seconds, managing memory and data lifecycle.
Instructions
Set an expiration time for a Redis key.
Args: name: The Redis key. expire_seconds: Time in seconds after which the key should expire.
Returns: A success message or an error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| expire_seconds | Yes |
Implementation Reference
- src/tools/misc.py:50-70 (handler)The main handler function for the 'expire' tool. It sets an expiration time on a specified Redis key using the Redis EXPIRE command. Includes input validation via type hints and docstring, error handling for Redis errors, and returns a success or error message.@mcp.tool() async def expire(name: str, expire_seconds: int) -> str: """Set an expiration time for a Redis key. Args: name: The Redis key. expire_seconds: Time in seconds after which the key should expire. Returns: A success message or an error message. """ try: r = RedisConnectionManager.get_connection() success = r.expire(name, expire_seconds) return ( f"Expiration set to {expire_seconds} seconds for '{name}'." if success else f"Key '{name}' does not exist." ) except RedisError as e: return f"Error setting expiration for key '{name}': {str(e)}"