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
4344else :
4445 import openslide
4546
46- from openslide import OpenSlide , OpenSlideError
47+ from openslide import OpenSlide , OpenSlideCache , OpenSlideError
4748from openslide .deepzoom import DeepZoomGenerator
4849
4950SLIDE_DIR = '.'
5051SLIDE_CACHE_SIZE = 10
52+ SLIDE_TILE_CACHE_MB = 128
5153DEEPZOOM_FORMAT = 'jpeg'
5254DEEPZOOM_TILE_SIZE = 254
5355DEEPZOOM_OVERLAP = 1
6062
6163
6264class _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
127134def _get_slide (path ):
0 commit comments