Skip to main content

simple memoization with sqlite3 and expiry

Project description

Hippocampus

working memory for your python code

Hippocampus is a small library that persists function calls in a sqlite3 database. It can be used through a(parameterized) decorator which caches calls and results.

Usage

from hippocampus import memoize


@memoize(remember="forever", db_path="mydb.sqlite3")
def my_expensive_function(arg, kwarg):
    pass

# first call is a regular function call
result = my_expensive_function("arg", kwarg={"a": "dict"})

# second call is a cached read from db
result = my_expensive_function("arg", kwarg={"a": "dict"})

Parameters

memoize() takes three parameters:

  • remember: How long to cache the result
  • db_path: Set path for the database file. If not set, the user's cache directry (same as for pip) is used
  • overwrite: Debug option to force overwriting existing records. May be useful if your code changed.

There is one (required) parameter remember which is used to set expiry of cached items. The default is to not expire (keep forever). Other values may be given in human readable terms and are parsed by dateparser. This means you can use either absolute (remember="2023-12-01") or relative (remember="two minutes") date specifications. Note that the parameter means you will need to include parens on the decorator even if you omit remember: Always use @memoize().

Limitations

The decorator is intentionally simple to remove complexity and only accepts functions with json-compatible args and outputs. You can use any json-compatible type for arguments and keyword arguments, but the return value needs to be a json-compatible dictionary. In addition to the regular allowed types (str, int, list, dict), you can also provide date/time objects, which will be serizalized and deserialized by the orjson library.

Only straight return calls are supported; you cannot memoize a generator. This may change in the future, but requires some db modeling considerations.

Finally, values are currently never deleted, even if they expire. Your memoization database may thus grow to be rather large. Expired calls are overwritten when invoked again, thus calling your memoized function after the expiry (even with a different expiry setting) refreshes the old record.

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

hippocampus-0.1.4.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

hippocampus-0.1.4-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hippocampus-0.1.4.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.1.0

File hashes

Hashes for hippocampus-0.1.4.tar.gz
Algorithm Hash digest
SHA256 82eb4b72cca75c203c86333bfd41e89b023880a21061a62bf3bf8fb2c43b6146
MD5 13ebab171f5bc5884360404f4f3ff628
BLAKE2b-256 30f093863fe0294b9f683ac34474522c70fe03e84ca135cb2782a2538cf04dbc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hippocampus-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.1.0

File hashes

Hashes for hippocampus-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 55a627561602426470a2e9a1bd8e84c750191aeba898eb71875151ce21187857
MD5 50843e4701be79e7c5d4bd45a2668ea6
BLAKE2b-256 a6015ddfbba810d563a642c4ae86891d690c350268d5e0c31cf973d4ef59ddc5

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