Skip to main content

a simplistic asynchronous memory cache library for use with msgspec dataclasses

Project description

keyspec

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

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 instert(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 instert("user", "pass")
    await instert("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 event set it as a variable ex: 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.1.0.tar.gz (8.9 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.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for keyspec-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c9c6cbf139bdd550aec65740ac050b144fb40bb1848b859e578ec08ba12e85cb
MD5 65b5d95df99c1e2f587dbbdc33d25beb
BLAKE2b-256 dd494ba5663d09407315938ba2fd0e3057548256f7d7b3bfa93988893b347277

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for keyspec-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76644e4be70f7f2447c7e172ffbe1857baffdbb17afb3d9fde541dfd364420b3
MD5 8e963d105c6a7284fd2c6f53b90481d3
BLAKE2b-256 55c3493c9c68e036890a2949163a8a2f45d1cf26d9f1d38132b5e1593c0318c5

See more details on using hashes here.

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