Skip to main content

dynamocacher

Description

A package which allows you to use dynamodb to cache output of a slow or expensive function, to reduce cost and increase the efficiency

Dax is supported using a Pynamodb fork, and its highly recommended for performance-critical application 50-100x performance difference is expected with dax

ToDos

  • Dax support : Done
  • Auto create table : Done
  • policy template : in progress
  • example readme : Done

Examples

colabNotebook

SampleUsage

cache this slow function

def power(input:dict):
  ''' 
  This is a very bad and slow function
  accept a dict containing these keys
    base: float
    power: int
  and return the power embarrasingly slowly
  response
    result: float
  '''
  base = input['base']
  power = input['power']
  result = base
  for i in range (power):
    result *= base
  return {
      'result':result
  }

def powerWithCaching(input:dict):
  cache = Cache.getCache(input, timeout = 3600)
  if cache: return cache
  result = power(input)
  Cache.addCache(input=input, output= result)
  return result

performance

Example code for trying it yourself

sampleInput = {
    'base':  1 + 1e-10,
    'power': 86289369
}

%timeit power(sampleInput) # 3.72 s ± 35 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit powerWithCaching(sampleInput) # 238 ms ± 482 µs without dax
%timeit powerWithCaching(sampleInput) # 5.43 ms with dax

SetUp

from dynamocacher.cacher import Cacher

class Cache(Cacher):
  class Meta:
      table_name = 'dynamoCache'
      region = 'us-east-1'
      aws_access_key_id = USER
      aws_secret_access_key = PW
      billing_mode= 'PAY_PER_REQUEST'
      dax_read_endpoints = ['....:8111'] # optional dax, note that dax will speed up dynamodb significantly
      dax_write_endpoints = ['....:8111']# optional dax, note that dax will speed up dynamodb significantly

createTable

Cache.create_table()

basic

addCache

sampleInput = {
    'query': 'testQuery',
    'fruit': 'strawberry'
}
Cache.addCache(
    input = sampleInput,
    output = sampleReturn
)

getCache

output = Cache.getCache(sampleInput, timeout = 3600) # ignore cache older than 3600 seconds

creating a table with sam

  Properties:
  TableName: dynamoCache
  PrimaryKey:
    Name: cacheKey
    Type: String
  Tags:
    Department: Engineering
    AppType: Serverless

Metadata

Release files for dynamocacher 0.0.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dynamocacher 0.0.18
File Size Uploaded Provenance
dynamocacher-0.0.18.tar.gz 3.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dynamocacher 0.0.18
File Interpreter ABI Platform
dynamocacher-0.0.18-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / dynamocacher-0.0.18.tar.gz

Download URL dynamocacher-0.0.18.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d266b06d676db4f3aedcad53851c093d30990bb58367742604ca52ff22ba7590
BLAKE2b-256 checksum
How to use checksums
0e5485be59c0a051974fb18bcded1f4ef83e7d1eabf90b64b74ffb0f2b00cd37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

Release files / dynamocacher-0.0.18-py3-none-any.whl

Download URL dynamocacher-0.0.18-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bc136b5d8280bf888840009febbf830597bb623334eb4dbd2610728b0d58584
BLAKE2b-256 checksum
How to use checksums
8d23b1c739eacfa42e3760bf3079909f2b42d97de864e504165042c6fceef038
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

Security for dynamocacher 0.0.18

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page