Mercurial > kallithea
comparison rhodecode/model/db.py @ 3682:cf65b2c1b69d beta
invalidation: some documentation and refactoring, second round
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Wed, 27 Mar 2013 19:55:50 +0100 |
parents | 0db18ba129ea |
children | df57253e965a |
comparison
equal
deleted
inserted
replaced
3681:05006e3e286b | 3682:cf65b2c1b69d |
---|---|
1145 def invalidate(self): | 1145 def invalidate(self): |
1146 return CacheInvalidation.invalidate(self.repo_name) | 1146 return CacheInvalidation.invalidate(self.repo_name) |
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 |
1154 def scm_instance_no_cache(self): | 1154 def scm_instance_no_cache(self): |
1155 return self.__get_instance() | 1155 return self.__get_instance() |
1634 ) | 1634 ) |
1635 # cache_id, not used | 1635 # cache_id, not used |
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): |
1650 return u"<%s('%s:%s')>" % (self.__class__.__name__, | 1651 return u"<%s('%s:%s')>" % (self.__class__.__name__, |
1651 self.cache_id, self.cache_key) | 1652 self.cache_id, self.cache_key) |
1662 | 1663 |
1663 @classmethod | 1664 @classmethod |
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: | 1681 Session().commit() |
1680 Session().commit() | |
1681 except Exception: | 1682 except Exception: |
1682 log.error(traceback.format_exc()) | 1683 log.error(traceback.format_exc()) |
1683 Session().rollback() | 1684 Session().rollback() |
1684 return inv_obj | 1685 return inv_obj |
1685 | 1686 |
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') |
1697 repo_name = remove_suffix(repo_name, '_RSS') | 1695 repo_name = remove_suffix(repo_name, '_RSS') |
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): |
1708 """ | 1708 """ |
1709 Mark this Cache key for invalidation, either by key or whole | 1709 Mark this Cache key for invalidation, either by key or whole |
1732 log.error(traceback.format_exc()) | 1732 log.error(traceback.format_exc()) |
1733 Session().rollback() | 1733 Session().rollback() |
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 | 1740 """ |
1741 :param key: | 1741 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() |
1742 """ | |
1743 inv_obj = cls.query().filter(cls.cache_key == 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() |
1747 | 1745 |
1748 @classmethod | 1746 @classmethod |