Skip to content

Commit 722bff2

Browse files
committed
Cached properties
1 parent b3964a9 commit 722bff2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

nbviewer/app.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
from .log import log_request
4545
from .utils import git_info, jupyter_info, url_path_join
4646

47+
try: # Python 3.8
48+
from functools import cached_property
49+
except ImportError:
50+
from .utils import cached_property
51+
4752
#-----------------------------------------------------------------------------
4853
# Code
4954
#-----------------------------------------------------------------------------
@@ -115,13 +120,13 @@ def _load_max_cache_uris(self):
115120
def _load_static_url_prefix(self):
116121
return url_path_join(self.base_url, '/static/')
117122

118-
@property
123+
@cached_property
119124
def base_url(self):
120125
# prefer the JupyterHub defined service prefix over the CLI
121126
base_url = os.getenv("JUPYTERHUB_SERVICE_PREFIX", options.base_url)
122127
return base_url
123128

124-
@property
129+
@cached_property
125130
def cache(self):
126131
memcache_urls = os.environ.get('MEMCACHIER_SERVERS', os.environ.get('MEMCACHE_SERVERS'))
127132
# Handle linked Docker containers
@@ -154,14 +159,14 @@ def cache(self):
154159

155160
# for some reason this needs to be a computed property,
156161
# and not a traitlets Any(), otherwise nbviewer won't run
157-
@property
162+
@cached_property
158163
def client(self):
159164
AsyncHTTPClient.configure(HTTPClientClass)
160165
client = AsyncHTTPClient()
161166
client.cache = self.cache
162167
return client
163168

164-
@property
169+
@cached_property
165170
def env(self):
166171
env = Environment(loader=FileSystemLoader(self.template_paths), autoescape=True)
167172
env.filters['markdown'] = markdown.markdown
@@ -180,7 +185,7 @@ def env(self):
180185

181186
return env
182187

183-
@property
188+
@cached_property
184189
def fetch_kwargs(self):
185190
fetch_kwargs = dict(connect_timeout=10,)
186191
if options.proxy_host:
@@ -194,13 +199,13 @@ def fetch_kwargs(self):
194199

195200
return fetch_kwargs
196201

197-
@property
202+
@cached_property
198203
def formats(self):
199204
formats = configure_formats(options, self.config, log.app_log)
200205
return formats
201206

202207
# load frontpage sections
203-
@property
208+
@cached_property
204209
def frontpage_setup(self):
205210
with io.open(options.frontpage, 'r') as f:
206211
frontpage_setup = json.load(f)
@@ -213,20 +218,20 @@ def frontpage_setup(self):
213218
}
214219
return frontpage_setup
215220

216-
@property
221+
@cached_property
217222
def pool(self):
218223
if options.processes:
219224
pool = ProcessPoolExecutor(options.processes)
220225
else:
221226
pool = ThreadPoolExecutor(options.threads)
222227
return pool
223228

224-
@property
229+
@cached_property
225230
def rate_limiter(self):
226231
rate_limiter = RateLimiter(limit=options.rate_limit, interval=options.rate_limit_interval, cache=self.cache)
227232
return rate_limiter
228233

229-
@property
234+
@cached_property
230235
def template_paths(self):
231236
template_paths = pjoin(here, 'templates')
232237
if options.template_path is not None:

nbviewer/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from base64 import encodebytes
99
from base64 import decodebytes
1010

11+
from functools import lru_cache
12+
1113
import cgi
1214
from contextlib import contextmanager
1315
import re
@@ -247,3 +249,6 @@ def time_block(message, debug_limit=1):
247249
dt = time.time() - tic
248250
log = app_log.info if dt > debug_limit else app_log.debug
249251
log("%s in %.2f ms", message, 1e3 * dt)
252+
253+
def cached_property(method):
254+
return property(lru_cache(1)(method))

0 commit comments

Comments
 (0)