Skip to main content

A postgresql wrapper similar to diskcache

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 is logging.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 containing key and value.
  • 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


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.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

pg_cache-0.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_cache-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 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.2.tar.gz
Algorithm Hash digest
SHA256 0c5558bf7f2c6723d0c61305b671cde1c5eacd6caf8a77bdfee83d356c748f2a
MD5 3674fcf5fc0c988259545b0903863d86
BLAKE2b-256 21af9995194e2b6286cbe865d5c4c1b9c90e2d8bbda18d1918701074d22fe913

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pg_cache-0.1.2-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

Hashes for pg_cache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 644e156cbb6d6651f8598a8e94a6d62e420c9e8376c7b63fb806e8f4256af0e8
MD5 fcc409d185f52c64a3b9810a4bfe5e04
BLAKE2b-256 a159c3d55c085413b33b4801d6e48cdb6330cbd836efc04fedfb72385e19056c

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