A caching package with both synchronous and asynchronous support for PostgreSQL.
Project description
PgCache
PgCache
is a synchronous caching library based on PostgreSQL, using SQLAlchemy for database operations.
Installation
First, ensure you have installed the required dependencies:
pip install sqlalchemy psycopg2
Usage
Initializing the Cache
import logging
from pg_cache.sync_cache import PgCache # Assuming the synchronous version class file is sync_cache.py
DATABASE_URL = "postgresql://username:password@localhost:5432/dbname"
def main():
cache = PgCache(DATABASE_URL, "cache_table", log_level=logging.ERROR)
# Initialize the database
cache.init_db()
# Set cache
cache.set("test_key", {"foo": "bar"}, expire_after_seconds=60)
print("Set cache entry")
# Get cache
value = cache.get("test_key")
print(f"Got cache entry: {value}")
# Delete cache
cache.delete("test_key")
print("Deleted cache entry")
# Get deleted cache
value = cache.get("test_key")
print(f"Got cache entry after deletion: {value}")
if __name__ == "__main__":
main()
Method Descriptions
__init__(self, db_url: str, table_name: str, log_level: int = logging.ERROR)
Initialize a PgCache
instance.
db_url
: Database connection string.table_name
: Name of the cache table.log_level
: Log level, default islogging.ERROR
.
init_db(self) -> None
Initialize the database and create the cache table.
set(self, key: str, value: Any, expire_after_seconds: int = 86400) -> None
Set a cache entry.
key
: Cache key.value
: Cache value, can be any type of data.expire_after_seconds
: Cache expiration time in seconds, default is 86400 seconds (1 day).
set_bulk(self, entries: List[Dict[str, Any]], expire_after_seconds: int = 86400) -> None
Set multiple cache entries in bulk.
entries
: List of multiple cache entries, each entry is a dictionary containingkey
andvalue
.expire_after_seconds
: Cache expiration time in seconds, default is 86400 seconds (1 day).
get(self, key: str) -> Optional[Any]
Get a cache entry.
key
: Cache key.- Return value: Cache value, if the cache entry does not exist or has expired, returns
None
.
delete(self, key: str) -> None
Delete a cache entry.
key
: Cache key.
flushdb(self) -> None
Clear all cache entries.
export_to_file(self, file_path: str) -> None
Export cache entries to a file.
file_path
: Path to the export file.
import_from_file(self, file_path: str) -> None
Import cache entries from a file.
file_path
: Path to the import file.
Example
Here is a complete example demonstrating how to use the PgCache
class:
import logging
from pg_cache.sync_cache import PgCache # Assuming the synchronous version class file is sync_cache.py
DATABASE_URL = "postgresql://username:password@localhost:5432/dbname"
def main():
cache = PgCache(DATABASE_URL, "cache_table", log_level=logging.ERROR)
# Initialize the database
cache.init_db()
# Set cache
cache.set("test_key", {"foo": "bar"}, expire_after_seconds=60)
print("Set cache entry")
# Get cache
value = cache.get("test_key")
print(f"Got cache entry: {value}")
# Delete cache
cache.delete("test_key")
print("Deleted cache entry")
# Get deleted cache
value = cache.get("test_key")
print(f"Got cache entry after deletion: {value}")
if __name__ == "__main__":
main()
License
This project is licensed under the MIT License.
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
File details
Details for the file pg_cache-0.1.1.tar.gz
.
File metadata
- Download URL: pg_cache-0.1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aa03f3b3bf0b7801d47662c9041ae74ac53855ffb731f0d5a7f19dc846074e6 |
|
MD5 | e7d7150cf473b0a8c88dbed17699f00a |
|
BLAKE2b-256 | 40a44eb1e75bf6f2f5cc34844cebde77d1b8f2e0685e14174feb5e500582ca9a |
File details
Details for the file pg_cache-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: pg_cache-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c74069c96f397713cf5e35ed326e3d574d79a14dcda80402e999c3b4af6e7048 |
|
MD5 | 17438c57cfc32ede790edaeb64d58e2e |
|
BLAKE2b-256 | 971e084303ec242abb78bb758c7bf352c9d6fcda07d8b1726a4268fa83fd2f3d |