Mercurial > kallithea
comparison rhodecode/model/db.py @ 3684:e8aff2016d86 beta
invalidation: inline _get_or_create_inv_obj
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Thu, 04 Apr 2013 15:55:53 +0200 |
parents | df57253e965a |
children | 02e1e270bf93 |
comparison
equal
deleted
inserted
replaced
3683:df57253e965a | 3684:e8aff2016d86 |
---|---|
1667 import rhodecode | 1667 import rhodecode |
1668 prefix = rhodecode.CONFIG.get('instance_id', '') | 1668 prefix = rhodecode.CONFIG.get('instance_id', '') |
1669 return "%s%s" % (prefix, key) | 1669 return "%s%s" % (prefix, key) |
1670 | 1670 |
1671 @classmethod | 1671 @classmethod |
1672 def _get_or_create_inv_obj(cls, key, repo_name): | 1672 def invalidate(cls, key): |
1673 inv_obj = Session().query(cls).filter(cls.cache_key == key).scalar() | 1673 """ |
1674 Returns Invalidation object if the local cache with the given key is invalid, | |
1675 None otherwise. | |
1676 """ | |
1677 repo_name = key | |
1678 repo_name = remove_suffix(repo_name, '_README') | |
1679 repo_name = remove_suffix(repo_name, '_RSS') | |
1680 repo_name = remove_suffix(repo_name, '_ATOM') | |
1681 | |
1682 cache_key = cls._get_cache_key(key) | |
1683 inv_obj = Session().query(cls).filter(cls.cache_key == cache_key).scalar() | |
1674 if not inv_obj: | 1684 if not inv_obj: |
1675 try: | 1685 try: |
1676 inv_obj = CacheInvalidation(key, repo_name) | 1686 inv_obj = CacheInvalidation(cache_key, repo_name) |
1677 Session().add(inv_obj) | 1687 Session().add(inv_obj) |
1678 Session().commit() | 1688 Session().commit() |
1679 except Exception: | 1689 except Exception: |
1680 log.error(traceback.format_exc()) | 1690 log.error(traceback.format_exc()) |
1681 Session().rollback() | 1691 Session().rollback() |
1682 return inv_obj | 1692 return |
1683 | 1693 |
1684 @classmethod | 1694 if not inv_obj.cache_active: |
1685 def invalidate(cls, key): | |
1686 """ | |
1687 Returns Invalidation object if the local cache with the given key is invalid, | |
1688 None otherwise. | |
1689 """ | |
1690 repo_name = key | |
1691 repo_name = remove_suffix(repo_name, '_README') | |
1692 repo_name = remove_suffix(repo_name, '_RSS') | |
1693 repo_name = remove_suffix(repo_name, '_ATOM') | |
1694 | |
1695 cache_key = cls._get_cache_key(key) | |
1696 inv_obj = cls._get_or_create_inv_obj(cache_key, repo_name) | |
1697 | |
1698 if inv_obj and not inv_obj.cache_active: | |
1699 # `cache_active = False` means that this cache | 1695 # `cache_active = False` means that this cache |
1700 # no longer is valid | 1696 # no longer is valid |
1701 return inv_obj | 1697 return inv_obj |
1702 | 1698 |
1703 @classmethod | 1699 @classmethod |