Simple Memory Cache
A dead simple memory cache to allow a variable to easily be lazy-loaded when needed. Only 43 lines of code and no dependencies.
This module is used by our SpinHub App (https://www.spinhub.ca) which is currently in development. It allows us to in-memory cache variables for re-use in successful AWS Lambda function executions.
Use-case 1: Caching responses
A specific use case for us is to cache de JWKS (the JSON Web Key Set) of our Auth0 domain:
import requests
from simple_memory_cache import GLOBAL_CACHE
AUTH0_DOMAIN = 'mysubdomain.auth0.com'
JWKS_URL = AUTH0_DOMAIN + '/.well-known/jwks.json'
jwks_var = GLOBAL_CACHE.MemoryCachedVar('jwks')
@jwks.on_first_access
def _retrieve_jwks():
return requests.get(JWKS_URL).json()
def do_something():
# First call will trigger _retrieve_jwks and store the return value.
# If this is called within the same AWS Lambda instance, the global context will still be there
# and the value will simply be returned
jwks = jwks_var.get()
try:
(validate JWT token w/ jwks)
except (ValidationError):
# On a validation error, Auth0 recommends to refetch the JWKS in case it changed.
# We invalidate the value and get it once again.
jwks_var.invalidate()
jwks = jwks_var.get()
(validate JWT token w/ jwks)
Use-case 2: Specific Implementation for Flask Globals
We often encounter the case where we wish to set a Flask g member only if it is not set. You can create your own MemoryCachedVar adapted to this case in 5 lines of code:
from simple_memory_cache import CachedVar, NO_VALUE_STORED
from flask import g, request
class FlaskGCache(CachedVar):
def _get_stored_value(self):
return getattr(g, self.name, NO_VALUE_STORED)
def _set_stored_value(self, value):
setattr(g, self.name, value)
app = (...)
user_var = FlaskGCache('user')
@user_var.on_first_access
def retrieve_user_from_request():
token = request.headers.get('Authorization', None)
# ... Decode token or raise Unauthorized
return user
@app.route()
def private_route():
user = user_var.get() # Authorizes the user once, will raise Unauthorized if unable.
# ...
user = user_var.get() # Simply returns the value stored in g.user
Creators
Created by Tack Verification, a company dedicated to reducing operational costs related to hardware systems verification. https://www.tackv.ca
Metadata
Release files for simple-memory-cache 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_memory_cache-1.0.0.tar.gz | 3.0 kB | Without provenance | Details for simple_memory_cache-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_memory_cache-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for simple_memory_cache-1.0.0-py3-none-any.whl |
Total release size: 7.0 kB
Release files / simple_memory_cache-1.0.0.tar.gz
| Download URL | simple_memory_cache-1.0.0.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97e1780001ef3117fe53c6012a4913a915007520e05e2120373725eff0041c2e
|
|
BLAKE2b-256 checksum How to use checksums |
e0a43f35ba91bb93eb3018a6d3cd4582e4d7ca48a0e8ba9f6f595086c1f9bfe7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
|
Release files / simple_memory_cache-1.0.0-py3-none-any.whl
| Download URL | simple_memory_cache-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a956c98408e63b04e06d2601123a8cb039217d0981cdcd79e0e083282cbb639
|
|
BLAKE2b-256 checksum How to use checksums |
52bf59104c2827924a2505e19b2df906ede2728283f6c81729f5b5e78d7d2fbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
|
Security for simple-memory-cache 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.