Skip to main content

A lightweight Python library that fetches URL content and stores it in a local SQLite database for a specified amount of time.

Project description

URLcache2db

A lightweight Python library that fetches URL content and stores it in a local SQLite database for a specified amount of time. If a URL is requested again within the cache expiration window, the library returns the cached content instead of making a new HTTP request.

Features

  • Avoid redundant HTTP requests by caching responses locally.
  • Easy integration with standard SQLite.
  • Supports sending custom headers and data with requests, differentiating cache entries automatically based on request payload.
  • Cleans up expired cache entries automatically.

Requirements

  • Python 3.x
  • requests

Installation

You can install the dependencies via the included requirements.txt:

pip install -r requirements.txt

Basic Usage

from urlcache import URLCache

# Initialize to cache for 1 hour (3600 seconds) in "my_cache.db"
cache = URLCache(db_path="my_cache.db", expiration_seconds=3600)

# Fetch the content. Will make an HTTP request on the first call.
content = cache.get("https://httpbin.org/get")

# Call it again. If it happens within an hour, it skips the HTTP request 
# and returns the cache from SQLite immediately.
cached_content = cache.get("https://httpbin.org/get")

Advanced Usage (Headers & Data)

The cache key is uniquely generated based on the URL, headers, and data. Changing the headers or payload data will result in a separate cache entry.

from urlcache import URLCache

cache = URLCache(expiration_seconds=600)

custom_headers = {
    "User-Agent": "MyCustomApp/1.0",
    "Authorization": "Bearer 1234567890"
}

payload_data = {
    "search": "python",
    "limit": 10
}

# First time: makes the GET request
content = cache.get("https://httpbin.org/get", headers=custom_headers, data=payload_data)

# Second time within 10 minutes: uses the cached version
cached_content = cache.get("https://httpbin.org/get", headers=custom_headers, data=payload_data)

Maintenance

You can periodically remove expired entries from your database using:

cache.clear_expired()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

urlcache2db-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

urlcache2db-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file urlcache2db-0.1.0.tar.gz.

File metadata

  • Download URL: urlcache2db-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for urlcache2db-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b3aeb2d38410c21215ea4867e49f586b778b40ff35b3d5324786cbef33169d4
MD5 954bef6c49bf076f9cc45b9e60f37d75
BLAKE2b-256 f1df4f86becefc1e4ef3faf9042a539c1dbdbae1f09ca861d03312e3e2d28426

See more details on using hashes here.

File details

Details for the file urlcache2db-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: urlcache2db-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for urlcache2db-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 414c094c3f9b67d7ba01c6760415735f78cd412fede0b0d6f8fc0e49a597d467
MD5 5c77c968365af8dab1d847ce2f2f326a
BLAKE2b-256 ae878fb0d6d488d1084c7c9797065ddd3f0647c8b2f38320f77bc391a733afea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page