Skip to main content

DBCACHE

This small library provides small and simple stores based on postgresql for the following purposes:

  • a cache store for bytes with an expiration timestamp
  • a key-value store with json serializable values
  • a versioned key-value store with values as bytes

To initialize the database schema, type:

$ dbcache init-db postgresql://foo:bar@postgresql/mydb

Example usage (cache):

  from time import sleep
  from datetime import timedelta
  from dbcache.api import dbcache

  cache = dbcache('postgresql://foo:bar@postgresql/mydb')

  assert cache.get('a') is None
  cache.set('a', b'aaa')
  assert cache.get('a') == b'aaa'

  cache.delete('a')
  assert cache.get('a') is None

  cache.set('b', b'bbb', lifetime=timedelta(seconds=1))
  assert cache.get('b') == b'bbb'
  sleep(1)
  assert cache.get('b') is None

Example usage (key/value store):

  from dbcache.api import kvstore
  store = kvstore('postgresql://foo:bar@postgresql/mydb')

  assert store.get('a') is None
  store.set('a', 'aaa')
  assert store.get('a') == 'aaa'
  store.set('a', 'newvalue')
  assert store.get('a') == 'newvalue'

  store.delete('a')
  assert store.get('a') is None

  store.set('int', 42)
  assert store.get('int') == 42

  store.set('float', 42.0)
  store.set('dict', {'a': [1, 2]})

  assert store.all() == {
      'dict': {'a': [1, 2]},
      'float': 42.0,
      'int': 42
  }

  assert store.keys() == ['dict', 'float', 'int']

Example usage (versioned key/value store):

  from dbcache.api import vkvstore
  vstore = vkvstore('postgresql://foo:bar@postgresql/mydb')

  assert vstore.get('html') is None
  v1 = datetime(2023, 1, 1, 12, 30, tzinfo=timezone.utc)
  vstore.set(
      'html',
      b'<html></html>',
      insertion_date=v1
  )
  assert vstore.get('html') == b'<html></html>'

  v2 = datetime(2023, 1, 2, 12, 30, tzinfo=timezone.utc)
  vstore.set(
      'html',
      b'<html>Hello, World</html>',
      insertion_date=v2
  )
  assert vstore.get('html') == b'<html>Hello, World</html>'
  assert vstore.get('html', insertion_date=v1) == b'<html></html>'

  assert vstore.versions('html') == [
      v1,
      v2
  ]
  assert vstore.versions('nope') == []

  vstore.delete('html')
  assert vstore.keys() == []
  assert vstore.get('html') is None

Metadata

Release files for dbcache 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for dbcache 0.5.0
File Interpreter ABI Platform
dbcache-0.5.0-py3-none-any.whl Python 3 none any Details

Release files / dbcache-0.5.0-py3-none-any.whl

Download URL dbcache-0.5.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbc056ba0e0cda241a0a595c265db2e68a4a47593a883803001cdcbbac41f501
BLAKE2b-256 checksum
How to use checksums
089a0340994a7f82a4e13dbaad6732695cf666b0a91ea6c91337125b9dea2218
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.5.0 This release

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page