##// END OF EJS Templates
caches: store computation time inside context manager as helper. Since the with block is full...
marcink -
r2936:776b3361 default
parent child Browse files
Show More
@@ -17,7 +17,6 b''
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 import time
21 import pytz
20 import pytz
22 import logging
21 import logging
23
22
@@ -159,7 +158,6 b' class RepoFeedView(RepoAppView):'
159
158
160 return feed.mime_type, feed.writeString('utf-8')
159 return feed.mime_type, feed.writeString('utf-8')
161
160
162 start = time.time()
163 inv_context_manager = rc_cache.InvalidationContext(
161 inv_context_manager = rc_cache.InvalidationContext(
164 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
162 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
165 with inv_context_manager as invalidation_context:
163 with inv_context_manager as invalidation_context:
@@ -171,8 +169,9 b' class RepoFeedView(RepoAppView):'
171
169
172 mime_type, feed = generate_atom_feed(
170 mime_type, feed = generate_atom_feed(
173 self.db_repo.repo_id, self.db_repo.repo_name, 'atom')
171 self.db_repo.repo_id, self.db_repo.repo_name, 'atom')
174 compute_time = time.time() - start
172
175 log.debug('Repo ATOM feed computed in %.3fs', compute_time)
173 log.debug('Repo ATOM feed computed in %.3fs',
174 inv_context_manager.compute_time)
176
175
177 response = Response(feed)
176 response = Response(feed)
178 response.content_type = mime_type
177 response.content_type = mime_type
@@ -224,7 +223,6 b' class RepoFeedView(RepoAppView):'
224
223
225 return feed.mime_type, feed.writeString('utf-8')
224 return feed.mime_type, feed.writeString('utf-8')
226
225
227 start = time.time()
228 inv_context_manager = rc_cache.InvalidationContext(
226 inv_context_manager = rc_cache.InvalidationContext(
229 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
227 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
230 with inv_context_manager as invalidation_context:
228 with inv_context_manager as invalidation_context:
@@ -236,8 +234,8 b' class RepoFeedView(RepoAppView):'
236
234
237 mime_type, feed = generate_rss_feed(
235 mime_type, feed = generate_rss_feed(
238 self.db_repo.repo_id, self.db_repo.repo_name, 'rss')
236 self.db_repo.repo_id, self.db_repo.repo_name, 'rss')
239 compute_time = time.time() - start
237 log.debug(
240 log.debug('Repo RSS feed computed in %.3fs', compute_time)
238 'Repo RSS feed computed in %.3fs', inv_context_manager.compute_time)
241
239
242 response = Response(feed)
240 response = Response(feed)
243 response.content_type = mime_type
241 response.content_type = mime_type
@@ -18,7 +18,6 b''
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import time
22 import logging
21 import logging
23 import string
22 import string
24 import rhodecode
23 import rhodecode
@@ -86,7 +85,6 b' class RepoSummaryView(RepoAppView):'
86 readme_filename = readme_node.path
85 readme_filename = readme_node.path
87 return readme_data, readme_filename
86 return readme_data, readme_filename
88
87
89 start = time.time()
90 inv_context_manager = rc_cache.InvalidationContext(
88 inv_context_manager = rc_cache.InvalidationContext(
91 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
89 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
92 with inv_context_manager as invalidation_context:
90 with inv_context_manager as invalidation_context:
@@ -98,8 +96,10 b' class RepoSummaryView(RepoAppView):'
98
96
99 instance = generate_repo_readme(
97 instance = generate_repo_readme(
100 db_repo.repo_id, db_repo.repo_name, renderer_type)
98 db_repo.repo_id, db_repo.repo_name, renderer_type)
101 compute_time = time.time() - start
99
102 log.debug('Repo readme generated and computed in %.3fs', compute_time)
100 log.debug(
101 'Repo readme generated and computed in %.3fs',
102 inv_context_manager.compute_time)
103 return instance
103 return instance
104
104
105 def _get_landing_commit_or_none(self, db_repo):
105 def _get_landing_commit_or_none(self, db_repo):
@@ -18,6 +18,7 b''
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 import os
20 import os
21 import time
21 import logging
22 import logging
22 import functools
23 import functools
23 import threading
24 import threading
@@ -210,23 +211,19 b' class InvalidationContext(object):'
210 """
211 """
211 usage::
212 usage::
212
213
213 import time
214 from rhodecode.lib import rc_cache
214 from rhodecode.lib import rc_cache
215 my_id = 1
215
216 cache_namespace_uid = 'cache_demo.{}'.format(my_id)
216 cache_namespace_uid = CacheKey.SOME_NAMESPACE.format(1)
217 invalidation_namespace = 'repo_cache:1'
218 region = rc_cache.get_or_create_region('cache_perms', cache_namespace_uid)
217 region = rc_cache.get_or_create_region('cache_perms', cache_namespace_uid)
219
218
220 @region.conditional_cache_on_arguments(namespace=cache_namespace_uid,
219 @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, condition=True)
221 expiration_time=30,
222 condition=True)
223 def heavy_compute(cache_name, param1, param2):
220 def heavy_compute(cache_name, param1, param2):
224 print('COMPUTE {}, {}, {}'.format(cache_name, param1, param2))
221 print('COMPUTE {}, {}, {}'.format(cache_name, param1, param2))
225 import time
226 time.sleep(30)
227 return True
228
222
229 start = time.time()
223 # invalidation namespace is shared namespace key for all process caches
224 # we use it to send a global signal
225 invalidation_namespace = 'repo_cache:1'
226
230 inv_context_manager = rc_cache.InvalidationContext(
227 inv_context_manager = rc_cache.InvalidationContext(
231 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
228 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
232 with inv_context_manager as invalidation_context:
229 with inv_context_manager as invalidation_context:
@@ -236,7 +233,7 b' class InvalidationContext(object):'
236 heavy_compute.invalidate('some_name', 'param1', 'param2')
233 heavy_compute.invalidate('some_name', 'param1', 'param2')
237
234
238 result = heavy_compute('some_name', 'param1', 'param2')
235 result = heavy_compute('some_name', 'param1', 'param2')
239 compute_time = time.time() - start
236 compute_time = inv_context_manager.compute_time
240 print(compute_time)
237 print(compute_time)
241
238
242 # To send global invalidation signal, simply run
239 # To send global invalidation signal, simply run
@@ -268,6 +265,7 b' class InvalidationContext(object):'
268 self.cache_key = compute_key_from_params(uid)
265 self.cache_key = compute_key_from_params(uid)
269 self.cache_key = 'proc:{}_thread:{}_{}'.format(
266 self.cache_key = 'proc:{}_thread:{}_{}'.format(
270 self.proc_id, self.thread_id, self.cache_key)
267 self.proc_id, self.thread_id, self.cache_key)
268 self.compute_time = 0
271
269
272 def get_or_create_cache_obj(self, uid, invalidation_namespace=''):
270 def get_or_create_cache_obj(self, uid, invalidation_namespace=''):
273 log.debug('Checking if %s cache key is present and active', self.cache_key)
271 log.debug('Checking if %s cache key is present and active', self.cache_key)
@@ -284,20 +282,23 b' class InvalidationContext(object):'
284 """
282 """
285 # register or get a new key based on uid
283 # register or get a new key based on uid
286 self.cache_obj = self.get_or_create_cache_obj(uid=self.uid)
284 self.cache_obj = self.get_or_create_cache_obj(uid=self.uid)
287
285 self._start_time = time.time()
288 if self.cache_obj.cache_active:
286 if self.cache_obj.cache_active:
289 # means our cache obj is existing and marked as it's
287 # means our cache obj is existing and marked as it's
290 # cache is not outdated, we return ActiveRegionCache
288 # cache is not outdated, we return ActiveRegionCache
291 self.skip_cache_active_change = True
289 self.skip_cache_active_change = True
290
292 return ActiveRegionCache(context=self)
291 return ActiveRegionCache(context=self)
293
292
294 # the key is either not existing or set to False, we return
293 # the key is either not existing or set to False, we return
295 # the real invalidator which re-computes value. We additionally set
294 # the real invalidator which re-computes value. We additionally set
296 # the flag to actually update the Database objects
295 # the flag to actually update the Database objects
297 self.skip_cache_active_change = False
296 self.skip_cache_active_change = False
298 return FreshRegionCache(context=self)
297 return FreshRegionCache(context=self)
299
298
300 def __exit__(self, exc_type, exc_val, exc_tb):
299 def __exit__(self, exc_type, exc_val, exc_tb):
300 # save compute time
301 self.compute_time = time.time() - self._start_time
301
302
302 if self.skip_cache_active_change:
303 if self.skip_cache_active_change:
303 return
304 return
@@ -2339,7 +2339,6 b' class Repository(Base, BaseModel):'
2339 def get_instance_cached(repo_id):
2339 def get_instance_cached(repo_id):
2340 return self._get_instance()
2340 return self._get_instance()
2341
2341
2342 start = time.time()
2343 inv_context_manager = rc_cache.InvalidationContext(
2342 inv_context_manager = rc_cache.InvalidationContext(
2344 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
2343 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
2345 with inv_context_manager as invalidation_context:
2344 with inv_context_manager as invalidation_context:
@@ -2349,8 +2348,8 b' class Repository(Base, BaseModel):'
2349 get_instance_cached.invalidate(self.repo_id)
2348 get_instance_cached.invalidate(self.repo_id)
2350
2349
2351 instance = get_instance_cached(self.repo_id)
2350 instance = get_instance_cached(self.repo_id)
2352 compute_time = time.time() - start
2351 log.debug(
2353 log.debug('Repo instance fetched in %.3fs', compute_time)
2352 'Repo instance fetched in %.3fs', inv_context_manager.compute_time)
2354 return instance
2353 return instance
2355
2354
2356 def _get_instance(self, cache=True, config=None):
2355 def _get_instance(self, cache=True, config=None):
General Comments 0
You need to be logged in to leave comments. Login now