CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/pypi-requests-cache

tessl install tessl/pypi-requests-cache@1.2.0

A persistent cache for python requests

Agent Success

Agent success rate when using this tile

76%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.27x

Baseline

Agent success rate without this tile

60%

task.mdevals/scenario-9/

API Response Cache Manager

Build a simple Python module that fetches data from a public REST API and caches responses with configurable expiration times to minimize unnecessary network requests.

Requirements

Create a Python module cache_manager.py that demonstrates HTTP caching with time-based expiration:

  1. Set up HTTP caching - Initialize caching for HTTP requests with configurable expiration times
  2. Fetch API data - Retrieve data from the JSONPlaceholder API (https://jsonplaceholder.typicode.com/)
  3. Use different expiration durations - Demonstrate caching with both short (5 seconds) and long (3600 seconds / 1 hour) expiration times
  4. Show cache behavior - Demonstrate that responses are cached and reused until they expire

Functional Requirements

  • Fetch posts from https://jsonplaceholder.typicode.com/posts with a 5-second cache expiration
  • Fetch a user from https://jsonplaceholder.typicode.com/users/1 with a 1-hour cache expiration
  • Demonstrate that repeated requests within the expiration window use cached data
  • Clear the cache when needed

Notes

  • The caching should be transparent - the same code fetches data whether cached or not
  • Cache expiration times should be set when the cache is configured
  • Cached responses should automatically expire after their configured duration

Implementation

@generates

API

def fetch_posts_with_short_cache():
    """
    Fetches posts from JSONPlaceholder API using a cached session
    with 5-second expiration.

    Returns:
        List of post dictionaries
    """
    pass

def fetch_user_with_long_cache():
    """
    Fetches user with ID=1 from JSONPlaceholder API using a cached session
    with 1-hour (3600 seconds) expiration.

    Returns:
        Dictionary containing user information
    """
    pass

def clear_cache():
    """
    Clears all cached responses.
    """
    pass

Test Cases

  • Calling fetch_posts_with_short_cache() twice in quick succession returns the same cached data on the second call @test
  • Calling fetch_user_with_long_cache() twice in quick succession returns the same cached data on the second call @test
  • After clearing the cache, fetch_posts_with_short_cache() makes a fresh network request @test
  • After waiting 6 seconds, fetch_posts_with_short_cache() makes a fresh network request due to expiration @test

Dependencies { .dependencies }

requests-cache { .dependency }

A persistent HTTP cache for Python requests that provides transparent caching with configurable expiration.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/requests-cache@1.2.x
tile.json