Skip to main content

Dead Simple Memory Cache

Project description

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

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

simple_memory_cache-1.0.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

simple_memory_cache-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file simple_memory_cache-1.0.0.tar.gz.

File metadata

  • Download URL: simple_memory_cache-1.0.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for simple_memory_cache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 97e1780001ef3117fe53c6012a4913a915007520e05e2120373725eff0041c2e
MD5 9f70e62e228e2a4afa974a97dd8883a3
BLAKE2b-256 e0a43f35ba91bb93eb3018a6d3cd4582e4d7ca48a0e8ba9f6f595086c1f9bfe7

See more details on using hashes here.

File details

Details for the file simple_memory_cache-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: simple_memory_cache-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for simple_memory_cache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a956c98408e63b04e06d2601123a8cb039217d0981cdcd79e0e083282cbb639
MD5 dc1c71df6d22673864d1b86cfbecabec
BLAKE2b-256 52bf59104c2827924a2505e19b2df906ede2728283f6c81729f5b5e78d7d2fbe

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