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
headersanddatawith 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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file urlcache2db-0.1.2.tar.gz.
File metadata
- Download URL: urlcache2db-0.1.2.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6036e379d5af944d5a61b005b1f09ba72b7d7cfe5d7b3098e03aac06e1d9fba1
|
|
| MD5 |
9c34c764db27bd498f94817240a56dfb
|
|
| BLAKE2b-256 |
d379cbbdaa2ef5843f2583db687474eeb0e3ed9cc6f125023218d66d25714f43
|
File details
Details for the file urlcache2db-0.1.2-py3-none-any.whl.
File metadata
- Download URL: urlcache2db-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59f9c7e1132f74cd8d38e26affbdf207c1555cdeafd7560534ee6b6238c4341b
|
|
| MD5 |
8e0e3f2a40ccd85fba2c054b7e04b2cd
|
|
| BLAKE2b-256 |
5342795bb871b955078fb48f464396487a5aab65e5132c0335fec83344a9b8ef
|