Skip to content

Commit

Permalink
caching in python
Browse files Browse the repository at this point in the history
  • Loading branch information
tebeka committed Aug 26, 2024
1 parent f188854 commit f0675dc
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 0 deletions.
15 changes: 15 additions & 0 deletions py-cache/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from functools import wraps


def cached(fn):
cache = {} # args -> value

@wraps(fn)
def wrapper(*args, **kw):
if args in cache:
return cache[args]

value = cache[args] = fn(*args)
return value

return wrapper
7 changes: 7 additions & 0 deletions py-cache/caching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Caching

Save a computation result and using it later. AKA memoization.

You trade memory consumption for performance.

Works on "pure" functions that don't have side effects.
1 change: 1 addition & 0 deletions py-cache/current.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13
6 changes: 6 additions & 0 deletions py-cache/end.ipy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os

!kitty +icat book-covers.png
width = os.get_terminal_size().columns
url = 'https://pragprog.com/search/?q=miki+tebeka'
print(f'{url:^{width}}')
3 changes: 3 additions & 0 deletions py-cache/four.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
There's two hard problems in computer science: We only have one joke and it's not funny.

- Phillip Scott Bowden
3 changes: 3 additions & 0 deletions py-cache/header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Caching in Python
[Miki Tebeka](mailto:[email protected])

14 changes: 14 additions & 0 deletions py-cache/libs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Use Libraries


The "Not Invented Here" (NIH) Syndrome is the tendency to reject ideas that come from "outside".
And unfortunately, "outside" can still apply to solutions that come from other teams within the organization.

- functools.lru_cache
- https://docs.python.org/3/library/functools.html#functools.lru_cache
- joblib.Memory
- https://joblib.readthedocs.io/en/latest/memory.html#memory
- Redis/Memcached
- https://redis.io/
- https://memcached.org/

39 changes: 39 additions & 0 deletions py-cache/next.ipy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
current = 'current.txt'

try:
with open(current) as fp:
i = int(fp.read())
except OSError:
i = 0

!clear
if i == 0:
!glow caching.md
elif i == 1:
!source-highlight -n -f esc -s python -i user.py
%run user.py
elif i == 2:
!source-highlight -n -f esc -s python -i cache.py
elif i == 3:
!source-highlight -n -f esc -s python -i user_cached.py
%run user_cached.py
elif i == 4:
!glow two.md
elif i == 5:
!glow three.md
elif i == 6:
!glow four.md
elif i == 7:
!glow libs.md
elif i == 8:
!source-highlight -n -f esc -s python -i user_lru.py
%run user_lru.py
elif i == 9:
!source-highlight -n -f esc -s python -i user_mem.py
%run user_mem.py
else:
echo 'Thank You!' | cowsay | lolcat -a -d 3


with open(current, 'w') as out:
print(i+1, file=out, end='')
5 changes: 5 additions & 0 deletions py-cache/playbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%run setup.py

%timeit get_user('bruce')

In cache show get_user(['bruce'])
5 changes: 5 additions & 0 deletions py-cache/setup.ipy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%alias_magic -p next.ipy next %run
%alias_magic -p end.ipy end %run
!rm -f current.txt
!clear
!glow header.md
3 changes: 3 additions & 0 deletions py-cache/three.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

- Leon Bambrick
3 changes: 3 additions & 0 deletions py-cache/two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
There are only two hard things in Computer Science: cache invalidation and naming things.

- Phil Karlton
14 changes: 14 additions & 0 deletions py-cache/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass
from time import sleep

@dataclass
class User:
login: str
name: str
roles: list[str]


def get_user(login):
sleep(0.1) # simulate DB access
return User(login, 'Bruce', ['ADMIN'])

17 changes: 17 additions & 0 deletions py-cache/user_cached.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass
from time import sleep

from cache import cached

@dataclass
class User:
login: str
name: str
roles: list[str]


@cached
def get_user(login):
sleep(0.1) # simulate DB access
return User(login, 'Bruce', ['ADMIN'])

17 changes: 17 additions & 0 deletions py-cache/user_lru.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass
from time import sleep

from functools import lru_cache

@dataclass
class User:
login: str
name: str
roles: list[str]


@lru_cache(1024)
def get_user(login):
sleep(0.1) # simulate DB access
return User(login, 'Bruce', ['ADMIN'])

19 changes: 19 additions & 0 deletions py-cache/user_mem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import dataclass
from time import sleep

from joblib import Memory

memory = Memory('/tmp/users', verbose=False)

@dataclass
class User:
login: str
name: str
roles: list[str]


@memory.cache
def get_user(login):
sleep(0.1) # simulate DB access
return User(login, 'Bruce', ['ADMIN'])

0 comments on commit f0675dc

Please sign in to comment.