Show More
@@ -693,18 +693,27 b' class Repository(Base, BaseModel):' | |||
|
693 | 693 | def scm_instance(self): |
|
694 | 694 | return self.__get_instance() |
|
695 | 695 | |
|
696 | @property | |
|
697 | def scm_instance_cached(self): | |
|
696 | def scm_instance_cached(self, cache_map=None): | |
|
698 | 697 | @cache_region('long_term') |
|
699 | 698 | def _c(repo_name): |
|
700 | 699 | return self.__get_instance() |
|
701 | 700 | rn = self.repo_name |
|
702 | 701 | log.debug('Getting cached instance of repo') |
|
703 | inv = self.invalidate | |
|
704 | if inv is not None: | |
|
702 | ||
|
703 | if cache_map: | |
|
704 | # get using prefilled cache_map | |
|
705 | invalidate_repo = cache_map[self.repo_name] | |
|
706 | if invalidate_repo: | |
|
707 | invalidate_repo = (None if invalidate_repo.cache_active | |
|
708 | else invalidate_repo) | |
|
709 | else: | |
|
710 | # get from invalidate | |
|
711 | invalidate_repo = self.invalidate | |
|
712 | ||
|
713 | if invalidate_repo is not None: | |
|
705 | 714 | region_invalidate(_c, None, rn) |
|
706 | 715 | # update our cache |
|
707 | CacheInvalidation.set_valid(inv.cache_key) | |
|
716 | CacheInvalidation.set_valid(invalidate_repo.cache_key) | |
|
708 | 717 | return _c(rn) |
|
709 | 718 | |
|
710 | 719 | def __get_instance(self): |
@@ -1072,6 +1081,7 b' class CacheInvalidation(Base, BaseModel)' | |||
|
1072 | 1081 | __tablename__ = 'cache_invalidation' |
|
1073 | 1082 | __table_args__ = ( |
|
1074 | 1083 | UniqueConstraint('cache_key'), |
|
1084 | Index('key_idx', 'cache_key'), | |
|
1075 | 1085 | {'extend_existing': True, 'mysql_engine':'InnoDB', |
|
1076 | 1086 | 'mysql_charset': 'utf8'}, |
|
1077 | 1087 | ) |
@@ -1088,6 +1098,7 b' class CacheInvalidation(Base, BaseModel)' | |||
|
1088 | 1098 | def __unicode__(self): |
|
1089 | 1099 | return u"<%s('%s:%s')>" % (self.__class__.__name__, |
|
1090 | 1100 | self.cache_id, self.cache_key) |
|
1101 | ||
|
1091 | 1102 | @classmethod |
|
1092 | 1103 | def clear_cache(cls): |
|
1093 | 1104 | cls.query().delete() |
@@ -1117,7 +1128,7 b' class CacheInvalidation(Base, BaseModel)' | |||
|
1117 | 1128 | try: |
|
1118 | 1129 | inv_obj = CacheInvalidation(key, org_key) |
|
1119 | 1130 | Session.add(inv_obj) |
|
1120 | Session.commit() | |
|
1131 | #Session.commit() | |
|
1121 | 1132 | except Exception: |
|
1122 | 1133 | log.error(traceback.format_exc()) |
|
1123 | 1134 | Session.rollback() |
@@ -1174,6 +1185,40 b' class CacheInvalidation(Base, BaseModel)' | |||
|
1174 | 1185 | Session.add(inv_obj) |
|
1175 | 1186 | Session.commit() |
|
1176 | 1187 | |
|
1188 | @classmethod | |
|
1189 | def get_cache_map(cls): | |
|
1190 | ||
|
1191 | class cachemapdict(dict): | |
|
1192 | ||
|
1193 | def __init__(self, *args, **kwargs): | |
|
1194 | fixkey = kwargs.get('fixkey') | |
|
1195 | if fixkey: | |
|
1196 | del kwargs['fixkey'] | |
|
1197 | self.fixkey = fixkey | |
|
1198 | super(cachemapdict, self).__init__(*args, **kwargs) | |
|
1199 | ||
|
1200 | def __getattr__(self, name): | |
|
1201 | key = name | |
|
1202 | if self.fixkey: | |
|
1203 | key, _prefix, _org_key = cls._get_key(key) | |
|
1204 | if key in self.__dict__: | |
|
1205 | return self.__dict__[key] | |
|
1206 | else: | |
|
1207 | return self[key] | |
|
1208 | ||
|
1209 | def __getitem__(self, key): | |
|
1210 | if self.fixkey: | |
|
1211 | key, _prefix, _org_key = cls._get_key(key) | |
|
1212 | try: | |
|
1213 | return super(cachemapdict, self).__getitem__(key) | |
|
1214 | except KeyError: | |
|
1215 | return | |
|
1216 | ||
|
1217 | cache_map = cachemapdict(fixkey=True) | |
|
1218 | for obj in cls.query().all(): | |
|
1219 | cache_map[obj.cache_key] = cachemapdict(obj.get_dict()) | |
|
1220 | return cache_map | |
|
1221 | ||
|
1177 | 1222 | |
|
1178 | 1223 | class ChangesetComment(Base, BaseModel): |
|
1179 | 1224 | __tablename__ = 'changeset_comments' |
@@ -77,8 +77,12 b' class CachedRepoList(object):' | |||
|
77 | 77 | return '<%s (%s)>' % (self.__class__.__name__, self.__len__()) |
|
78 | 78 | |
|
79 | 79 | def __iter__(self): |
|
80 | # pre-propagated cache_map to save executing select statements | |
|
81 | # for each repo | |
|
82 | cache_map = CacheInvalidation.get_cache_map() | |
|
83 | ||
|
80 | 84 | for dbr in self.db_repo_list: |
|
81 | scmr = dbr.scm_instance_cached | |
|
85 | scmr = dbr.scm_instance_cached(cache_map) | |
|
82 | 86 | # check permission at this level |
|
83 | 87 | if not HasRepoPermissionAny( |
|
84 | 88 | 'repository.read', 'repository.write', 'repository.admin' |
General Comments 0
You need to be logged in to leave comments.
Login now