Skip to content

Commit

Permalink
try to skip table invalidations when tables are in the blacklist (or …
Browse files Browse the repository at this point in the history
…not in the whitelist). leave as it is, #9 & #10
  • Loading branch information
jmoiron committed Oct 24, 2012
1 parent 49dc914 commit 0f16eb1
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions johnny/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ def disable():

patch,unpatch = enable,disable

def resolve_table(x):
"""Return a table name for x, where x is either a model instance or a string."""
if isinstance(x, basestring):
return x
return x._meta.db_table


def invalidate(*tables, **kwargs):
"""Invalidate the current generation for one or more tables. The arguments
can be either strings representing database table names or models. Pass in
kwarg ``using`` to set the database."""
backend = get_backend()
db = kwargs.get('using', 'default')

def resolve(x):
if isinstance(x, basestring):
return x
return x._meta.db_table

if backend._patched:
for t in map(resolve, tables):
for t in map(resolve_table, tables):
backend.keyhandler.invalidate_table(t, db)


Expand Down Expand Up @@ -138,7 +140,7 @@ def parse_tables_from_sql(sql):
table_re = re.compile(r'(?:FROM|JOIN) `(?P<table>\w+)`')
return table_re.findall(sql)

tables = list(query.tables)
tables = list(query.table_map)
if (query.where and query.where.children and
isinstance(query.where.children[0], WhereNode)):
where_node = query.where.children[0]
Expand Down Expand Up @@ -397,9 +399,13 @@ def newfun(cls, *args, **kwargs):
#are not populated.
tables = [cls.query.model._meta.db_table]
else:
#if cls.query.tables != list(cls.query.table_map):
# pass
#tables = list(cls.query.table_map)
tables = cls.query.tables
for table in tables:
self.keyhandler.invalidate_table(table, db)
if not disallowed_table(table):
self.keyhandler.invalidate_table(table, db)
return ret
return newfun

Expand Down Expand Up @@ -442,15 +448,18 @@ def unpatch(self):

def invalidate_m2m(self, instance, **kwargs):
if self._patched:
self.keyhandler.invalidate_table(instance)
table = resolve_table(instance)
if not disallowed_table(table):
self.keyhandler.invalidate_table(instance)

def invalidate(self, instance, **kwargs):
if self._patched:
self.keyhandler.invalidate_table(instance._meta.db_table)
table = resolve_table(instance)
if not disallowed_table(table):
self.keyhandler.invalidate_table(table)

tables = set()
tables.add(instance._meta.db_table)
self.keyhandler.invalidate_table(instance._meta.db_table)
tables.add(table)

try:
instance._meta._related_objects_cache
Expand All @@ -461,7 +470,8 @@ def invalidate(self, instance, **kwargs):
obj_table = obj.model._meta.db_table
if obj_table not in tables:
tables.add(obj_table)
self.keyhandler.invalidate_table(obj_table)
if not disallowed_table(obj_table):
self.keyhandler.invalidate_table(obj_table)

def _handle_signals(self):
post_save.connect(self.invalidate, sender=None)
Expand All @@ -474,6 +484,7 @@ def flush_query_cache(self):
tables = connection.introspection.table_names()
#seen_models = connection.introspection.installed_models(tables)
for table in tables:
# we want this to just work, so invalidate even things in blacklist
self.keyhandler.invalidate_table(table)


Expand Down Expand Up @@ -542,7 +553,8 @@ def newfun(cls, result_type=MULTI):
elif tables and sql.startswith('UPDATE'):
# issue #1 in bitbucket, not invalidating on update
for table in tables:
self.keyhandler.invalidate_table(table)
if not disallowed_table(table):
self.keyhandler.invalidate_table(table)
return result
return newfun

Expand Down

0 comments on commit 0f16eb1

Please sign in to comment.