Add order_by kwarg to db.cache.cache_model() function.

The idea here is that in the event a duplicate key(s) exists for the model
being cached, sorting the records should ensure the "last one wins".  This
doesn't accomplish much, but at least the same record should be cached
during subsequent runs, which may help.

This was added to help track down duplicate product UPCs when they were
detected, but I wound up not actually using it in the end.  It's here for
next time...
This commit is contained in:
Lance Edgar 2015-03-10 12:37:37 -05:00
parent b2b23ab02a
commit 77a7bfba9b

View file

@ -37,10 +37,11 @@ class ModelCacher(object):
Generic model data caching class. Generic model data caching class.
""" """
def __init__(self, session, model_class, key='uuid', query_options=None, normalizer=None): def __init__(self, session, model_class, key='uuid', order_by=None, query_options=None, normalizer=None):
self.session = session self.session = session
self.model_class = model_class self.model_class = model_class
self.key = key self.key = key
self.order_by = order_by
self.query_options = query_options self.query_options = query_options
if normalizer is None: if normalizer is None:
self.normalize = lambda d: d self.normalize = lambda d: d
@ -53,6 +54,8 @@ class ModelCacher(object):
def query(self): def query(self):
q = self.session.query(self.model_class) q = self.session.query(self.model_class)
if self.order_by:
q = q.order_by(self.order_by)
if self.query_options: if self.query_options:
for option in self.query_options: for option in self.query_options:
q = q.options(option) q = q.options(option)