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