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.1.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.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: urlcache2db-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ae9e4ee666de4cdba3a713af75c223fd3e6f12b379e4cbac75dbbb17fab3694a
MD5 95eddb97f1cefad6b91cdfefc8b0e37b
BLAKE2b-256 b02a00ceac24564ac65c8506b9869914fc2edb4b606597d1924e2d94b800d212

See more details on using hashes here.

File details

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

File metadata

  • Download URL: urlcache2db-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 afc5a0a6c806888b247b118cc65870bcbbfc9dd840b172d7e6cb60537bed77b6
MD5 2d2375b2f78f1f2839f7d4df9ba26bdf
BLAKE2b-256 a03886b8d72d72cce818722ac4b4fe514ea390a94a0e80ef22ed4c840799ab4a

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