Memoises everything else that needs to be memoised.

This commit is contained in:
Christopher Neugebauer 2016-05-01 12:42:06 +10:00
parent 3d635521eb
commit efb73e7a68
5 changed files with 13 additions and 18 deletions

View file

@ -107,16 +107,3 @@ class BatchController(object):
cache = {} cache = {}
cache[cls._NESTING_KEY] = 0 cache[cls._NESTING_KEY] = 0
return cache return cache
'''
TODO: memoise CartController.for_user
TODO: memoise user_remainders (Product, Category)
TODO: memoise _filtered_flags
TODO: memoise FlagCounter.count() (doesn't take user, but it'll do for now)
TODO: memoise _filtered_discounts
Tests:
- ``end_batch`` behaviour for CartController (use for_user *A LOT*)
- discounts not calculated until outermost batch point exits.
- Revision number shouldn't change until outermost batch point exits.
'''

View file

@ -7,6 +7,7 @@ from django.db.models import Sum
from django.db.models import When from django.db.models import When
from django.db.models import Value from django.db.models import Value
from .batch import BatchController
class AllProducts(object): class AllProducts(object):
pass pass
@ -39,6 +40,7 @@ class CategoryController(object):
return set(i.category for i in available) return set(i.category for i in available)
@classmethod @classmethod
@BatchController.memoise
def user_remainders(cls, user): def user_remainders(cls, user):
''' '''

View file

@ -1,6 +1,8 @@
import itertools import itertools
from conditions import ConditionController from .batch import BatchController
from .conditions import ConditionController
from registrasion.models import commerce from registrasion.models import commerce
from registrasion.models import conditions from registrasion.models import conditions
@ -10,7 +12,6 @@ from django.db.models import Sum
from django.db.models import Value from django.db.models import Value
from django.db.models import When from django.db.models import When
class DiscountAndQuantity(object): class DiscountAndQuantity(object):
''' Represents a discount that can be applied to a product or category ''' Represents a discount that can be applied to a product or category
for a given user. for a given user.
@ -99,6 +100,7 @@ class DiscountController(object):
return discounts return discounts
@classmethod @classmethod
@BatchController.memoise
def _filtered_clauses(cls, user): def _filtered_clauses(cls, user):
''' '''

View file

@ -6,6 +6,7 @@ from collections import namedtuple
from django.db.models import Count from django.db.models import Count
from django.db.models import Q from django.db.models import Q
from .batch import BatchController
from .conditions import ConditionController from .conditions import ConditionController
from registrasion.models import conditions from registrasion.models import conditions
@ -115,7 +116,7 @@ class FlagController(object):
if not met and product not in messages: if not met and product not in messages:
messages[product] = message messages[product] = message
total_flags = FlagCounter.count() total_flags = FlagCounter.count(user)
valid = {} valid = {}
@ -158,6 +159,7 @@ class FlagController(object):
return error_fields return error_fields
@classmethod @classmethod
@BatchController.memoise
def _filtered_flags(cls, user): def _filtered_flags(cls, user):
''' '''
@ -209,11 +211,11 @@ _ConditionsCount = namedtuple(
) )
# TODO: this should be cacheable.
class FlagCounter(_FlagCounter): class FlagCounter(_FlagCounter):
@classmethod @classmethod
def count(cls): @BatchController.memoise
def count(cls, user):
# Get the count of how many conditions should exist per product # Get the count of how many conditions should exist per product
flagbases = conditions.FlagBase.objects flagbases = conditions.FlagBase.objects

View file

@ -9,6 +9,7 @@ from django.db.models import Value
from registrasion.models import commerce from registrasion.models import commerce
from registrasion.models import inventory from registrasion.models import inventory
from .batch import BatchController
from .category import CategoryController from .category import CategoryController
from .flag import FlagController from .flag import FlagController
@ -55,6 +56,7 @@ class ProductController(object):
return out return out
@classmethod @classmethod
@BatchController.memoise
def user_remainders(cls, user): def user_remainders(cls, user):
''' '''