set_expire
Set an expiration time in seconds on a Redis key. The key will be automatically deleted after the specified duration.
Instructions
设置键的过期时间(需要关闭只读模式)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Redis键名 | |
| seconds | Yes | 过期时间(秒) |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/redis_mcp_server/db.py:285-291 (handler)Core handler logic for set_expire. Checks write permission, verifies key access, then calls Redis EXPIRE command. Returns success status along with the key and expire seconds.
def set_expire(self, key: str, seconds: int) -> dict[str, Any]: """设置键的过期时间""" self._check_write_permission() if not self._is_key_allowed(key): raise PermissionError(f"键 '{key}' 不允许访问") result = self._client.expire(key, seconds) return {"key": key, "expire_seconds": seconds, "success": result} - src/redis_mcp_server/server.py:183-198 (handler)MCP tool wrapper for set_expire. Registered with @mcp.tool() decorator, calls db.set_expire(), serializes result to JSON, handles PermissionError and other exceptions.
@mcp.tool() def set_expire(key: str, seconds: int) -> str: """设置键的过期时间(需要关闭只读模式) Args: key: Redis键名 seconds: 过期时间(秒) """ try: result = db.set_expire(key, seconds) return json.dumps(result, ensure_ascii=False, indent=2) except PermissionError as e: return json.dumps({"error": str(e)}, ensure_ascii=False) except Exception as e: return json.dumps({"error": str(e)}, ensure_ascii=False) - tests/test_server.py:100-106 (registration)Test that verifies set_expire is registered as a tool in the MCP server's tool manager.
def test_server_has_tools(self): tool_names = list(self.server.mcp._tool_manager._tools.keys()) expected = [ "ping", "server_info", "db_size", "scan_keys", "get_key_type", "get_key_value", "get_hash", "get_memory_usage", "set_string", "delete_keys", "set_expire", ]