Show More
@@ -141,10 +141,9 b' class FeedController(BaseRepoController)' | |||||
141 | return feed.writeString('utf-8') |
|
141 | return feed.writeString('utf-8') | |
142 |
|
142 | |||
143 | key = repo_name + '_ATOM' |
|
143 | key = repo_name + '_ATOM' | |
144 |
|
|
144 | valid = CacheInvalidation.test_and_set_valid(key) | |
145 |
if |
|
145 | if not valid: | |
146 | region_invalidate(_get_feed_from_cache, None, key) |
|
146 | region_invalidate(_get_feed_from_cache, None, key) | |
147 | CacheInvalidation.set_valid(inv.cache_key) |
|
|||
148 | return _get_feed_from_cache(key) |
|
147 | return _get_feed_from_cache(key) | |
149 |
|
148 | |||
150 | def rss(self, repo_name): |
|
149 | def rss(self, repo_name): | |
@@ -174,8 +173,7 b' class FeedController(BaseRepoController)' | |||||
174 | return feed.writeString('utf-8') |
|
173 | return feed.writeString('utf-8') | |
175 |
|
174 | |||
176 | key = repo_name + '_RSS' |
|
175 | key = repo_name + '_RSS' | |
177 |
|
|
176 | valid = CacheInvalidation.test_and_set_valid(key) | |
178 |
if |
|
177 | if not valid: | |
179 | region_invalidate(_get_feed_from_cache, None, key) |
|
178 | region_invalidate(_get_feed_from_cache, None, key) | |
180 | CacheInvalidation.set_valid(inv.cache_key) |
|
|||
181 | return _get_feed_from_cache(key) |
|
179 | return _get_feed_from_cache(key) |
@@ -88,7 +88,6 b' class SummaryController(BaseRepoControll' | |||||
88 |
|
88 | |||
89 | return download_l |
|
89 | return download_l | |
90 |
|
90 | |||
91 |
|
||||
92 | def __get_readme_data(self, db_repo): |
|
91 | def __get_readme_data(self, db_repo): | |
93 | repo_name = db_repo.repo_name |
|
92 | repo_name = db_repo.repo_name | |
94 |
|
93 | |||
@@ -126,10 +125,9 b' class SummaryController(BaseRepoControll' | |||||
126 | return readme_data, readme_file |
|
125 | return readme_data, readme_file | |
127 |
|
126 | |||
128 | key = repo_name + '_README' |
|
127 | key = repo_name + '_README' | |
129 |
|
|
128 | valid = CacheInvalidation.test_and_set_valid(key) | |
130 |
if |
|
129 | if not valid: | |
131 | region_invalidate(_get_readme_from_cache, None, key) |
|
130 | region_invalidate(_get_readme_from_cache, None, key) | |
132 | CacheInvalidation.set_valid(inv.cache_key) |
|
|||
133 | return _get_readme_from_cache(key) |
|
131 | return _get_readme_from_cache(key) | |
134 |
|
132 | |||
135 | @LoginRequired() |
|
133 | @LoginRequired() |
@@ -476,7 +476,7 b' def repo2db_mapper(initial_repo_list, re' | |||||
476 | # system, this will register all repos and multiple instances |
|
476 | # system, this will register all repos and multiple instances | |
477 | cache_key = CacheInvalidation._get_cache_key(name) |
|
477 | cache_key = CacheInvalidation._get_cache_key(name) | |
478 | log.debug("Creating invalidation cache key for %s: %s", name, cache_key) |
|
478 | log.debug("Creating invalidation cache key for %s: %s", name, cache_key) | |
479 |
CacheInvalidation. |
|
479 | CacheInvalidation.test_and_set_valid(name) | |
480 |
|
480 | |||
481 | sa.commit() |
|
481 | sa.commit() | |
482 | removed = [] |
|
482 | removed = [] |
@@ -1163,10 +1163,6 b' class Repository(Base, BaseModel):' | |||||
1163 | # SCM CACHE INSTANCE |
|
1163 | # SCM CACHE INSTANCE | |
1164 | #========================================================================== |
|
1164 | #========================================================================== | |
1165 |
|
1165 | |||
1166 | @property |
|
|||
1167 | def invalidate(self): |
|
|||
1168 | return CacheInvalidation.invalidate(self.repo_name) |
|
|||
1169 |
|
||||
1170 | def set_invalidate(self): |
|
1166 | def set_invalidate(self): | |
1171 | """ |
|
1167 | """ | |
1172 | Mark caches of this repo as invalid. |
|
1168 | Mark caches of this repo as invalid. | |
@@ -1184,27 +1180,16 b' class Repository(Base, BaseModel):' | |||||
1184 | return self.scm_instance_cached() |
|
1180 | return self.scm_instance_cached() | |
1185 | return self.__get_instance() |
|
1181 | return self.__get_instance() | |
1186 |
|
1182 | |||
1187 |
def scm_instance_cached(self, cache_ |
|
1183 | def scm_instance_cached(self, valid_cache_keys=None): | |
1188 | @cache_region('long_term') |
|
1184 | @cache_region('long_term') | |
1189 | def _c(repo_name): |
|
1185 | def _c(repo_name): | |
1190 | return self.__get_instance() |
|
1186 | return self.__get_instance() | |
1191 | rn = self.repo_name |
|
1187 | rn = self.repo_name | |
1192 |
|
1188 | |||
1193 | if cache_map: |
|
1189 | valid = CacheInvalidation.test_and_set_valid(rn, valid_cache_keys=valid_cache_keys) | |
1194 | # get using prefilled cache_map |
|
1190 | if not valid: | |
1195 | invalidate_repo = cache_map[self.repo_name] |
|
1191 | log.debug('Cache for %s invalidated, getting new object' % (rn)) | |
1196 | if invalidate_repo: |
|
|||
1197 | invalidate_repo = (None if invalidate_repo.cache_active |
|
|||
1198 | else invalidate_repo) |
|
|||
1199 | else: |
|
|||
1200 | # get from invalidate |
|
|||
1201 | invalidate_repo = self.invalidate |
|
|||
1202 |
|
||||
1203 | if invalidate_repo is not None: |
|
|||
1204 | region_invalidate(_c, None, rn) |
|
1192 | region_invalidate(_c, None, rn) | |
1205 | log.debug('Cache for %s invalidated, getting new object' % (rn)) |
|
|||
1206 | # update our cache |
|
|||
1207 | CacheInvalidation.set_valid(invalidate_repo.cache_key) |
|
|||
1208 | else: |
|
1193 | else: | |
1209 | log.debug('Getting obj for %s from cache' % (rn)) |
|
1194 | log.debug('Getting obj for %s from cache' % (rn)) | |
1210 | return _c(rn) |
|
1195 | return _c(rn) | |
@@ -1825,34 +1810,6 b' class CacheInvalidation(Base, BaseModel)' | |||||
1825 | return "%s%s" % (prefix, key) |
|
1810 | return "%s%s" % (prefix, key) | |
1826 |
|
1811 | |||
1827 | @classmethod |
|
1812 | @classmethod | |
1828 | def invalidate(cls, key): |
|
|||
1829 | """ |
|
|||
1830 | Returns Invalidation object if the local cache with the given key is invalid, |
|
|||
1831 | None otherwise. |
|
|||
1832 | """ |
|
|||
1833 | repo_name = key |
|
|||
1834 | repo_name = remove_suffix(repo_name, '_README') |
|
|||
1835 | repo_name = remove_suffix(repo_name, '_RSS') |
|
|||
1836 | repo_name = remove_suffix(repo_name, '_ATOM') |
|
|||
1837 |
|
||||
1838 | cache_key = cls._get_cache_key(key) |
|
|||
1839 | inv_obj = Session().query(cls).filter(cls.cache_key == cache_key).scalar() |
|
|||
1840 | if not inv_obj: |
|
|||
1841 | try: |
|
|||
1842 | inv_obj = CacheInvalidation(cache_key, repo_name) |
|
|||
1843 | Session().add(inv_obj) |
|
|||
1844 | Session().commit() |
|
|||
1845 | except Exception: |
|
|||
1846 | log.error(traceback.format_exc()) |
|
|||
1847 | Session().rollback() |
|
|||
1848 | return |
|
|||
1849 |
|
||||
1850 | if not inv_obj.cache_active: |
|
|||
1851 | # `cache_active = False` means that this cache |
|
|||
1852 | # no longer is valid |
|
|||
1853 | return inv_obj |
|
|||
1854 |
|
||||
1855 | @classmethod |
|
|||
1856 | def set_invalidate(cls, repo_name): |
|
1813 | def set_invalidate(cls, repo_name): | |
1857 | """ |
|
1814 | """ | |
1858 | Mark all caches of a repo as invalid in the database. |
|
1815 | Mark all caches of a repo as invalid in the database. | |
@@ -1871,46 +1828,43 b' class CacheInvalidation(Base, BaseModel)' | |||||
1871 | Session().rollback() |
|
1828 | Session().rollback() | |
1872 |
|
1829 | |||
1873 | @classmethod |
|
1830 | @classmethod | |
1874 | def set_valid(cls, cache_key): |
|
1831 | def test_and_set_valid(cls, key, valid_cache_keys=None): | |
1875 | """ |
|
1832 | """ | |
1876 | Mark this cache key as active and currently cached |
|
1833 | Mark this cache key as active and currently cached. | |
|
1834 | Return True if the existing cache registration still was valid. | |||
|
1835 | Return False to indicate that it had been invalidated and caches should be refreshed. | |||
1877 | """ |
|
1836 | """ | |
1878 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() |
|
1837 | cache_key = cls._get_cache_key(key) | |
1879 | inv_obj.cache_active = True |
|
1838 | ||
1880 | Session().add(inv_obj) |
|
1839 | if valid_cache_keys and cache_key in valid_cache_keys: | |
1881 | Session().commit() |
|
1840 | return True | |
|
1841 | ||||
|
1842 | repo_name = key | |||
|
1843 | repo_name = remove_suffix(repo_name, '_README') | |||
|
1844 | repo_name = remove_suffix(repo_name, '_RSS') | |||
|
1845 | repo_name = remove_suffix(repo_name, '_ATOM') | |||
|
1846 | ||||
|
1847 | try: | |||
|
1848 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() | |||
|
1849 | if not inv_obj: | |||
|
1850 | inv_obj = CacheInvalidation(cache_key, repo_name) | |||
|
1851 | was_valid = inv_obj.cache_active | |||
|
1852 | inv_obj.cache_active = True | |||
|
1853 | Session().add(inv_obj) | |||
|
1854 | Session().commit() | |||
|
1855 | return was_valid | |||
|
1856 | except Exception: | |||
|
1857 | log.error(traceback.format_exc()) | |||
|
1858 | Session().rollback() | |||
|
1859 | return False | |||
1882 |
|
1860 | |||
1883 | @classmethod |
|
1861 | @classmethod | |
1884 |
def get_cache_ |
|
1862 | def get_valid_cache_keys(cls): | |
1885 |
|
1863 | """ | ||
1886 | class cachemapdict(dict): |
|
1864 | Return opaque object with information of which caches still are valid | |
1887 |
|
1865 | and can be used without checking for invalidation. | ||
1888 | def __init__(self, *args, **kwargs): |
|
1866 | """ | |
1889 | self.fixkey = kwargs.pop('fixkey', False) |
|
1867 | return set(inv_obj.cache_key for inv_obj in cls.query().filter(cls.cache_active).all()) | |
1890 | super(cachemapdict, self).__init__(*args, **kwargs) |
|
|||
1891 |
|
||||
1892 | def __getattr__(self, name): |
|
|||
1893 | cache_key = name |
|
|||
1894 | if self.fixkey: |
|
|||
1895 | cache_key = cls._get_cache_key(name) |
|
|||
1896 | if cache_key in self.__dict__: |
|
|||
1897 | return self.__dict__[cache_key] |
|
|||
1898 | else: |
|
|||
1899 | return self[cache_key] |
|
|||
1900 |
|
||||
1901 | def __getitem__(self, name): |
|
|||
1902 | cache_key = name |
|
|||
1903 | if self.fixkey: |
|
|||
1904 | cache_key = cls._get_cache_key(name) |
|
|||
1905 | try: |
|
|||
1906 | return super(cachemapdict, self).__getitem__(cache_key) |
|
|||
1907 | except KeyError: |
|
|||
1908 | return None |
|
|||
1909 |
|
||||
1910 | cache_map = cachemapdict(fixkey=True) |
|
|||
1911 | for obj in cls.query().all(): |
|
|||
1912 | cache_map[obj.cache_key] = cachemapdict(obj.get_dict()) |
|
|||
1913 | return cache_map |
|
|||
1914 |
|
1868 | |||
1915 |
|
1869 | |||
1916 | class ChangesetComment(Base, BaseModel): |
|
1870 | class ChangesetComment(Base, BaseModel): |
@@ -97,12 +97,12 b' class CachedRepoList(object):' | |||||
97 | return '<%s (%s)>' % (self.__class__.__name__, self.__len__()) |
|
97 | return '<%s (%s)>' % (self.__class__.__name__, self.__len__()) | |
98 |
|
98 | |||
99 | def __iter__(self): |
|
99 | def __iter__(self): | |
100 |
# pre-propagated cache_ |
|
100 | # pre-propagated valid_cache_keys to save executing select statements | |
101 | # for each repo |
|
101 | # for each repo | |
102 |
cache_ |
|
102 | valid_cache_keys = CacheInvalidation.get_valid_cache_keys() | |
103 |
|
103 | |||
104 | for dbr in self.db_repo_list: |
|
104 | for dbr in self.db_repo_list: | |
105 |
scmr = dbr.scm_instance_cached(cache_ |
|
105 | scmr = dbr.scm_instance_cached(valid_cache_keys) | |
106 | # check permission at this level |
|
106 | # check permission at this level | |
107 | if not HasRepoPermissionAny( |
|
107 | if not HasRepoPermissionAny( | |
108 | *self.perm_set)(dbr.repo_name, 'get repo check'): |
|
108 | *self.perm_set)(dbr.repo_name, 'get repo check'): |
General Comments 0
You need to be logged in to leave comments.
Login now