Skip to content

Commit 3695ae7

Browse files
author
bofm
committed
Updated README and examples
1 parent ea8cdeb commit 3695ae7

3 files changed

Lines changed: 59 additions & 28 deletions

File tree

README.rst

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ Usage
2121

2222
.. code:: python
2323
24-
from caching import cache
24+
from caching import Cache
25+
26+
# File-based cache with unlimited ttl and maximum of 128 cached elements
27+
@Cache(ttl=-1, maxsize=128, filepath='/tmp/mycache')
28+
def long_running_function(a, b, *args, c=None, **kwargs):
29+
pass
2530
26-
@cache(ttl=60, maxsize=128, filepath='/tmp/mycache')
31+
# Memory-based cache with limited ttl and maxsize
32+
@Cache(ttl=60, maxsize=128)
2733
def long_running_function(a, b, *args, c=None, **kwargs):
2834
pass
2935
@@ -34,33 +40,50 @@ Advanced usage
3440
3541
from caching import Cache
3642
37-
# Set default parameters
43+
# One cache for many functions
3844
3945
cache = Cache(filepath='/tmp/mycache', ttl=3600, maxsize=1024)
4046
41-
# Use default parameters
42-
4347
@cache
4448
def pow(x, y):
4549
return x**y
4650
47-
# Override default parameters
48-
49-
@cache(filepath=None, ttl=-1, maxsize=10000)
51+
@cache
5052
def factorial(n):
5153
if n == 0:
5254
return 1
5355
return n * factorial(n-1)
5456
57+
58+
# Custom cache key
59+
5560
def cache_key(x):
5661
return str(x)
5762
58-
@cache(ttl=-1, maxsize=10000, key=cache_key)
63+
cache = Cache(key=cache_key)
64+
call_count = 0
65+
66+
@cache
5967
def toupper(a):
68+
nonlocal call_count
69+
call_count += 1
6070
return str(a).upper()
6171
72+
@cache
73+
def tolower(a):
74+
nonlocal call_count
75+
call_count += 1
76+
return str(a).lower()
77+
78+
# The key function returns the same result for both 1 and '1'
79+
assert toupper('1') == toupper(1)
80+
assert call_count == 1
81+
82+
6283
# Using cache as a key-value store
6384
85+
cache = Cache()
86+
6487
try:
6588
result = cache[1]
6689
except KeyError:

caching/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .storage import CacheStorageBase, SQLiteStorage
33

44

5-
__version__ = '0.1.dev1'
5+
__version__ = '0.1.dev2'

tests/test_readme_examples.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
def test_readme(tempdirpath):
2-
from caching import Cache, cache
32

4-
# Simple usage
3+
from caching import Cache
54

6-
@cache(ttl=60, maxsize=128, filepath='/tmp/mycache')
7-
def long_running_function(a, b, *args, c=None, **kwargs):
8-
pass
9-
10-
long_running_function(1, 2, 3, 5, z=1)
11-
12-
# Set default parameters
5+
# One cache for many functions
136

147
cache = Cache(filepath=f'{tempdirpath}/mycache', ttl=3600, maxsize=1024)
158

16-
# Use default parameters
17-
189
@cache
1910
def pow(x, y):
20-
return x ** y
11+
return x**y
2112

22-
# Override default parameters
23-
24-
@cache(filepath=None, ttl=-1, maxsize=10000)
13+
@cache
2514
def factorial(n):
2615
if n == 0:
2716
return 1
2817
return n * factorial(n - 1)
2918

19+
# Custom cache key
20+
3021
def cache_key(x):
3122
return str(x)
3223

33-
@cache(ttl=-1, maxsize=10000, key=cache_key)
24+
cache = Cache(key=cache_key)
25+
call_count = 0
26+
27+
@cache
3428
def toupper(a):
29+
nonlocal call_count
30+
call_count += 1
3531
return str(a).upper()
3632

33+
@cache
34+
def tolower(a):
35+
nonlocal call_count
36+
call_count += 1
37+
return str(a).lower()
38+
39+
# The key function returns the same result for both 1 and '1'
40+
assert toupper('1') == toupper(1)
41+
assert call_count == 1
42+
3743
# Using cache as a key-value store
3844

39-
def calculate_result(*args):
40-
return args * 2
45+
cache = Cache()
4146

4247
try:
4348
result = cache[1]
4449
except KeyError:
50+
def calculate_result(x):
51+
return x
52+
4553
result = calculate_result(1)
4654
cache[1] = result
4755
assert 1 in cache

0 commit comments

Comments
 (0)