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 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_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 _cache_shop_configuration(shop): """ Cache global or shop specific configuration. Global configuration (`shop` is ``None``) is read first, then `shop` based configuration is updated over that. :param shop: Shop to cache configuration for, or None :type shop: shoop.core.models.Shop|None :return: Cached configuration :rtype: dict """ configuration = {} configuration.update(_get_configuration_from_db(None)) if shop: configuration.update(_get_configuration_from_db(shop)) cache.set(_get_cache_key(shop), configuration) return configuration
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 set(shop, key, value): """ Set configuration item value for a shop or globally. If given `shop` is ``None``, the value of given `key` is set globally for all shops. Otherwise sets a shop specific value which overrides the global value in configuration of the specified shop. :param shop: Shop to set value for, or None to set a global value :type shop: shoop.core.models.Shop|None :param key: Name of the key to set :type key: str :param value: Value to set. Note: Must be JSON serializable. :type value: Any """ ConfigurationItem.objects.update_or_create( shop=shop, key=key, defaults={"value": value}) if shop: cache.set(_get_cache_key(shop), None) else: cache.bump_version(_SHOP_CONF_NAMESPACE)
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] )