or run

npx @tessl/cli init
Log in

Version

Files

docs

chart-components.mdchart-plugins.mddata-transformation.mdform-data.mdindex.mdquery-building.md
tile.json

task.mdevals/scenario-2/

Query Result Cache Helper

Build a utility module that helps determine the effective cache timeout for database queries in a BI application with hierarchical cache configuration. The system should follow Apache Superset's multi-level cache timeout resolution pattern.

Requirements

Implement a cache timeout resolver that follows this hierarchy (from most specific to most general):

  1. Query-level: Individual query cache timeout override
  2. Chart-level: Chart's cache timeout setting
  3. Dataset-level: Dataset's cache timeout configuration
  4. Database-level: Database connection cache timeout
  5. System default: Global application default

The resolver should return the first non-null timeout value found when traversing this hierarchy.

Functionality

Timeout Resolution

Create a function that determines the effective cache timeout:

def resolve_cache_timeout(
    query_timeout: Optional[int] = None,
    chart_timeout: Optional[int] = None,
    dataset_timeout: Optional[int] = None,
    database_timeout: Optional[int] = None,
    system_default: int = 3600
) -> int:
    """
    Resolve cache timeout using hierarchical configuration.

    Returns the first non-None timeout from the hierarchy,
    or system_default if all are None.
    """
    pass

Cache Key Generation

Create a function that generates consistent cache keys from query parameters:

def generate_cache_key(
    sql: str,
    database_name: str,
    schema: Optional[str] = None,
    user_id: Optional[int] = None
) -> str:
    """
    Generate a cache key hash from query parameters.

    The key should deterministically represent the query context
    and include user_id for user-specific caching.
    """
    pass

Cache Configuration Helper

Create a helper class that combines both capabilities:

class CacheConfigHelper:
    def __init__(self, system_default_timeout: int = 3600):
        """Initialize with system default timeout in seconds."""
        pass

    def get_effective_timeout(
        self,
        query_timeout: Optional[int] = None,
        chart_timeout: Optional[int] = None,
        dataset_timeout: Optional[int] = None,
        database_timeout: Optional[int] = None
    ) -> int:
        """Get the effective timeout following the hierarchy."""
        pass

    def create_cache_key(
        self,
        sql: str,
        database_name: str,
        schema: Optional[str] = None,
        user_id: Optional[int] = None
    ) -> str:
        """Generate cache key for the given query parameters."""
        pass

Test Cases

  • When all timeout levels are provided, returns the query-level timeout @test
  • When query timeout is None but chart timeout exists, returns chart-level timeout @test
  • When only dataset and database timeouts exist, returns dataset-level timeout @test
  • When all timeouts are None, returns the system default @test
  • Cache keys are identical for queries with the same parameters @test
  • Cache keys differ when any parameter changes @test

@generates

Dependencies { .dependencies }

hashlib { .dependency }

Provides cryptographic hash functions for cache key generation (Python standard library).