clear_old_cache
Remove expired weather cache entries older than a specified maximum age in minutes, freeing storage and ensuring data freshness.
Instructions
Clear expired weather cache entries.
Args: max_age_minutes: Maximum age in minutes before considering expired
Returns: Status message with number of entries removed
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_age_minutes | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The actual implementation of clear_expired_cache (the helper called by clear_old_cache). Iterates over weather_cache dict, finds entries older than max_age_minutes, deletes them, and returns the count.
def clear_expired_cache(self, max_age_minutes: int = 60) -> int: """Clear expired cache entries. Args: max_age_minutes: Maximum age before considering expired Returns: Number of entries removed """ now = datetime.now(tz=timezone.utc) expired_keys = [ key for key, value in self.weather_cache.items() if now - value["timestamp"] > timedelta(minutes=max_age_minutes) ] for key in expired_keys: del self.weather_cache[key] return len(expired_keys) - src/mcp_server/server.py:152-163 (handler)The MCP tool handler 'clear_old_cache' decorated with @mcp.tool(). Accepts max_age_minutes parameter, delegates to conversation_tools.clear_expired_cache(), and returns a status message.
@mcp.tool() def clear_old_cache(max_age_minutes: int = 60) -> str: """Clear expired weather cache entries. Args: max_age_minutes: Maximum age in minutes before considering expired Returns: Status message with number of entries removed """ removed = conversation_tools.clear_expired_cache(max_age_minutes) return f"Cleared {removed} expired cache entries older than {max_age_minutes} minutes" - src/mcp_server/server.py:152-163 (registration)Registration of the 'clear_old_cache' tool via the @mcp.tool() decorator on the function definition inside create_server().
@mcp.tool() def clear_old_cache(max_age_minutes: int = 60) -> str: """Clear expired weather cache entries. Args: max_age_minutes: Maximum age in minutes before considering expired Returns: Status message with number of entries removed """ removed = conversation_tools.clear_expired_cache(max_age_minutes) return f"Cleared {removed} expired cache entries older than {max_age_minutes} minutes" - Helper method get_cached_weather() used by other tools to check cache age; related to the caching system that clear_old_cache manages.
def get_cached_weather(self, cache_key: str, max_age_minutes: int = 60) -> str | None: """Get cached weather data if still fresh. Args: cache_key: Cache identifier to lookup max_age_minutes: Maximum age in minutes before cache expires Returns: Cached data if fresh, None if expired or not found """ if cache_key not in self.weather_cache: return None cached = self.weather_cache[cache_key] age = datetime.now(tz=timezone.utc) - cached["timestamp"] if age <= timedelta(minutes=max_age_minutes): cached_data: str = cached["data"] return cached_data # Cache expired, remove it del self.weather_cache[cache_key] return None