storing cache using dynamodb dax
Project description
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
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dynamocacher-0.0.18.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file dynamocacher-0.0.18.tar.gz
.
File metadata
- Download URL: dynamocacher-0.0.18.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d266b06d676db4f3aedcad53851c093d30990bb58367742604ca52ff22ba7590 |
|
MD5 | ede5ed0f61ccd3aac8cca5cd490ea3c1 |
|
BLAKE2b-256 | 0e5485be59c0a051974fb18bcded1f4ef83e7d1eabf90b64b74ffb0f2b00cd37 |
File details
Details for the file dynamocacher-0.0.18-py3-none-any.whl
.
File metadata
- Download URL: dynamocacher-0.0.18-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bc136b5d8280bf888840009febbf830597bb623334eb4dbd2610728b0d58584 |
|
MD5 | deae53d5c37774298cdaf6b2a0ca9c74 |
|
BLAKE2b-256 | 8d23b1c739eacfa42e3760bf3079909f2b42d97de864e504165042c6fceef038 |