PgCache and AsyncPgCache Usage Guide
Introduction
PgCache and AsyncPgCache are cache management classes for PostgreSQL databases. PgCache provides synchronous
operations, while AsyncPgCache offers asynchronous operations. They allow you to cache data in a PostgreSQL database
and provide functionalities to set, get, delete, and import/export cache entries.
Installation
Before using these classes, ensure you have the following Python packages installed:
pip install -r requirements.txt
or pip install pg-cache
Usage
PgCache
Initialization
from pg_cache import PgCache
db_url = "postgresql://user:password@localhost/dbname"
table_name = "cache_table"
cache = PgCache(db_url, table_name)
cache.init_db()
Set Cache
cache.set("my_key", "my_value", expire_after_seconds=3600)
Set Bulk Cache
entries
= [
{"key": "key1", "value": "value1"},
{"key": "key2", "value": "value2"}
]
cache.set_bulk(entries, expire_after_seconds=3600)
Get Cache
value = cache.get("my_key")
Delete Cache
cache.delete("my_key")
Delete Bulk Cache
keys = ["key1", "key2"]
cache.delete_bulk(keys)
Flush Cache
cache.flushdb()
Export Cache to File
cache.export_to_file("cache_backup.json")
Import Cache from File
cache.import_from_file("cache_backup.json")
AsyncPgCache
Initialization
import asyncio
from your_module import AsyncPgCache
db_url = "postgresql+asyncpg://user:password@localhost/dbname"
table_name = "cache_table"
cache = AsyncPgCache(db_url, table_name)
async def init():
await cache.init_db()
asyncio.run(init())
Set Cache
async def set_cache():
await cache.set("my_key", "my_value", expire_after_seconds=3600)
asyncio.run(set_cache())
Set Bulk Cache
entries = [
{"key": "key1", "value": "value1"},
{"key": "key2", "value": "value2"}
]
async def set_bulk_cache():
await cache.set_bulk(entries, expire_after_seconds=3600)
asyncio.run(set_bulk_cache())
Get Cache
async def get_cache():
value = await cache.get("my_key")
print(value)
asyncio.run(get_cache())
Delete Cache
async def delete_cache():
await cache.delete("my_key")
asyncio.run(delete_cache())
Delete Bulk Cache
keys = ["key1", "key2"]
async def delete_bulk_cache():
await cache.delete_bulk(keys)
asyncio.run(delete_bulk_cache())
Flush Cache
async def flush_cache():
await cache.flushdb()
asyncio.run(flush_cache())
Export Cache to File
async def export_cache():
await cache.export_to_file("cache_backup.json")
asyncio.run(export_cache())
Import Cache from File
async def import_cache():
await cache.import_from_file("cache_backup.json")
asyncio.run(import_cache())
Logging
You can control the logging level by setting the log_level parameter when initializing PgCache or AsyncPgCache.
For example:
cache = PgCache(db_url, table_name, log_level=logging.INFO)
Conclusion
PgCache and AsyncPgCache provide powerful cache management functionalities suitable for applications that need to
cache data in a PostgreSQL database. With both synchronous and asynchronous implementations, you can choose the
appropriate method based on your needs.
Release files for pg-cache 0.1.6.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pg_cache-0.1.6.6.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pg_cache-0.1.6.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 kB
Release files / pg_cache-0.1.6.6.tar.gz
| Download URL | pg_cache-0.1.6.6.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
109c980567eb84f09fd27f883732345a89003000e39b4a8e889e7392d15d7ece
|
|
BLAKE2b-256 checksum How to use checksums |
d3b53c93f597e39695566eb2e8960ed0b9ee2df9f5cb3273aab7afd165b060b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Release files / pg_cache-0.1.6.6-py3-none-any.whl
| Download URL | pg_cache-0.1.6.6-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
195816d715632269d2921cc2edc63122ecab67bb68f087072cb633a40912fbec
|
|
BLAKE2b-256 checksum How to use checksums |
d88649cd1e52f849282ce30730a9383bb3f79de2df4f4478decab43e501b3af8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|