Skip to main content

A simplistic asynchronous memory cache & database library for use with msgspec dataclasses

Project description

keyspec

PyPI version PyPI - Downloads License: Apache-2.0

A msgspec-like database system for python inspired by aiocache.

Installation

pip install keyspec

Common Usage

from msgspec import Struct

from keyspec import Client, cache

# Create your msgspec Structure, Attrs dataclass or etc...
class User(Struct):
    name: str
    password: str

# you can get pretty lazy if needed.
factory = cache(
    "data.db",
    User,
)


@factory
async def insert(client: Client[User], username: str, password: str) -> None:
    await client.set(username, User(username, password))

# The less lazy option is to do this.
@cache(
    "data.db",
    User,
)
async def get(client: Client[User], username: str) -> User | None:
    return await client.get(username)


@factory
async def get_all(client: Client[User]) -> dict[str, User]:
    return await client.get_all()


async def test():
    # NOTE: Client is deleted as the factory takes care of
    # the first attribute for you...
    await insert("user", "pass")
    await insert("user1", "password")
    data = await get_all()
    # {
    #   'user': User(name='user', password='pass'),
    #   'user1': User(name='user1', password='password')
    # }
    print(data)


if __name__ == "__main__":
    import anyio

    anyio.run(test)

Why I wrote it.

  • Msgspec doesn't work as a sqlalchemy table on it's own and therefore there can't be any real competitor with SQLModel and why write a class object twice, it's unnessesary. the reason for this is that sqlalchemy can't make use of __slots__. This was an alternative approch in the longrun that at least gives msgspec some form of database functionality.

  • Windows doesn't have a saveable cache system with redis as far as I am aware so I gave it cysqlite and anyio to at least make up for that. As someone who hates OS-Gating, giving a proper database system for windows/linux and Apple devices was a priority for me.

  • I was requested by the aio-libs team that if there was another cache extension library it would have to be put somewhere else. So I ended up just making my own cache-like database with a better system to utilize. This one uses msgpack to help with the speed of decoding the objects however, I may add a feature that allows for json to be used instead. (This may lead to a new subclassable wrapper system and a new cache wrapper).

  • Compared to aiocache the set function adds or updates rather than one or the other. This just gets rid of another annoyance I had all together.

  • It doesn't take much practice at all to get used to using the library, even a beginner with a bit of knowlege on how anyio works could figure this out just fine. If you need something quick, dirty, or lazy, look no further than the cache function provided just for you that automatically opens and closes the database with the use of a single async function wrapper, you can even set the cache as a variable to wrap. For example: a factory attribute to save a bit of precious time and productivity (This was the ultimate goal after all...).

  • You get anyio support which means it will run on trio and asyncio eventloops.

  • Cysqlite is regularly updated and maintained alongside it's companion library anyio-cysqlite which is written by me, the same author of this library.

  • With anyio support, it is possible to use this library with litestar which has support for msgsepc by default. Examples may include captchas (with the use of a decently timed ttl) or user accounts by given name (recommended). TOTP tools for 2fa logins might also be a good one. The possibilities are endless.

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

keyspec-0.2.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keyspec-0.2.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file keyspec-0.2.0.tar.gz.

File metadata

  • Download URL: keyspec-0.2.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for keyspec-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7e50b3aba5a82b695e2821fd1fccd9fac09ecfb8c86566fc31084dcbcfbf35f7
MD5 e52013711e3a050feb83c78ecd535306
BLAKE2b-256 7e12a2726b5faa36f76256937e9836418d2027e8a06d1d00b866bebc694d0c81

See more details on using hashes here.

Provenance

The following attestation bundles were made for keyspec-0.2.0.tar.gz:

Publisher: release.yaml on Vizonex/keyspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file keyspec-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: keyspec-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for keyspec-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab0742f49a904e3922619a1b8fe34df407953bfe95e543b96e28ed51e3767949
MD5 e1dfee312797c5f40978c2783b627dde
BLAKE2b-256 90c4113888ca71c12eeba7992a10220f334280cfc2be1df0642b80c15aecb3a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for keyspec-0.2.0-py3-none-any.whl:

Publisher: release.yaml on Vizonex/keyspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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