Python django.core.cache.cache.get_many() Examples

The following are 10 code examples of django.core.cache.cache.get_many(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.core.cache.cache , or try the search function .
Example #1
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_get_many_with_one_expired(self):
        # Multiple cache keys can be returned using get_many
        the_cache = caches["no_cull"]
        the_cache.set("a", "a", 0.1)
        time.sleep(0.2)

        the_cache.set("b", "b")
        the_cache.set("c", "c")
        the_cache.set("d", "d")

        with self.assertNumQueries(1):
            value = the_cache.get_many(["a", "c", "d"])
        assert value == {"c": "c", "d": "d"}

        with self.assertNumQueries(1):
            value = the_cache.get_many(["a", "b", "e"])

        assert value == {"b": "b"} 
Example #2
Source File: permissions.py    From edx-analytics-dashboard with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_user_course_permissions(user):
    """
    Return list of courses accessible by user or None to represent all courses.

    Arguments
        user (User) --  User for which course permissions should be returned
    """
    # ensure we don't request course permissions for users which would return all courses
    if user.is_superuser or user.is_staff:
        return None

    key_courses, key_last_updated = _get_course_permission_cache_keys(user)
    keys = [key_courses, key_last_updated]

    # Check the cache for data
    values = cache.get_many(keys)
    courses = values.get(key_courses, [])

    # If data is not in the cache, refresh the permissions and validate against the new data.
    if not values.get(key_last_updated):
        courses = _refresh_user_course_permissions(user)

    return courses 
Example #3
Source File: models.py    From connect with MIT License 6 votes vote down vote up
def get_moderation_tasks(self):
        """Gets a list of moderation task types that are pending."""
        messages_key = '%s_messages_to_mod' % self.pk
        groups_key = '%s_groups_to_mod' % self.pk
        mods = cache.get_many([messages_key, groups_key])
        if messages_key not in mods:
            messages_to_moderate = self.messages_to_moderate.count()
            cache.set(messages_key, messages_to_moderate, 600)
            mods[messages_key] = messages_to_moderate
        if groups_key not in mods:
            groups_to_moderate = self.group_join_requests_to_moderate().count()
            cache.set(groups_key, groups_to_moderate, 600)
            mods[groups_key] = groups_to_moderate
        return {
            'groups_to_mod': mods[groups_key],
            'messages_to_mod': mods[messages_key]
        } 
Example #4
Source File: caching.py    From feedsubs with MIT License 6 votes vote down vote up
def get_cleaned_articles(articles) -> dict:
    from_cache = cache.get_many([cache_id_to_key(a.id) for a in articles])
    rv = {cache_key_to_id(k): v for k, v in from_cache.items()}

    to_cache = dict()
    for article in articles:
        if article.id in rv:
            continue

        cleaned = html_processing.clean_article(
            article.content,
            base_url=article.feed.uri
        )
        rv[article.id] = cleaned
        to_cache[cache_id_to_key(article.id)] = cleaned

    if to_cache:
        cache.set_many(to_cache, timeout=7200)

    return rv 
Example #5
Source File: attr_cache.py    From lego with MIT License 5 votes vote down vote up
def lookup_cache(self, content_strings):
        """
        Lookup cache keys
        """
        cache_result = cache.get_many(
            [self.CACHE_KEY + content_string for content_string in content_strings]
        )

        # Remove the cache_key prefix
        values = {}
        for key, value in cache_result.items():
            values[key[len(self.CACHE_KEY) :]] = value

        return values 
Example #6
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_many(self):
        # Multiple cache keys can be returned using get_many
        cache.set("a", "a")
        cache.set("b", "b")
        cache.set("c", "c")
        cache.set("d", "d")

        with self.assertNumQueries(1):
            value = cache.get_many(["a", "c", "d"])
        assert value == {"a": "a", "c": "c", "d": "d"}

        with self.assertNumQueries(1):
            value = cache.get_many(["a", "b", "e"])

        assert value == {"a": "a", "b": "b"} 
Example #7
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_delete_with_prefix_with_no_reverse_works(self):
        cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"})
        assert cache.delete_with_prefix("K") == 2
        assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"} 
Example #8
Source File: inbox.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_context_data(self, *args, **kwargs):
        context = super(InboxView, self).get_context_data(*args, **kwargs)

        object_list = []
        object_id_list = []
        for email in context["page_obj"].object_list:
            object_list.append(email)
            object_id_list.append(email.id)

        if len(object_id_list) == 0:
            return context

        headers = cache.get_many(object_id_list, version="email-header")

        missing_list = set(object_id_list) - set(headers.keys())
        if len(missing_list) > 0:
            missing_headers = models.Header.objects.filter(part__parent=None, part__email__in=missing_list)
            missing_headers = missing_headers.get_many("Subject", "From", group_by="part__email_id")
            headers.update(missing_headers)
            cache.set_many(missing_headers, version="email-header", timeout=None)

        for email in object_list:
            header_set = headers[email.id]
            email.subject = header_set.get("Subject")
            email.sender = header_set["From"]

        inbox = getattr(self, 'inbox_obj', None)
        if inbox is not None:
            inbox = inbox.id

        set_emails_to_seen.delay(object_id_list, self.request.user.id, inbox)
        return context 
Example #9
Source File: ratelimit.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def counter_full(self, request):
        now = timezone.now()

        keys = [self.make_key(request, now - timedelta(minutes=i)) for i in range(self.window)]
        counters = cache.get_many(keys)
        if sum(counters.values()) >= self.max_count:
            self.full_callback(request)
            self.counter_increase(request)
            return True

        return False 
Example #10
Source File: submission.py    From eoj3 with MIT License 5 votes vote down vote up
def get_queryset(self):
    contest_participants = {user.user_id: user.comment for user in
                            ContestParticipant.objects.filter(contest=self.contest).select_related('user',
                                                                                                   'contest').all()}
    qs = self.contest.submission_set.filter(status=SubmissionStatus.ACCEPTED). \
      defer("code", "status_message", "status_detail").all()
    available = set(
      cache.get_many(list(map(lambda x: BALLOON_CACHE_NAME % (x.contest_id, x.author_id, x.problem_id), qs))).keys())
    self.contest.add_contest_problem_to_submissions(qs)
    for submission in qs:
      submission.username = contest_participants.get(submission.author_id, "INVALID")
      if BALLOON_CACHE_NAME % (submission.contest_id, submission.author_id, submission.problem_id) in available:
        submission.ok = True
    return qs