Skip to content

Commit 29395e8

Browse files
committed
examples/deepzoom: share a tile cache among all multiserver slides
1 parent 5644229 commit 29395e8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

examples/deepzoom/deepzoom_multiserver.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# deepzoom_multiserver - Example web application for viewing multiple slides
44
#
55
# Copyright (c) 2010-2015 Carnegie Mellon University
6+
# Copyright (c) 2021 Benjamin Gilbert
67
#
78
# This library is free software; you can redistribute it and/or modify it
89
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -43,11 +44,12 @@
4344
else:
4445
import openslide
4546

46-
from openslide import OpenSlide, OpenSlideError
47+
from openslide import OpenSlide, OpenSlideCache, OpenSlideError
4748
from openslide.deepzoom import DeepZoomGenerator
4849

4950
SLIDE_DIR = '.'
5051
SLIDE_CACHE_SIZE = 10
52+
SLIDE_TILE_CACHE_MB = 128
5153
DEEPZOOM_FORMAT = 'jpeg'
5254
DEEPZOOM_TILE_SIZE = 254
5355
DEEPZOOM_OVERLAP = 1
@@ -60,11 +62,13 @@
6062

6163

6264
class _SlideCache:
63-
def __init__(self, cache_size, dz_opts):
65+
def __init__(self, cache_size, tile_cache_mb, dz_opts):
6466
self.cache_size = cache_size
6567
self.dz_opts = dz_opts
6668
self._lock = Lock()
6769
self._cache = OrderedDict()
70+
# Share a single tile cache among all slide handles
71+
self._tile_cache = OpenSlideCache(tile_cache_mb * 1024 * 1024)
6872

6973
def get(self, path):
7074
with self._lock:
@@ -75,6 +79,7 @@ def get(self, path):
7579
return slide
7680

7781
osr = OpenSlide(path)
82+
osr.set_cache(self._tile_cache)
7883
slide = DeepZoomGenerator(osr, **self.dz_opts)
7984
try:
8085
mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
@@ -121,7 +126,9 @@ def _setup():
121126
'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
122127
}
123128
opts = {v: app.config[k] for k, v in config_map.items()}
124-
app.cache = _SlideCache(app.config['SLIDE_CACHE_SIZE'], opts)
129+
app.cache = _SlideCache(
130+
app.config['SLIDE_CACHE_SIZE'], app.config['SLIDE_TILE_CACHE_MB'], opts
131+
)
125132

126133

127134
def _get_slide(path):

0 commit comments

Comments
 (0)