##// END OF EJS Templates
caches: use optional identity cache options on get_by_repo_name and get_by_username...
marcink -
r255:a979e37d default
parent child Browse files
Show More
@@ -220,6 +220,20 b' class BaseModel(object):'
220 obj = cls.query().get(id_)
220 obj = cls.query().get(id_)
221 Session().delete(obj)
221 Session().delete(obj)
222
222
223 @classmethod
224 def identity_cache(cls, session, attr_name, value):
225 exist_in_session = []
226 for (item_cls, pkey), instance in session.identity_map.items():
227 if cls == item_cls and getattr(instance, attr_name) == value:
228 exist_in_session.append(instance)
229 if exist_in_session:
230 if len(exist_in_session) == 1:
231 return exist_in_session[0]
232 log.exception(
233 'multiple objects with attr %s and '
234 'value %s found with same name: %r',
235 attr_name, value, exist_in_session)
236
223 def __repr__(self):
237 def __repr__(self):
224 if hasattr(self, '__unicode__'):
238 if hasattr(self, '__unicode__'):
225 # python repr needs to return str
239 # python repr needs to return str
@@ -639,16 +653,26 b' class User(Base, BaseModel):'
639 log.error(traceback.format_exc())
653 log.error(traceback.format_exc())
640
654
641 @classmethod
655 @classmethod
642 def get_by_username(cls, username, case_insensitive=False, cache=False):
656 def get_by_username(cls, username, case_insensitive=False,
657 cache=False, identity_cache=False):
658 session = Session()
659
643 if case_insensitive:
660 if case_insensitive:
644 q = cls.query().filter(func.lower(cls.username) == func.lower(username))
661 q = cls.query().filter(
662 func.lower(cls.username) == func.lower(username))
645 else:
663 else:
646 q = cls.query().filter(cls.username == username)
664 q = cls.query().filter(cls.username == username)
647
665
648 if cache:
666 if cache:
649 q = q.options(FromCache(
667 if identity_cache:
650 "sql_cache_short",
668 val = cls.identity_cache(session, 'username', username)
651 "get_user_%s" % _hash_key(username)))
669 if val:
670 return val
671 else:
672 q = q.options(
673 FromCache("sql_cache_short",
674 "get_user_by_name_%s" % _hash_key(username)))
675
652 return q.scalar()
676 return q.scalar()
653
677
654 @classmethod
678 @classmethod
@@ -1364,7 +1388,7 b' class Repository(Base, BaseModel):'
1364 def normalize_repo_name(cls, repo_name):
1388 def normalize_repo_name(cls, repo_name):
1365 """
1389 """
1366 Normalizes os specific repo_name to the format internally stored inside
1390 Normalizes os specific repo_name to the format internally stored inside
1367 dabatabase using URL_SEP
1391 database using URL_SEP
1368
1392
1369 :param cls:
1393 :param cls:
1370 :param repo_name:
1394 :param repo_name:
@@ -1372,19 +1396,20 b' class Repository(Base, BaseModel):'
1372 return cls.NAME_SEP.join(repo_name.split(os.sep))
1396 return cls.NAME_SEP.join(repo_name.split(os.sep))
1373
1397
1374 @classmethod
1398 @classmethod
1375 def get_by_repo_name(cls, repo_name):
1399 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1376 session = Session()
1400 session = Session()
1377 exist_in_session = []
1378 for (item_cls, pkey), instance in session.identity_map.items():
1379 if cls == item_cls and instance.repo_name == repo_name:
1380 exist_in_session.append(instance)
1381 if exist_in_session:
1382 if len(exist_in_session) == 1:
1383 return exist_in_session[0]
1384 log.exception(
1385 'multiple repos with same name: %r' % exist_in_session)
1386
1387 q = session.query(cls).filter(cls.repo_name == repo_name)
1401 q = session.query(cls).filter(cls.repo_name == repo_name)
1402
1403 if cache:
1404 if identity_cache:
1405 val = cls.identity_cache(session, 'repo_name', repo_name)
1406 if val:
1407 return val
1408 else:
1409 q = q.options(
1410 FromCache("sql_cache_short",
1411 "get_repo_by_name_%s" % _hash_key(repo_name)))
1412
1388 return q.scalar()
1413 return q.scalar()
1389
1414
1390 @classmethod
1415 @classmethod
General Comments 0
You need to be logged in to leave comments. Login now