-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
45 lines (32 loc) · 1.21 KB
/
example.py
File metadata and controls
45 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import boto3
from flexicache import Cache, MemoryCache, FileCache, DDBCache
# pip install flexicache
def exmaple_mem():
# Memory caching
memory_cache = MemoryCache()
@Cache(memory_cache)
def memory_cached_function(a, b):
return a + b
result = memory_cached_function(2, 2)
print(f"memory_cached_function(2, 2) = {result}")
assert result == 2 + 2
def exmaple_file():
# Local file system caching
file_cache = FileCache('cache.json')
@Cache(file_cache)
def file_cached_function(a, b):
return a * b
result = file_cached_function(2, 2)
print(f"file_cached_function(2, 2) = {result}")
assert result == 2 * 2
def exmaple_ddb():
# AWS DynamoDB caching
aws_ddb_client = boto3.client('dynamodb',
region_name='eu-north-1') # , aws_access_key_id='your-access-key', aws_secret_access_key='your-secret-key')
ddb_cache = DDBCache(aws_ddb_client, table_name='example_cache_table', cache_seconds=900)
@Cache(ddb_cache)
def ddb_cached_function(a, b):
return a ** b
result = ddb_cached_function(1, 2)
print(f"ddb_cached_function(1, 2) = {result}")
assert result == 1 ** 2