Exemple #1
0
def test_cache_api():
    key = "test_prefix:123"
    value = "456"
    cache.set(key, value)
    assert cache.get(key) == value
    cache.bump_version(key)
    assert cache.get(key, default="derp") == "derp"  # version was bumped, so no way this is there
    cache.set(key, value)
    assert cache.get(key) == value
def test_configuration_cache():
    cache.clear()
    shop = get_default_shop()
    configuration.set(None, "key1", "test1")
    configuration.set(shop, "key2", "test2")

    # Shop configurations cache should be bumped
    assert cache.get(configuration._get_cache_key(shop)) is None
    configuration.get(shop, "key1")
    # Now shop configurations and key2 should found from cache
    assert cache.get(configuration._get_cache_key(shop)).get("key2") == "test2"
def test_configuration_cache():
    cache.clear()
    shop = get_default_shop()
    configuration.set(None, "key1", "test1")
    configuration.set(shop, "key2", "test2")

    # Shop configurations cache should be bumped
    assert cache.get(configuration._get_cache_key(shop)) is None
    configuration.get(shop, "key1")
    # Now shop configurations and key2 should found from cache
    assert cache.get(configuration._get_cache_key(shop)).get("key2") == "test2"
Exemple #4
0
def get_search_product_ids(request, query):
    query = query.strip().lower()
    cache_key = "simple_search:%s" % hashlib.sha1(force_bytes(query)).hexdigest()
    product_ids = cache.get(cache_key)
    if product_ids is None:
        product_ids = Product.objects.filter(
            Q(translations__name__icontains=query) |
            Q(translations__description__icontains=query) |
            Q(translations__keywords__icontains=query)
        ).distinct().values_list("pk", flat=True)
        cache.set(cache_key, product_ids, 60 * 5)
    return product_ids
def _get_configuration(shop):
    """
    Get global or shop specific configuration with caching.

    :param shop: Shop to get configuration for, or None
    :type shop: shoop.core.models.Shop|None
    :return: Global or shop specific configuration
    :rtype: dict
    """
    configuration = cache.get(_get_cache_key(shop))
    if configuration is None:
        configuration = _cache_shop_configuration(shop)
    return configuration
Exemple #6
0
def _get_configuration(shop):
    """
    Get global or shop specific configuration with caching.

    :param shop: Shop to get configuration for, or None
    :type shop: shoop.core.models.Shop|None
    :return: Global or shop specific configuration
    :rtype: dict
    """
    configuration = cache.get(_get_cache_key(shop))
    if configuration is None:
        configuration = _cache_shop_configuration(shop)
    return configuration
Exemple #7
0
def get_search_product_ids(request, query):
    query = query.strip().lower()
    cache_key = "simple_search:%s" % hashlib.sha1(
        force_bytes(query)).hexdigest()
    product_ids = cache.get(cache_key)
    if product_ids is None:
        product_ids = Product.objects.filter(
            Q(translations__name__icontains=query)
            | Q(translations__description__icontains=query)
            | Q(translations__keywords__icontains=query)).distinct(
            ).values_list("pk", flat=True)
        cache.set(cache_key, product_ids, 60 * 5)
    return product_ids
def get_best_selling_product_info(shop_ids, cutoff_days=30):
    shop_ids = sorted(map(int, shop_ids))
    cutoff_date = datetime.date.today() - datetime.timedelta(days=cutoff_days)
    cache_key = "best_sellers:%r_%s" % (shop_ids, cutoff_date)
    sales_data = cache.get(cache_key)
    if sales_data is None:
        sales_data = (OrderLine.objects.filter(
            order__shop_id__in=shop_ids,
            order__order_date__gte=cutoff_date).exclude(
                product=None).values("product").annotate(
                    n=Sum("quantity")).order_by("-n")[:100].values_list(
                        "product", "n"))
        cache.set(cache_key, sales_data, 3 * 60 * 60)  # three hours
    return sales_data
def get_products_ordered_with(prod, count=20, request=None, language=None):
    cache_key = "ordered_with:%d" % prod.pk
    product_ids = cache.get(cache_key)
    if product_ids is None:
        # XXX: could this be optimized more? (and does it matter?)
        order_ids = (OrderLine.objects.filter(
            product=prod, type=OrderLineType.PRODUCT).values_list("order__id",
                                                                  flat=True))
        product_ids = (OrderLine.objects.filter(
            order_id__in=order_ids).exclude(
                product=prod).distinct().values_list("product", flat=True))
        cache.set(cache_key, set(product_ids), 4 * 60 * 60)
    return (Product.objects.all_visible(
        request,
        language=language).filter(id__in=product_ids).order_by("?")[:count])
def get_best_selling_product_info(shop_ids, cutoff_days=30):
    shop_ids = sorted(map(int, shop_ids))
    cutoff_date = datetime.date.today() - datetime.timedelta(days=cutoff_days)
    cache_key = "best_sellers:%r_%s" % (shop_ids, cutoff_date)
    sales_data = cache.get(cache_key)
    if sales_data is None:
        sales_data = (
            OrderLine.objects
            .filter(
                order__shop_id__in=shop_ids,
                order__order_date__gte=cutoff_date
            )
            .exclude(product=None)
            .values("product")
            .annotate(n=Sum("quantity"))
            .order_by("-n")[:100]
            .values_list("product", "n")
        )
        cache.set(cache_key, sales_data, 3 * 60 * 60)  # three hours
    return sales_data
def get_products_ordered_with(prod, count=20, request=None, language=None):
    cache_key = "ordered_with:%d" % prod.pk
    product_ids = cache.get(cache_key)
    if product_ids is None:
        # XXX: could this be optimized more? (and does it matter?)
        order_ids = (
            OrderLine.objects.filter(product=prod, type=OrderLineType.PRODUCT)
            .values_list("order__id", flat=True)
        )
        product_ids = (
            OrderLine.objects
            .filter(order_id__in=order_ids)
            .exclude(product=prod)
            .distinct()
            .values_list("product", flat=True)
        )
        cache.set(cache_key, set(product_ids), 4 * 60 * 60)
    return (
        Product.objects
        .all_visible(request, language=language)
        .filter(id__in=product_ids)
        .order_by("?")[:count]
    )