Skip to content

Commit

Permalink
moved caching helpers to caching utils, added cache invalidation for …
Browse files Browse the repository at this point in the history
…properties
  • Loading branch information
pigletto committed Feb 5, 2013
1 parent fb2ef97 commit 6f238ec
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 55 deletions.
3 changes: 1 addition & 2 deletions lfs/caching/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.db.models.signals import pre_delete

# lfs imports
from lfs.caching.utils import clear_cache
from lfs.caching.utils import clear_cache, invalidate_cache_group_id
from lfs.cart.models import Cart
from lfs.catalog.models import Category
from lfs.catalog.models import Product
Expand All @@ -23,7 +23,6 @@
from lfs.order.models import OrderItem
from lfs.page.models import Page
from lfs.shipping.models import ShippingMethod
from lfs.core.utils import invalidate_cache_group_id

# reviews imports
from reviews.signals import review_added
Expand Down
22 changes: 22 additions & 0 deletions lfs/caching/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,25 @@ def clear_cache():
cache._expire_info.clear()
except AttributeError:
pass


def get_cache_group_id(group_code):
""" Get id for group_code that is stored in cache. This id is supposed to be included in cache key for all items
from specific group.
"""
cache_group_key = '%s-%s-GROUP' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_code)
group_id = cache.get(cache_group_key, 0)
if group_id == 0:
group_id = 1
cache.set(cache_group_key, group_id, cache.default_timeout * 2)
return group_id


def invalidate_cache_group_id(group_code):
""" Invalidation of group is in fact only incrementation of group_id
"""
cache_group_key = '%s-%s-GROUP' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_code)
try:
cache.incr(cache_group_key)
except ValueError, e:
pass
42 changes: 21 additions & 21 deletions lfs/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.conf import settings

# lfs imports
from lfs.caching.utils import get_cache_group_id
import lfs.catalog.utils
from lfs.core.fields.thumbs import ImageWithThumbsField
from lfs.core.managers import ActiveManager
Expand Down Expand Up @@ -785,10 +786,7 @@ def get_categories(self, with_parents=False):
if categories is not None:
return categories

if self.is_variant():
object = self.parent
else:
object = self
object = self.get_parent()

if with_parents:
categories = []
Expand All @@ -807,10 +805,7 @@ def get_category(self):
"""
Returns the first category of a product.
"""
if self.is_variant():
object = self.parent
else:
object = self
object = self.get_parent()

try:
return object.get_categories()[0]
Expand Down Expand Up @@ -1100,7 +1095,10 @@ def get_variant_properties(self):
Returns the property value of a variant in the correct ordering of the
properties.
"""
cache_key = "%s-variant-properties-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id)
pid = self.get_parent().pk
group_id = get_cache_group_id('properties-%s' % pid)

cache_key = "%s-variant-properties-%s-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_id, self.id)

properties = cache.get(cache_key)
if properties:
Expand Down Expand Up @@ -1208,7 +1206,9 @@ def get_variant_properties_for_parent(self):
Returns the property value of a variant in the correct ordering of the
properties. Traverses through all parent properties
"""
cache_key = "%s-variant-properties-for-parent-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id)
pid = self.get_parent().pk
group_id = get_cache_group_id('properties-%s' % pid)
cache_key = "%s-variant-properties-for-parent-%s-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_id, self.id)

properties = cache.get(cache_key)
if properties:
Expand All @@ -1223,12 +1223,14 @@ def has_option(self, property, option):
"""
Returns True if the variant has the given property / option combination.
"""
options = cache.get("%s-productpropertyvalue%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id))
pid = self.get_parent().pk
group_id = get_cache_group_id('properties-%s' % pid)
options = cache.get("%s-%s-productpropertyvalue%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_id, self.id))
if options is None:
options = {}
for pvo in self.property_values.all():
options[pvo.property_id] = pvo.value
cache.set("%s-productpropertyvalue%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id), options)
cache.set("%s-%s-productpropertyvalue%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_id, self.id), options)

try:
return options[property.id] == str(option.id)
Expand Down Expand Up @@ -1272,11 +1274,7 @@ def get_price_calculator(self, request):
Returns the price calculator class as defined in LFS_PRICE_CALCULATORS
in settings.
"""
if self.is_variant():
obj = self.parent
else:
obj = self

obj = self.get_parent()
if obj.price_calculator is not None:
price_calculator = obj.price_calculator
else:
Expand Down Expand Up @@ -1606,10 +1604,7 @@ def get_cheapest_for_sale_price_gross(self, request):
"""
Returns the min price and min base price as dict.
"""
if self.is_variant():
product = self.parent
else:
product = self
product = self.get_parent()

prices = []
for variant in Product.objects.filter(parent=product, active=True):
Expand Down Expand Up @@ -1824,6 +1819,11 @@ def is_product_with_variants(self):
"""
return self.sub_type == PRODUCT_WITH_VARIANTS

def get_parent(self):
if self.is_variant():
return self.parent
return self

def is_variant(self):
"""
Returns True if product is variant.
Expand Down
4 changes: 2 additions & 2 deletions lfs/core/templatetags/lfs_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from lfs.order.settings import SUBMITTED, PAYMENT_FLAGGED, PAYMENT_FAILED
import lfs.utils.misc
import logging
from lfs.caching.utils import lfs_get_object_or_404
from lfs.caching.utils import lfs_get_object_or_404, get_cache_group_id
from lfs.catalog.models import Category
from lfs.catalog.settings import CONFIGURABLE_PRODUCT, VARIANT
from lfs.catalog.settings import CATEGORY_VARIANT_CHEAPEST_PRICES
Expand Down Expand Up @@ -230,7 +230,7 @@ def product_navigation(context, product):

# prepare cache key for product_navigation group
# used to invalidate cache for all product_navigations at once
pn_cache_key = lfs.core.utils.get_cache_group_id('product_navigation')
pn_cache_key = get_cache_group_id('product_navigation')

# if there is last_manufacturer then product was visited from manufacturer view
# as category view removes last_manufacturer from the session
Expand Down
24 changes: 0 additions & 24 deletions lfs/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# django imports
from django.conf import settings
from django.contrib.redirects.models import Redirect
from django.core.cache import cache
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.utils import simplejson
Expand All @@ -18,8 +17,6 @@
from django.shortcuts import render_to_response

# lfs imports
import lfs.catalog.utils
from lfs.caching.utils import lfs_get_object_or_404
from lfs.core.models import Shop
from lfs.catalog.models import Category

Expand Down Expand Up @@ -436,24 +433,3 @@ def lfs_pagination(request, current_page, url='', getparam='start'):
to_return['getvars'] = "&%s" % getvars.urlencode()
return to_return


def get_cache_group_id(group_code):
""" Get id for group_code that is stored in cache. This id is supposed to be included in cache key for all items
from specific group.
"""
cache_group_key = '%s-%s-GROUP' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_code)
group_id = cache.get(cache_group_key, 0)
if group_id == 0:
group_id = 1
cache.set(cache_group_key, group_id, cache.default_timeout * 2)
return group_id


def invalidate_cache_group_id(group_code):
""" Invalidation of group is in fact only incrementation of group_id
"""
cache_group_key = '%s-%s-GROUP' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_code)
try:
cache.incr(cache_group_key)
except ValueError, e:
pass
25 changes: 19 additions & 6 deletions lfs/manage/product/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from django.utils.translation import ugettext_lazy as _

# lfs imports
import lfs.catalog.utils
from lfs.caching.utils import lfs_get_object_or_404
from lfs.caching.utils import lfs_get_object_or_404, get_cache_group_id, invalidate_cache_group_id
from lfs.core.signals import product_changed
from lfs.catalog.models import Product
from lfs.catalog.models import ProductPropertyValue
Expand Down Expand Up @@ -113,8 +112,9 @@ def manage_variants(request, product_id, as_string=False, template_name="manage/
default_variant_form = DefaultVariantForm(instance=product)
category_variant_form = CategoryVariantForm(instance=product)

# TODO: Delete cache when delete options
cache_key = "%s-manage-properties-variants-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, product_id)
pid = product.get_parent().pk
group_id = get_cache_group_id('properties-%s' % pid)
cache_key = "%s-manage-properties-variants-%s-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, group_id, product_id)
variants = cache.get(cache_key)
# Get all properties. We need to traverse through all property / options
# in order to select the options of the current variant.
Expand Down Expand Up @@ -215,6 +215,8 @@ def add_property(request, product_id):
product_property.save()

product_changed.send(product)
pid = product.get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = [["#variants", manage_variants(request, product_id, as_string=True)]]

Expand All @@ -238,6 +240,8 @@ def delete_property(request, product_id, property_id):
else:
property.delete()
product_changed.send(product)
pid = product.get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = (("#variants", manage_variants(request, product_id, as_string=True)),)

Expand Down Expand Up @@ -271,6 +275,8 @@ def change_property_position(request):
product_property.save()

_refresh_property_positions(product_id)
pid = Product.objects.get(pk=product_id).get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = (("#variants", manage_variants(request, product_id, as_string=True)),)

Expand Down Expand Up @@ -307,7 +313,10 @@ def add_property_option(request, product_id):
option.position = i
option.save()

product_changed.send(Product.objects.get(pk=product_id))
product = Product.objects.get(pk=product_id)
product_changed.send(product)
pid = product.get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = [["#variants", manage_variants(request, product_id, as_string=True)]]

Expand All @@ -331,6 +340,8 @@ def delete_property_option(request, product_id, option_id):
else:
property_option.delete()
product_changed.send(product)
pid = product.get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = (("#variants", manage_variants(request, product_id, as_string=True)),)

Expand Down Expand Up @@ -558,6 +569,8 @@ def update_variants(request, product_id):

# Send a signal to update cache
product_changed.send(product)
pid = product.get_parent().pk
invalidate_cache_group_id('properties-%s' % pid)

html = (
("#variants", manage_variants(request, product_id, as_string=True)),
Expand Down Expand Up @@ -642,7 +655,7 @@ def _selectable_products_inline(request, product):
AMOUNT = 20
products = _get_filtered_products_for_product_view(request)
paginator = Paginator(products, AMOUNT)
temp = product.parent if product.is_variant() else product
temp = product.get_parent()
page = get_current_page(request, products, temp, AMOUNT)

try:
Expand Down

0 comments on commit 6f238ec

Please sign in to comment.