##// END OF EJS Templates
invalidation: some documentation and refactoring
Mads Kiilerich -
r3606:c8ecfe42 beta
parent child Browse files
Show More
@@ -429,11 +429,6 b' def repo2db_mapper(initial_repo_list, re'
429 raise Exception('Missing administrative account!')
429 raise Exception('Missing administrative account!')
430 added = []
430 added = []
431
431
432 # # clear cache keys
433 # log.debug("Clearing cache keys now...")
434 # CacheInvalidation.clear_cache()
435 # sa.commit()
436
437 ##creation defaults
432 ##creation defaults
438 defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True)
433 defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True)
439 enable_statistics = defs.get('repo_enable_statistics')
434 enable_statistics = defs.get('repo_enable_statistics')
@@ -474,10 +469,9 b' def repo2db_mapper(initial_repo_list, re'
474 ScmModel().install_git_hook(db_repo.scm_instance)
469 ScmModel().install_git_hook(db_repo.scm_instance)
475 # during starting install all cache keys for all repositories in the
470 # during starting install all cache keys for all repositories in the
476 # system, this will register all repos and multiple instances
471 # system, this will register all repos and multiple instances
477 key, _prefix, _org_key = CacheInvalidation._get_key(name)
472 cache_key = CacheInvalidation._get_cache_key(name)
473 log.debug("Creating invalidation cache key for %s: %s", name, cache_key)
478 CacheInvalidation.invalidate(name)
474 CacheInvalidation.invalidate(name)
479 log.debug("Creating a cache key for %s, instance_id %s"
480 % (name, _prefix or 'unknown'))
481
475
482 sa.commit()
476 sa.commit()
483 removed = []
477 removed = []
@@ -1630,9 +1630,13 b' class CacheInvalidation(Base, BaseModel)'
1630 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1630 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1631 'mysql_charset': 'utf8'},
1631 'mysql_charset': 'utf8'},
1632 )
1632 )
1633 # cache_id, not used
1633 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1634 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1635 # cache_key as created by _get_cache_key
1634 cache_key = Column("cache_key", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1636 cache_key = Column("cache_key", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1637 # cache_args is usually a repo_name, possibly with _README/_RSS/_ATOM suffix
1635 cache_args = Column("cache_args", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1638 cache_args = Column("cache_args", String(255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
1639 # instance sets cache_active True when it is caching, other instances set cache_active to False to invalidate
1636 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
1640 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
1637
1641
1638 def __init__(self, cache_key, cache_args=''):
1642 def __init__(self, cache_key, cache_args=''):
@@ -1644,43 +1648,27 b' class CacheInvalidation(Base, BaseModel)'
1644 return u"<%s('%s:%s')>" % (self.__class__.__name__,
1648 return u"<%s('%s:%s')>" % (self.__class__.__name__,
1645 self.cache_id, self.cache_key)
1649 self.cache_id, self.cache_key)
1646
1650
1647 @property
1651 def get_prefix(self):
1648 def prefix(self):
1652 """
1653 Guess prefix that might have been used in _get_cache_key to generate self.cache_key .
1654 Only used for informational purposes in repo_edit.html .
1655 """
1649 _split = self.cache_key.split(self.cache_args, 1)
1656 _split = self.cache_key.split(self.cache_args, 1)
1650 if _split and len(_split) == 2:
1657 if len(_split) == 2:
1651 return _split[0]
1658 return _split[0]
1652 return ''
1659 return ''
1653
1660
1654 @classmethod
1661 @classmethod
1655 def clear_cache(cls):
1662 def _get_cache_key(cls, key):
1656 cls.query().delete()
1657
1658 @classmethod
1659 def _get_key(cls, key):
1660 """
1663 """
1661 Wrapper for generating a key, together with a prefix
1664 Wrapper for generating a unique cache key for this instance and "key".
1662
1663 :param key:
1664 """
1665 """
1665 import rhodecode
1666 import rhodecode
1666 prefix = ''
1667 prefix = rhodecode.CONFIG.get('instance_id', '')
1667 org_key = key
1668 return "%s%s" % (prefix, key)
1668 iid = rhodecode.CONFIG.get('instance_id')
1669 if iid:
1670 prefix = iid
1671
1672 return "%s%s" % (prefix, key), prefix, org_key
1673
1669
1674 @classmethod
1670 @classmethod
1675 def get_by_key(cls, key):
1671 def _get_or_create_inv_obj(cls, key, repo_name, commit=True):
1676 return cls.query().filter(cls.cache_key == key).scalar()
1677
1678 @classmethod
1679 def get_by_repo_name(cls, repo_name):
1680 return cls.query().filter(cls.cache_args == repo_name).all()
1681
1682 @classmethod
1683 def _get_or_create_key(cls, key, repo_name, commit=True):
1684 inv_obj = Session().query(cls).filter(cls.cache_key == key).scalar()
1672 inv_obj = Session().query(cls).filter(cls.cache_key == key).scalar()
1685 if not inv_obj:
1673 if not inv_obj:
1686 try:
1674 try:
@@ -1707,9 +1695,8 b' class CacheInvalidation(Base, BaseModel)'
1707 repo_name = remove_suffix(repo_name, '_RSS')
1695 repo_name = remove_suffix(repo_name, '_RSS')
1708 repo_name = remove_suffix(repo_name, '_ATOM')
1696 repo_name = remove_suffix(repo_name, '_ATOM')
1709
1697
1710 # adds instance prefix
1698 cache_key = cls._get_cache_key(key)
1711 key, _prefix, _org_key = cls._get_key(key)
1699 inv = cls._get_or_create_inv_obj(cache_key, repo_name)
1712 inv = cls._get_or_create_key(key, repo_name)
1713
1700
1714 if inv and inv.cache_active is False:
1701 if inv and inv.cache_active is False:
1715 return inv
1702 return inv
@@ -1724,9 +1711,11 b' class CacheInvalidation(Base, BaseModel)'
1724 """
1711 """
1725 invalidated_keys = []
1712 invalidated_keys = []
1726 if key:
1713 if key:
1727 key, _prefix, _org_key = cls._get_key(key)
1714 assert not repo_name
1728 inv_objs = Session().query(cls).filter(cls.cache_key == key).all()
1715 cache_key = cls._get_cache_key(key)
1729 elif repo_name:
1716 inv_objs = Session().query(cls).filter(cls.cache_key == cache_key).all()
1717 else:
1718 assert repo_name
1730 inv_objs = Session().query(cls).filter(cls.cache_args == repo_name).all()
1719 inv_objs = Session().query(cls).filter(cls.cache_args == repo_name).all()
1731
1720
1732 try:
1721 try:
@@ -1749,7 +1738,7 b' class CacheInvalidation(Base, BaseModel)'
1749
1738
1750 :param key:
1739 :param key:
1751 """
1740 """
1752 inv_obj = cls.get_by_key(key)
1741 inv_obj = cls.query().filter(cls.cache_key == key).scalar()
1753 inv_obj.cache_active = True
1742 inv_obj.cache_active = True
1754 Session().add(inv_obj)
1743 Session().add(inv_obj)
1755 Session().commit()
1744 Session().commit()
@@ -1760,28 +1749,26 b' class CacheInvalidation(Base, BaseModel)'
1760 class cachemapdict(dict):
1749 class cachemapdict(dict):
1761
1750
1762 def __init__(self, *args, **kwargs):
1751 def __init__(self, *args, **kwargs):
1763 fixkey = kwargs.get('fixkey')
1752 self.fixkey = kwargs.pop('fixkey', False)
1764 if fixkey:
1765 del kwargs['fixkey']
1766 self.fixkey = fixkey
1767 super(cachemapdict, self).__init__(*args, **kwargs)
1753 super(cachemapdict, self).__init__(*args, **kwargs)
1768
1754
1769 def __getattr__(self, name):
1755 def __getattr__(self, name):
1770 key = name
1756 cache_key = name
1771 if self.fixkey:
1757 if self.fixkey:
1772 key, _prefix, _org_key = cls._get_key(key)
1758 cache_key = cls._get_cache_key(name)
1773 if key in self.__dict__:
1759 if cache_key in self.__dict__:
1774 return self.__dict__[key]
1760 return self.__dict__[cache_key]
1775 else:
1761 else:
1776 return self[key]
1762 return self[cache_key]
1777
1763
1778 def __getitem__(self, key):
1764 def __getitem__(self, name):
1765 cache_key = name
1779 if self.fixkey:
1766 if self.fixkey:
1780 key, _prefix, _org_key = cls._get_key(key)
1767 cache_key = cls._get_cache_key(name)
1781 try:
1768 try:
1782 return super(cachemapdict, self).__getitem__(key)
1769 return super(cachemapdict, self).__getitem__(cache_key)
1783 except KeyError:
1770 except KeyError:
1784 return
1771 return None
1785
1772
1786 cache_map = cachemapdict(fixkey=True)
1773 cache_map = cachemapdict(fixkey=True)
1787 for obj in cls.query().all():
1774 for obj in cls.query().all():
@@ -219,7 +219,7 b''
219 </tr>
219 </tr>
220 %for cache in c.repo_info.cache_keys:
220 %for cache in c.repo_info.cache_keys:
221 <tr>
221 <tr>
222 <td>${cache.prefix or '-'}</td>
222 <td>${cache.get_prefix() or '-'}</td>
223 <td>${cache.cache_key}</td>
223 <td>${cache.cache_key}</td>
224 <td>${h.bool2icon(cache.cache_active)}</td>
224 <td>${h.bool2icon(cache.cache_active)}</td>
225 </tr>
225 </tr>
General Comments 0
You need to be logged in to leave comments. Login now