##// END OF EJS Templates
invalidate: encapsulate how key is generated from repo_name and kind
Mads Kiilerich -
r3773:8e2cd46f beta
parent child Browse files
Show More
@@ -118,7 +118,7 b' class FeedController(BaseRepoController)'
118 """Produce an atom-1.0 feed via feedgenerator module"""
118 """Produce an atom-1.0 feed via feedgenerator module"""
119
119
120 @cache_region('long_term')
120 @cache_region('long_term')
121 def _get_feed_from_cache(key):
121 def _get_feed_from_cache(key, kind):
122 feed = Atom1Feed(
122 feed = Atom1Feed(
123 title=self.title % repo_name,
123 title=self.title % repo_name,
124 link=url('summary_home', repo_name=repo_name,
124 link=url('summary_home', repo_name=repo_name,
@@ -140,17 +140,17 b' class FeedController(BaseRepoController)'
140 response.content_type = feed.mime_type
140 response.content_type = feed.mime_type
141 return feed.writeString('utf-8')
141 return feed.writeString('utf-8')
142
142
143 key = repo_name + '_ATOM'
143 kind = 'ATOM'
144 valid = CacheInvalidation.test_and_set_valid(key)
144 valid = CacheInvalidation.test_and_set_valid(repo_name, kind)
145 if not valid:
145 if not valid:
146 region_invalidate(_get_feed_from_cache, None, key)
146 region_invalidate(_get_feed_from_cache, None, repo_name, kind)
147 return _get_feed_from_cache(key)
147 return _get_feed_from_cache(repo_name, kind)
148
148
149 def rss(self, repo_name):
149 def rss(self, repo_name):
150 """Produce an rss2 feed via feedgenerator module"""
150 """Produce an rss2 feed via feedgenerator module"""
151
151
152 @cache_region('long_term')
152 @cache_region('long_term')
153 def _get_feed_from_cache(key):
153 def _get_feed_from_cache(key, kind):
154 feed = Rss201rev2Feed(
154 feed = Rss201rev2Feed(
155 title=self.title % repo_name,
155 title=self.title % repo_name,
156 link=url('summary_home', repo_name=repo_name,
156 link=url('summary_home', repo_name=repo_name,
@@ -172,8 +172,8 b' class FeedController(BaseRepoController)'
172 response.content_type = feed.mime_type
172 response.content_type = feed.mime_type
173 return feed.writeString('utf-8')
173 return feed.writeString('utf-8')
174
174
175 key = repo_name + '_RSS'
175 kind = 'RSS'
176 valid = CacheInvalidation.test_and_set_valid(key)
176 valid = CacheInvalidation.test_and_set_valid(repo_name, kind)
177 if not valid:
177 if not valid:
178 region_invalidate(_get_feed_from_cache, None, key)
178 region_invalidate(_get_feed_from_cache, None, repo_name, kind)
179 return _get_feed_from_cache(key)
179 return _get_feed_from_cache(repo_name, kind)
@@ -92,7 +92,7 b' class SummaryController(BaseRepoControll'
92 repo_name = db_repo.repo_name
92 repo_name = db_repo.repo_name
93
93
94 @cache_region('long_term')
94 @cache_region('long_term')
95 def _get_readme_from_cache(key):
95 def _get_readme_from_cache(key, kind):
96 readme_data = None
96 readme_data = None
97 readme_file = None
97 readme_file = None
98 log.debug('Looking for README file')
98 log.debug('Looking for README file')
@@ -124,11 +124,11 b' class SummaryController(BaseRepoControll'
124
124
125 return readme_data, readme_file
125 return readme_data, readme_file
126
126
127 key = repo_name + '_README'
127 kind = 'README'
128 valid = CacheInvalidation.test_and_set_valid(key)
128 valid = CacheInvalidation.test_and_set_valid(repo_name, kind)
129 if not valid:
129 if not valid:
130 region_invalidate(_get_readme_from_cache, None, key)
130 region_invalidate(_get_readme_from_cache, None, repo_name, kind)
131 return _get_readme_from_cache(key)
131 return _get_readme_from_cache(repo_name, kind)
132
132
133 @LoginRequired()
133 @LoginRequired()
134 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
134 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
@@ -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.test_and_set_valid(name)
479 CacheInvalidation.test_and_set_valid(name, None)
480
480
481 sa.commit()
481 sa.commit()
482 removed = []
482 removed = []
@@ -1186,7 +1186,7 b' class Repository(Base, BaseModel):'
1186 return self.__get_instance()
1186 return self.__get_instance()
1187 rn = self.repo_name
1187 rn = self.repo_name
1188
1188
1189 valid = CacheInvalidation.test_and_set_valid(rn, valid_cache_keys=valid_cache_keys)
1189 valid = CacheInvalidation.test_and_set_valid(rn, None, valid_cache_keys=valid_cache_keys)
1190 if not valid:
1190 if not valid:
1191 log.debug('Cache for %s invalidated, getting new object' % (rn))
1191 log.debug('Cache for %s invalidated, getting new object' % (rn))
1192 region_invalidate(_c, None, rn)
1192 region_invalidate(_c, None, rn)
@@ -1828,22 +1828,19 b' class CacheInvalidation(Base, BaseModel)'
1828 Session().rollback()
1828 Session().rollback()
1829
1829
1830 @classmethod
1830 @classmethod
1831 def test_and_set_valid(cls, key, valid_cache_keys=None):
1831 def test_and_set_valid(cls, repo_name, kind, valid_cache_keys=None):
1832 """
1832 """
1833 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.
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.
1835 Return False to indicate that it had been invalidated and caches should be refreshed.
1836 """
1836 """
1837
1838 key = (repo_name + '_' + kind) if kind else repo_name
1837 cache_key = cls._get_cache_key(key)
1839 cache_key = cls._get_cache_key(key)
1838
1840
1839 if valid_cache_keys and cache_key in valid_cache_keys:
1841 if valid_cache_keys and cache_key in valid_cache_keys:
1840 return True
1842 return True
1841
1843
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:
1844 try:
1848 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
1845 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
1849 if not inv_obj:
1846 if not inv_obj:
General Comments 0
You need to be logged in to leave comments. Login now