Skip to main content

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 = {
    "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)

SSL Verification & Custom User-Agent

You can easily bypass SSL certificate verification for self-signed or expired certificates using verify=False, and conveniently set a custom User-Agent string:

from urlcache import URLCache

cache = URLCache()

# Fetch content bypassing SSL verification and setting a specific User-Agent
content = cache.get(
    "https://expired.badssl.com/", 
    user_agent="MyCustomApp/2.0", 
    verify=False
)

Maintenance

You can periodically remove expired entries from your database using:

cache.clear_expired()

Metadata

Release files for urlcache2db 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for urlcache2db 0.1.2
File Size Uploaded
urlcache2db-0.1.2.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for urlcache2db 0.1.2
File Interpreter ABI Platform
urlcache2db-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / urlcache2db-0.1.2.tar.gz

Download URL urlcache2db-0.1.2.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6036e379d5af944d5a61b005b1f09ba72b7d7cfe5d7b3098e03aac06e1d9fba1
BLAKE2b-256 checksum
How to use checksums
d379cbbdaa2ef5843f2583db687474eeb0e3ed9cc6f125023218d66d25714f43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / urlcache2db-0.1.2-py3-none-any.whl

Download URL urlcache2db-0.1.2-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59f9c7e1132f74cd8d38e26affbdf207c1555cdeafd7560534ee6b6238c4341b
BLAKE2b-256 checksum
How to use checksums
5342795bb871b955078fb48f464396487a5aab65e5132c0335fec83344a9b8ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page