##// END OF EJS Templates
invalidation: some documentation and refactoring, second round
Mads Kiilerich -
r3682:cf65b2c1 beta
parent child Browse files
Show More
@@ -1147,7 +1147,7 class Repository(Base, BaseModel):
1147
1147
1148 def set_invalidate(self):
1148 def set_invalidate(self):
1149 """
1149 """
1150 set a cache for invalidation for this instance
1150 Mark caches of this repo as invalid.
1151 """
1151 """
1152 CacheInvalidation.set_invalidate(repo_name=self.repo_name)
1152 CacheInvalidation.set_invalidate(repo_name=self.repo_name)
1153
1153
@@ -1636,14 +1636,15 class CacheInvalidation(Base, BaseModel)
1636 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1636 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1637 # cache_key as created by _get_cache_key
1637 # cache_key as created by _get_cache_key
1638 cache_key = Column("cache_key", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1638 cache_key = Column("cache_key", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1639 # cache_args is usually a repo_name, possibly with _README/_RSS/_ATOM suffix
1639 # cache_args is a repo_name
1640 cache_args = Column("cache_args", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1640 cache_args = Column("cache_args", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1641 # instance sets cache_active True when it is caching, other instances set cache_active to False to invalidate
1641 # instance sets cache_active True when it is caching,
1642 # other instances set cache_active to False to indicate that this cache is invalid
1642 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
1643 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
1643
1644
1644 def __init__(self, cache_key, cache_args=''):
1645 def __init__(self, cache_key, repo_name=''):
1645 self.cache_key = cache_key
1646 self.cache_key = cache_key
1646 self.cache_args = cache_args
1647 self.cache_args = repo_name
1647 self.cache_active = False
1648 self.cache_active = False
1648
1649
1649 def __unicode__(self):
1650 def __unicode__(self):
@@ -1664,19 +1665,19 class CacheInvalidation(Base, BaseModel)
1664 def _get_cache_key(cls, key):
1665 def _get_cache_key(cls, key):
1665 """
1666 """
1666 Wrapper for generating a unique cache key for this instance and "key".
1667 Wrapper for generating a unique cache key for this instance and "key".
1668 key must / will start with a repo_name which will be stored in .cache_args .
1667 """
1669 """
1668 import rhodecode
1670 import rhodecode
1669 prefix = rhodecode.CONFIG.get('instance_id', '')
1671 prefix = rhodecode.CONFIG.get('instance_id', '')
1670 return "%s%s" % (prefix, key)
1672 return "%s%s" % (prefix, key)
1671
1673
1672 @classmethod
1674 @classmethod
1673 def _get_or_create_inv_obj(cls, key, repo_name, commit=True):
1675 def _get_or_create_inv_obj(cls, key, repo_name):
1674 inv_obj = Session().query(cls).filter(cls.cache_key == key).scalar()
1676 inv_obj = Session().query(cls).filter(cls.cache_key == key).scalar()
1675 if not inv_obj:
1677 if not inv_obj:
1676 try:
1678 try:
1677 inv_obj = CacheInvalidation(key, repo_name)
1679 inv_obj = CacheInvalidation(key, repo_name)
1678 Session().add(inv_obj)
1680 Session().add(inv_obj)
1679 if commit:
1680 Session().commit()
1681 Session().commit()
1681 except Exception:
1682 except Exception:
1682 log.error(traceback.format_exc())
1683 log.error(traceback.format_exc())
@@ -1686,11 +1687,8 class CacheInvalidation(Base, BaseModel)
1686 @classmethod
1687 @classmethod
1687 def invalidate(cls, key):
1688 def invalidate(cls, key):
1688 """
1689 """
1689 Returns Invalidation object if this given key should be invalidated
1690 Returns Invalidation object if the local cache with the given key is invalid,
1690 None otherwise. `cache_active = False` means that this cache
1691 None otherwise.
1691 state is not valid and needs to be invalidated
1692
1693 :param key:
1694 """
1692 """
1695 repo_name = key
1693 repo_name = key
1696 repo_name = remove_suffix(repo_name, '_README')
1694 repo_name = remove_suffix(repo_name, '_README')
@@ -1698,10 +1696,12 class CacheInvalidation(Base, BaseModel)
1698 repo_name = remove_suffix(repo_name, '_ATOM')
1696 repo_name = remove_suffix(repo_name, '_ATOM')
1699
1697
1700 cache_key = cls._get_cache_key(key)
1698 cache_key = cls._get_cache_key(key)
1701 inv = cls._get_or_create_inv_obj(cache_key, repo_name)
1699 inv_obj = cls._get_or_create_inv_obj(cache_key, repo_name)
1702
1700
1703 if inv and not inv.cache_active:
1701 if inv_obj and not inv_obj.cache_active:
1704 return inv
1702 # `cache_active = False` means that this cache
1703 # no longer is valid
1704 return inv_obj
1705
1705
1706 @classmethod
1706 @classmethod
1707 def set_invalidate(cls, key=None, repo_name=None):
1707 def set_invalidate(cls, key=None, repo_name=None):
@@ -1734,13 +1734,11 class CacheInvalidation(Base, BaseModel)
1734 return invalidated_keys
1734 return invalidated_keys
1735
1735
1736 @classmethod
1736 @classmethod
1737 def set_valid(cls, key):
1737 def set_valid(cls, cache_key):
1738 """
1738 """
1739 Mark this cache key as active and currently cached
1739 Mark this cache key as active and currently cached
1740
1741 :param key:
1742 """
1740 """
1743 inv_obj = cls.query().filter(cls.cache_key == key).scalar()
1741 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
1744 inv_obj.cache_active = True
1742 inv_obj.cache_active = True
1745 Session().add(inv_obj)
1743 Session().add(inv_obj)
1746 Session().commit()
1744 Session().commit()
@@ -297,10 +297,9 class ScmModel(BaseModel):
297
297
298 def mark_for_invalidation(self, repo_name):
298 def mark_for_invalidation(self, repo_name):
299 """
299 """
300 Puts cache invalidation task into db for
300 Mark caches of this repo invalid in the database.
301 further global cache invalidation
302
301
303 :param repo_name: this repo that should invalidation take place
302 :param repo_name: the repo for which caches should be marked invalid
304 """
303 """
305 invalidated_keys = CacheInvalidation.set_invalidate(repo_name=repo_name)
304 invalidated_keys = CacheInvalidation.set_invalidate(repo_name=repo_name)
306 repo = Repository.get_by_repo_name(repo_name)
305 repo = Repository.get_by_repo_name(repo_name)
General Comments 0
You need to be logged in to leave comments. Login now