當前位置: 首頁>>代碼示例>>Python>>正文


Python RequestCache.pop方法代碼示例

本文整理匯總了Python中edx_django_utils.cache.RequestCache.pop方法的典型用法代碼示例。如果您正苦於以下問題:Python RequestCache.pop方法的具體用法?Python RequestCache.pop怎麽用?Python RequestCache.pop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在edx_django_utils.cache.RequestCache的用法示例。


在下文中一共展示了RequestCache.pop方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_group_info_for_cohort

# 需要導入模塊: from edx_django_utils.cache import RequestCache [as 別名]
# 或者: from edx_django_utils.cache.RequestCache import pop [as 別名]
def get_group_info_for_cohort(cohort, use_cached=False):
    """
    Get the ids of the group and partition to which this cohort has been linked
    as a tuple of (int, int).

    If the cohort has not been linked to any group/partition, both values in the
    tuple will be None.

    The partition group info is cached for the duration of a request. Pass
    use_cached=True to use the cached value instead of fetching from the
    database.
    """
    cache = RequestCache(u"cohorts.get_group_info_for_cohort").data
    cache_key = six.text_type(cohort.id)

    if use_cached and cache_key in cache:
        return cache[cache_key]

    cache.pop(cache_key, None)

    try:
        partition_group = CourseUserGroupPartitionGroup.objects.get(course_user_group=cohort)
        return cache.setdefault(cache_key, (partition_group.group_id, partition_group.partition_id))
    except CourseUserGroupPartitionGroup.DoesNotExist:
        pass

    return cache.setdefault(cache_key, (None, None))
開發者ID:edx,項目名稱:edx-platform,代碼行數:29,代碼來源:cohorts.py

示例2: get_cohort

# 需要導入模塊: from edx_django_utils.cache import RequestCache [as 別名]
# 或者: from edx_django_utils.cache.RequestCache import pop [as 別名]
def get_cohort(user, course_key, assign=True, use_cached=False):
    """
    Returns the user's cohort for the specified course.

    The cohort for the user is cached for the duration of a request. Pass
    use_cached=True to use the cached value instead of fetching from the
    database.

    Arguments:
        user: a Django User object.
        course_key: CourseKey
        assign (bool): if False then we don't assign a group to user
        use_cached (bool): Whether to use the cached value or fetch from database.

    Returns:
        A CourseUserGroup object if the course is cohorted and the User has a
        cohort, else None.

    Raises:
       ValueError if the CourseKey doesn't exist.
    """
    if user.is_anonymous:
        return None
    cache = RequestCache(COHORT_CACHE_NAMESPACE).data
    cache_key = _cohort_cache_key(user.id, course_key)

    if use_cached and cache_key in cache:
        return cache[cache_key]

    cache.pop(cache_key, None)

    # First check whether the course is cohorted (users shouldn't be in a cohort
    # in non-cohorted courses, but settings can change after course starts)
    if not is_course_cohorted(course_key):
        return cache.setdefault(cache_key, None)

    # If course is cohorted, check if the user already has a cohort.
    try:
        membership = CohortMembership.objects.get(
            course_id=course_key,
            user_id=user.id,
        )
        return cache.setdefault(cache_key, membership.course_user_group)
    except CohortMembership.DoesNotExist:
        # Didn't find the group. If we do not want to assign, return here.
        if not assign:
            # Do not cache the cohort here, because in the next call assign
            # may be True, and we will have to assign the user a cohort.
            return None

    # Otherwise assign the user a cohort.
    try:
        # If learner has been pre-registered in a cohort, get that cohort. Otherwise assign to a random cohort.
        course_user_group = None
        for assignment in UnregisteredLearnerCohortAssignments.objects.filter(email=user.email, course_id=course_key):
            course_user_group = assignment.course_user_group
            assignment.delete()
            break
        else:
            course_user_group = get_random_cohort(course_key)
        add_user_to_cohort(course_user_group, user)
        return course_user_group
    except ValueError:
        # user already in cohort
        return course_user_group
    except IntegrityError as integrity_error:
        # An IntegrityError is raised when multiple workers attempt to
        # create the same row in one of the cohort model entries:
        # CourseCohort, CohortMembership.
        log.info(
            u"HANDLING_INTEGRITY_ERROR: IntegrityError encountered for course '%s' and user '%s': %s",
            course_key, user.id, six.text_type(integrity_error)
        )
        return get_cohort(user, course_key, assign, use_cached)
開發者ID:edx,項目名稱:edx-platform,代碼行數:76,代碼來源:cohorts.py


注:本文中的edx_django_utils.cache.RequestCache.pop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。