##// END OF EJS Templates
api: add cache for fts index builder call that returns file tree. This is often re-used and should return unchanged state fast
marcink -
r3462:e3886a39 default
parent child Browse files
Show More
@@ -29,13 +29,14 b' from rhodecode.api.utils import ('
29 get_user_group_or_error, get_user_or_error, validate_repo_permissions,
29 get_user_group_or_error, get_user_or_error, validate_repo_permissions,
30 get_perm_or_error, parse_args, get_origin, build_commit_data,
30 get_perm_or_error, parse_args, get_origin, build_commit_data,
31 validate_set_owner_permissions)
31 validate_set_owner_permissions)
32 from rhodecode.lib import audit_logger
32 from rhodecode.lib import audit_logger, rc_cache
33 from rhodecode.lib import repo_maintenance
33 from rhodecode.lib import repo_maintenance
34 from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi
34 from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi
35 from rhodecode.lib.celerylib.utils import get_task_id
35 from rhodecode.lib.celerylib.utils import get_task_id
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_str
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_str, safe_int
37 from rhodecode.lib.ext_json import json
37 from rhodecode.lib.ext_json import json
38 from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError
38 from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError
39 from rhodecode.lib.vcs import RepositoryError
39 from rhodecode.model.changeset_status import ChangesetStatusModel
40 from rhodecode.model.changeset_status import ChangesetStatusModel
40 from rhodecode.model.comment import CommentsModel
41 from rhodecode.model.comment import CommentsModel
41 from rhodecode.model.db import (
42 from rhodecode.model.db import (
@@ -598,13 +599,37 b' def get_repo_fts_tree(request, apiuser, '
598 _perms = ('repository.admin', 'repository.write', 'repository.read',)
599 _perms = ('repository.admin', 'repository.write', 'repository.read',)
599 validate_repo_permissions(apiuser, repoid, repo, _perms)
600 validate_repo_permissions(apiuser, repoid, repo, _perms)
600
601
602 repo_id = repo.repo_id
603 cache_seconds = safe_int(rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time'))
604 cache_on = cache_seconds > 0
605
606 cache_namespace_uid = 'cache_repo.{}'.format(repo_id)
607 region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid)
608
609 @region.conditional_cache_on_arguments(namespace=cache_namespace_uid,
610 condition=cache_on)
611 def compute_fts_tree(repo_id, commit_id, root_path, cache_ver):
612 return ScmModel().get_fts_data(repo_id, commit_id, root_path)
613
601 try:
614 try:
602 # check if repo is not empty by any chance, skip quicker if it is.
615 # check if repo is not empty by any chance, skip quicker if it is.
603 _scm = repo.scm_instance()
616 _scm = repo.scm_instance()
604 if _scm.is_empty():
617 if _scm.is_empty():
605 return []
618 return []
619 except RepositoryError:
620 log.exception("Exception occurred while trying to get repo nodes")
621 raise JSONRPCError('failed to get repo: `%s` nodes' % repo.repo_name)
606
622
607 tree_files = ScmModel().get_fts_data(repo, commit_id, root_path)
623 try:
624 # we need to resolve commit_id to a FULL sha for cache to work correctly.
625 # sending 'master' is a pointer that needs to be translated to current commit.
626 commit_id = _scm.get_commit(commit_id=commit_id).raw_id
627 log.debug(
628 'Computing FTS REPO TREE for repo_id %s commit_id `%s` '
629 'with caching: %s[TTL: %ss]' % (
630 repo_id, commit_id, cache_on, cache_seconds or 0))
631
632 tree_files = compute_fts_tree(repo_id, commit_id, root_path, 'v1')
608 return tree_files
633 return tree_files
609
634
610 except Exception:
635 except Exception:
General Comments 0
You need to be logged in to leave comments. Login now