Skip to main content

A PostgreSQL wrapper similar to diskcache

Project description

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 sqlalchemy asyncpg

Usage

PgCache

Initialization

from your_module 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.

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

pg_cache-0.1.4.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pg_cache-0.1.4-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pg_cache-0.1.4.tar.gz.

File metadata

  • Download URL: pg_cache-0.1.4.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for pg_cache-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cd96579c115ae84207ae6eed18023105971dac70bc5e8a0cc8daf59010dbe06e
MD5 c883f7df2618119100cc85d0a24a176e
BLAKE2b-256 d9ad881428bf46a74fda3ba75b2bc4768fd7b41d87d3a328987ca689d58f94c2

See more details on using hashes here.

File details

Details for the file pg_cache-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pg_cache-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for pg_cache-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 076f78377f03e45ddc65e4bd311108599486382b37769a7146a83383678df362
MD5 730f6bb95d773850ef4f1993fa58d92c
BLAKE2b-256 fa2f87575384413c55bca4234efa61a4e144baade579de2890e00d130cfc1f4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page