Skip to content

Commit

Permalink
byte-based size control of the tile cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jul 12, 2021
1 parent 621440d commit 45a176b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/aptus/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ async def home(request: Request):
}
return templates.TemplateResponse("mainpage.html", context)

# Cache of computed counts
cache = cachetools.LRUCache(50)
def run_in_executor(f):
# from https://stackoverflow.com/a/53719009/14343
@functools.wraps(f)
Expand All @@ -45,16 +43,20 @@ def inner(*args, **kwargs):
return loop.run_in_executor(None, lambda: f(*args, **kwargs))
return inner

# Cache of computed counts. One tile is about 830Kb.
cache_size = int(os.getenv("APTUS_CACHE", "500"))
tile_cache = cachetools.LRUCache(cache_size * 1_000_000, getsizeof=lambda nda: nda.nbytes)

@run_in_executor
def compute_tile(compute, cachekey):
old = cache.get(cachekey)
old = tile_cache.get(cachekey)
if old is None:
compute.compute_array()
else:
compute.set_counts(old)
pix = compute.color_mandel()
if old is None:
cache[cachekey] = compute.counts
tile_cache[cachekey] = compute.counts
im = PIL.Image.fromarray(pix)
fout = io.BytesIO()
compute.write_image(im, fout)
Expand Down

0 comments on commit 45a176b

Please sign in to comment.