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

The following are 27 code examples of django.core.cache.cache.get_or_set(). 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 7 votes vote down vote up
def test_get_or_set_version(self):
        msg_re = r"get_or_set\(\) missing 1 required positional argument: 'default'"
        cache.get_or_set("brian", 1979, version=2)

        with pytest.raises(TypeError, match=msg_re):
            cache.get_or_set("brian")

        with pytest.raises(TypeError, match=msg_re):
            cache.get_or_set("brian", version=1)

        assert cache.get("brian", version=1) is None
        assert cache.get_or_set("brian", 42, version=1) == 42
        assert cache.get_or_set("brian", 1979, version=2) == 1979
        assert cache.get("brian", version=3) is None

    # Modified Django tests 
Example #2
Source File: boards.py    From open-synthesis with GNU General Public License v3.0 6 votes vote down vote up
def user_board_listing(request, account_id):
    """Return a paginated board listing view for account with account_id."""
    metric_timeout_seconds = 60 * 2

    queries = {
        # default to boards contributed to
        None: lambda x: ('contributed to', user_boards_contributed(x, viewing_user=request.user)),
        'created': lambda x: ('created', user_boards_created(x, viewing_user=request.user)),
        'evaluated': lambda x: ('evaluated', user_boards_evaluated(x, viewing_user=request.user)),
        'contribute': lambda x: ('contributed to', user_boards_contributed(x, viewing_user=request.user)),
    }

    user = get_object_or_404(User, pk=account_id)
    query = request.GET.get('query')
    verb, board_list = queries.get(query, queries[None])(user)
    desc = _('List of intelligence boards user {username} has {verb}').format(username=user.username, verb=verb)
    context = {
        'user': user,
        'boards': make_paginator(request, board_list),
        'contributors': cache.get_or_set('contributor_count', generate_contributor_count(), metric_timeout_seconds),
        'evaluators': cache.get_or_set('evaluator_count', generate_evaluator_count(), metric_timeout_seconds),
        'meta_description': desc,
        'verb': verb
    }
    return render(request, 'boards/user_boards.html', context) 
Example #3
Source File: backend.py    From django-auth-ldap with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _load_user_dn(self):
        """
        Populates self._user_dn with the distinguished name of our user.

        This will either construct the DN from a template in
        AUTH_LDAP_USER_DN_TEMPLATE or connect to the server and search for it.
        If we have to search, we'll cache the DN.

        """
        if self._using_simple_bind_mode():
            self._user_dn = self._construct_simple_user_dn()
        else:
            if self.settings.CACHE_TIMEOUT > 0:
                cache_key = valid_cache_key(
                    "django_auth_ldap.user_dn.{}".format(self._username)
                )
                self._user_dn = cache.get_or_set(
                    cache_key, self._search_for_user_dn, self.settings.CACHE_TIMEOUT
                )
            else:
                self._user_dn = self._search_for_user_dn() 
Example #4
Source File: site_views.py    From open-ledger with MIT License 6 votes vote down vote up
def about(request):
    """Information about the current site, its goals, and what content is loaded"""
    # Provider counts
    providers = cache.get_or_set(CACHE_STATS_NAME, [], CACHE_STATS_DURATION)
    if not providers:
        for provider in sorted(settings.PROVIDERS.keys()):
            s = Search()
            q = Q('term', provider=provider)
            s = s.query(q)
            response = s.execute()
            if response.hits.total > 0:
                data = settings.PROVIDERS[provider]
                total = intcomma(response.hits.total)
                data.update({'hits': total})
                providers.append(data)
        # All results
        s = Search()
        response = s.execute()
        total = intcomma(response.hits.total)
        providers.append({'display_name': 'Total', 'hits': total})
        cache.set(CACHE_STATS_NAME, providers)
    return render(request, "about.html", {'providers': providers}) 
Example #5
Source File: views.py    From foundation.mozilla.org with Mozilla Public License 2.0 6 votes vote down vote up
def category_view(request, slug):
    # If getting by slug fails, also try to get it by name.
    try:
        category = BuyersGuideProductCategory.objects.get(slug=slug)
    except ObjectDoesNotExist:
        category = get_object_or_404(BuyersGuideProductCategory, name__iexact=slug)

    key = f'products_category__{slug.replace(" ", "_")}'
    products = cache.get_or_set(
        key,
        lambda: [p.to_dict() for p in Product.objects.filter(product_category__in=[category]).distinct()],
        86400
    )

    products = filter_draft_products(request, products)

    return render(request, 'category_page.html', {
        'categories': BuyersGuideProductCategory.objects.all(),
        'category': category,
        'products': products,
        'mediaUrl': MEDIA_URL,
    }) 
Example #6
Source File: manager.py    From allianceauth with GNU General Public License v2.0 5 votes vote down vote up
def __group_id_to_name(g_id):
        def get_group_name():
            groups = DiscourseManager._get_groups()
            for g in groups:
                if g['id'] == g_id:
                    return g['name']
            raise KeyError("Group ID %s not found on Discourse" % g_id)

        return cache.get_or_set(DiscourseManager._generate_cache_group_id_key(g_id), get_group_name,
                                GROUP_CACHE_MAX_AGE) 
Example #7
Source File: boards.py    From open-synthesis with GNU General Public License v3.0 5 votes vote down vote up
def board_listing(request):
    """Return a paginated board listing view showing all boards and their popularity."""
    board_list = Board.objects.user_readable(request.user).order_by('-pub_date')
    metric_timeout_seconds = 60 * 2
    desc = _('List of intelligence boards on {name} and summary information').format(name=get_current_site(request).name)  # nopep8
    context = {
        'boards': make_paginator(request, board_list),
        'contributors': cache.get_or_set('contributor_count', generate_contributor_count(), metric_timeout_seconds),
        'evaluators': cache.get_or_set('evaluator_count', generate_evaluator_count(), metric_timeout_seconds),
        'meta_description': desc,
    }
    return render(request, 'boards/boards.html', context) 
Example #8
Source File: manager.py    From allianceauth with GNU General Public License v2.0 5 votes vote down vote up
def __group_name_to_id(name):
        name = DiscourseManager._sanitize_groupname(name)

        def get_or_create_group():
            groups = DiscourseManager._get_groups()
            for g in groups:
                if g['name'].lower() == name.lower():
                    return g['id']
            return DiscourseManager._create_group(name)['id']

        return cache.get_or_set(DiscourseManager._generate_cache_group_name_key(name), get_or_create_group,
                                GROUP_CACHE_MAX_AGE) 
Example #9
Source File: manager.py    From allianceauth with GNU General Public License v2.0 5 votes vote down vote up
def _group_name_to_id(name):
        name = DiscordOAuthManager._sanitize_group_name(name)

        def get_or_make_role():
            groups = DiscordOAuthManager._get_groups()
            for g in groups:
                if g['name'] == name:
                    return g['id']
            return DiscordOAuthManager._create_group(name)['id']
        return cache.get_or_set(DiscordOAuthManager._generate_cache_role_key(name), get_or_make_role, GROUP_CACHE_MAX_AGE) 
Example #10
Source File: admin_status.py    From allianceauth with GNU General Public License v2.0 5 votes vote down vote up
def get_notifications():
    response = {
        'notifications': list(),
    }
    try:
        notifications = cache.get_or_set('github_notification_issues', get_github_notification_issues,
                                         NOTIFICATION_CACHE_TIME)
        # Limit notifications to those posted by repo owners and members
        response['notifications'] += [n for n in notifications if n['author_association'] in ['OWNER', 'MEMBER']][:5]
    except requests.RequestException:
        logger.exception('Error while getting github notifications')
    return response 
Example #11
Source File: utils.py    From caluma with GNU General Public License v3.0 5 votes vote down vote up
def data_source_cache(timeout=None):
    def decorator(method):
        @functools.wraps(method)
        def handle_cache(self, info):
            key = f"data_source_{type(self).__name__}"

            get_data_method = functools.partial(method, self, info)
            return cache.get_or_set(key, get_data_method, timeout)

        return handle_cache

    return decorator 
Example #12
Source File: views.py    From foundation.mozilla.org with Mozilla Public License 2.0 5 votes vote down vote up
def bg_about_page(template_name):
    @redirect_to_default_cms_site
    def render_view(request):
        key = 'categories'
        categories = cache.get_or_set(
            key,
            lambda: BuyersGuideProductCategory.objects.all(),
            86400
        )

        return render(request, f"about/{template_name}.html", {
            'categories': categories,
        })

    return render_view 
Example #13
Source File: views.py    From foundation.mozilla.org with Mozilla Public License 2.0 5 votes vote down vote up
def buyersguide_home(request):
    products = cache.get_or_set(
        'sorted_product_dicts',
        lambda: sorted([p.to_dict() for p in Product.objects.all()], key=get_average_creepiness),
        86400
    )

    products = filter_draft_products(request, products)

    return render(request, 'buyersguide_home.html', {
        'categories': BuyersGuideProductCategory.objects.all(),
        'products': products,
        'mediaUrl': MEDIA_URL,
    }) 
Example #14
Source File: utils.py    From hawthorne with GNU Lesser General Public License v3.0 5 votes vote down vote up
def globals(request):
  cached = cache.get_or_set('servers', Server.objects.all, None)

  return {'root': settings.ROOT,
          'games': Server.SUPPORTED,
          'servers': cached} 
Example #15
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_or_set_callable(self):
        def my_callable():
            return "value"

        assert cache.get_or_set("mykey", my_callable) == "value" 
Example #16
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_or_set(self):
        assert cache.get("projector") is None
        assert cache.get_or_set("projector", 42) == 42
        assert cache.get("projector") == 42 
Example #17
Source File: consumers.py    From django-rest-framework-reactive with Apache License 2.0 5 votes vote down vote up
def observer_evaluate(self, message):
        """Execute observer evaluation on the worker or throttle."""
        observer_id = message['observer']
        throttle_rate = get_queryobserver_settings()['throttle_rate']
        if throttle_rate <= 0:
            await self._evaluate(observer_id)
            return

        cache_key = throttle_cache_key(observer_id)
        try:
            count = cache.incr(cache_key)
            # Ignore if delayed observer already scheduled.
            if count == 2:
                await self.channel_layer.send(
                    CHANNEL_MAIN,
                    {
                        'type': TYPE_POLL,
                        'observer': observer_id,
                        'interval': throttle_rate,
                    },
                )
        except ValueError:
            count = cache.get_or_set(cache_key, default=1, timeout=throttle_rate)
            # Ignore if cache was set and increased in another thread.
            if count == 1:
                await self._evaluate(observer_id) 
Example #18
Source File: views.py    From pythonic-news with GNU Affero General Public License v3.0 5 votes vote down vote up
def newest(request): # Done
    page = int(request.GET.get('p', 0))
    add_filter = {}
    add_q = []
    if 'submitted_by' in request.GET.keys():
        try:
            submitted_by = CustomUser.objects.get_by_natural_key(request.GET['submitted_by'])
            add_filter['user'] = submitted_by
        except CustomUser.DoesNotExist:
            raise Http404()
    if 'upvoted_by' in request.GET.keys():
        try:
            assert request.user.is_authenticated
            assert request.user.username == request.GET['upvoted_by']
        except AssertionError:
            return HttpResponseForbidden()
        add_filter['pk__in'] = Vote.objects.filter(vote=1, user=request.user).values('item')
        add_q.append(~Q(user=request.user))
    if 'site' in request.GET.keys():
        add_filter['domain'] = request.GET['site']
    stories = lambda: list(_newest(page=page, add_filter=add_filter, add_q=add_q))
    stories = cache.get_or_set("news-newest-%s"%(page), stories, timeout=TIMEOUT_SHORT) # two seconds
    if len(stories) < 1 and page != 0:
        back = _one_page_back(request)
        if back:
            return back
    return render(request, 'news/index.html', {'stories': stories, 'hide_text':True, 'page': page, 'rank_start': page*settings.PAGING_SIZE}) 
Example #19
Source File: views.py    From pythonic-news with GNU Affero General Public License v3.0 5 votes vote down vote up
def ask(request):
    page = int(request.GET.get('p', 0))
    stories = lambda: list(_front_page(page=page, add_filter={'is_ask': True}))
    stories = cache.get_or_set("news-ask-%s"%(page), stories, timeout=TIMEOUT_MEDIUM) # one minute
    if len(stories) < 1 and page != 0:
        back = _one_page_back(request)
        if back:
            return back
    return render(request, 'news/index.html', {'stories': stories, 'hide_text':True, 'page': page, 'rank_start': page*settings.PAGING_SIZE}) 
Example #20
Source File: views.py    From pythonic-news with GNU Affero General Public License v3.0 5 votes vote down vote up
def show(request):
    page = int(request.GET.get('p', 0))
    stories = cache.get_or_set("news-show-%s"%(page), lambda: list(_front_page(page=page, add_filter={'is_show': True})), timeout=TIMEOUT_MEDIUM) # one minute
    if len(stories) < 1 and page != 0:
        back = _one_page_back(request)
        if back:
            return back
    return render(request, 'news/index.html', {'stories': stories, 'hide_text':True, 'page': page, 'rank_start': page*settings.PAGING_SIZE}) 
Example #21
Source File: views.py    From pythonic-news with GNU Affero General Public License v3.0 5 votes vote down vote up
def index(request):
    page = int(request.GET.get('p', 0))
    stories = cache.get_or_set("news-index-%s"%(page), lambda: list(_front_page(page=page)), timeout=TIMEOUT_MEDIUM) # one minute
    if len(stories) < 1 and page != 0:
        back = _one_page_back(request)
        if back:
            return back
    return render(request, 'news/index.html', {'stories': stories, 'hide_text':True, 'page': page, 'rank_start': page*settings.PAGING_SIZE}) 
Example #22
Source File: cache.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_data(self, **kwargs):  # pylint: disable=arguments-differ
        return cache.get_or_set(API_TIMESTAMP_KEY, time.time, None) 
Example #23
Source File: authentication.py    From timed-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def cached_request(self, method, token, cache_prefix):
        token_hash = hashlib.sha256(force_bytes(token)).hexdigest()

        func = functools.partial(method, token, None, None)

        return cache.get_or_set(
            f"{cache_prefix}.{token_hash}",
            func,
            timeout=settings.OIDC_BEARER_TOKEN_REVALIDATION_TIME,
        ) 
Example #24
Source File: views.py    From desec-stack with MIT License 5 votes vote down vote up
def list(self, request):
        key = 'desecapi.views.serials'
        serials = cache.get(key)
        if serials is None:
            serials = get_serials()
            cache.get_or_set(key, serials, timeout=15)
        return Response(serials) 
Example #25
Source File: list.py    From django-idcops with Apache License 2.0 5 votes vote down vote up
def _config(self):
        key = utils.make_template_fragment_key("{}.{}.{}".format(
            self.request.user.id, self.model_name, 'list'))
        data = cache.get_or_set(key, 
            get_user_config(self.request.user, 'list', self.model), 180)
        return data 
Example #26
Source File: views.py    From caluma with GNU General Public License v3.0 4 votes vote down vote up
def get_user(self, request):
        token = self.get_bearer_token(request)

        if token is None:
            return models.AnonymousUser()

        if not settings.OIDC_USERINFO_ENDPOINT:
            raise ImproperlyConfigured(
                'Token was provided, but "OIDC_USERINFO_ENDPOINT" is not set.'
            )

        userinfo_method = functools.partial(self.get_userinfo, token=token)
        # token might be too long for key so we use hash sum instead.
        hashsum_token = hashlib.sha256(force_bytes(token)).hexdigest()

        try:
            userinfo = cache.get_or_set(
                f"authentication.userinfo.{hashsum_token}",
                userinfo_method,
                timeout=settings.OIDC_BEARER_TOKEN_REVALIDATION_TIME,
            )
            return models.OIDCUser(token=token, userinfo=userinfo)
        except requests.HTTPError as e:
            try:
                if (
                    e.response.status_code in [401, 403]
                    and settings.OIDC_INTROSPECT_ENDPOINT
                ):
                    introspect_method = functools.partial(
                        self.get_introspection, token=token
                    )
                    introspection = cache.get_or_set(
                        f"authentication.introspect.{hashsum_token}",
                        introspect_method,
                        timeout=settings.OIDC_BEARER_TOKEN_REVALIDATION_TIME,
                    )
                    if "client_id" not in introspection:
                        response = HttpResponse(status=401)
                        raise HttpError(response)
                    return models.OIDCUser(token=token, introspection=introspection)
                else:
                    raise e
            except requests.HTTPError as internal_exception:
                # convert request error to django http error
                response = HttpResponse()
                response.status_code = internal_exception.response.status_code
                raise HttpError(response, message=str(internal_exception)) 
Example #27
Source File: admin_status.py    From allianceauth with GNU General Public License v2.0 4 votes vote down vote up
def get_version_info():
    response = {
        'latest_major': True,
        'latest_minor': True,
        'latest_patch': True,
        'current_version': __version__,
    }
    try:
        tags = cache.get_or_set('github_release_tags', get_github_tags, TAG_CACHE_TIME)
        current_ver = semver.Version.coerce(__version__)

        # Set them all to the current version to start
        # If the server has only earlier or the same version
        # then this will become the major/minor/patch versions
        latest_major = current_ver
        latest_minor = current_ver
        latest_patch = current_ver

        response.update({
            'latest_major_version': str(latest_major),
            'latest_minor_version': str(latest_minor),
            'latest_patch_version': str(latest_patch),
        })

        for tag in tags:
            tag_name = tag.get('tag_name')
            if tag_name[0] == 'v':
                # Strip 'v' off front of verison if it exists
                tag_name = tag_name[1:]
            try:
                tag_ver = semver.Version.coerce(tag_name)
            except ValueError:
                tag_ver = semver.Version('0.0.0', partial=True)
            if tag_ver > current_ver:
                if latest_major is None or tag_ver > latest_major:
                    latest_major = tag_ver
                    response['latest_major_version'] = tag_name
                    response['latest_major_url'] = tag['html_url']
                if tag_ver.major > current_ver.major:
                    response['latest_major'] = False
                elif tag_ver.major == current_ver.major:
                    if latest_minor is None or tag_ver > latest_minor:
                        latest_minor = tag_ver
                        response['latest_minor_version'] = tag_name
                        response['latest_minor_url'] = tag['html_url']
                    if tag_ver.minor > current_ver.minor:
                        response['latest_minor'] = False
                    elif tag_ver.minor == current_ver.minor:
                        if latest_patch is None or tag_ver > latest_patch:
                            latest_patch = tag_ver
                            response['latest_patch_version'] = tag_name
                            response['latest_patch_url'] = tag['html_url']
                        if tag_ver.patch > current_ver.patch:
                            response['latest_patch'] = False

    except requests.RequestException:
        logger.exception('Error while getting github release tags')
    return response