# HG changeset patch # User Søren Løvborg # Date 1457436486 -3600 # Node ID 82f818616265900aafc2d3267fec18c5b1e253c8 # Parent 6afa528ee30e0d1abcfbd4dbf8329090cca41ea0 db: cache SCM instance short-term (tied to SQLAlchemy session lifetime) Repeatedly checking whether SCM instances are invalidated is slow, and we don't actually _want_ SCM instances to invalidate half-way through a request either. Therefore cache them in on the db.Repository object, the lifetime of which is directly tied to the lifetime of the SQLAlchemy session, the lifetime of which is tied directly to the individual HTTP request. This way, we only check for invalidation the first time the SCM instance is accessed in a request. This will improve performance in cases where we have (by definition) badly written code that retrieves repo objects several times. diff -r 6afa528ee30e -r 82f818616265 kallithea/model/db.py --- a/kallithea/model/db.py Mon Mar 14 16:17:46 2016 +0100 +++ b/kallithea/model/db.py Tue Mar 08 12:28:06 2016 +0100 @@ -1437,9 +1437,13 @@ """ CacheInvalidation.set_invalidate(self.repo_name) + _scm_instance = None + @property def scm_instance(self): - return self.scm_instance_cached() + if self._scm_instance is None: + self._scm_instance = self.scm_instance_cached() + return self._scm_instance def scm_instance_cached(self, valid_cache_keys=None): @cache_region('long_term', 'scm_instance_cached')