mirror of
https://github.com/Hopiu/django-cachalot.git
synced 2026-05-10 05:34:45 +00:00
Python optimisations.
This commit is contained in:
parent
1d4e8f8c8b
commit
7f86cfe1cc
2 changed files with 10 additions and 8 deletions
|
|
@ -47,6 +47,9 @@ def _unset_raw_connection(original):
|
|||
return inner
|
||||
|
||||
|
||||
TUPLE_OR_LIST = (tuple, list)
|
||||
|
||||
|
||||
def _get_result_or_execute_query(execute_query_func, cache_key,
|
||||
table_cache_keys):
|
||||
cache = cachalot_caches.get_cache()
|
||||
|
|
@ -68,8 +71,7 @@ def _get_result_or_execute_query(execute_query_func, cache_key,
|
|||
return result
|
||||
|
||||
result = execute_query_func()
|
||||
if isinstance(result, Iterable) \
|
||||
and not isinstance(result, (tuple, list)):
|
||||
if isinstance(result, Iterable) and result.__class__ not in TUPLE_OR_LIST:
|
||||
result = list(result)
|
||||
|
||||
cache.set(cache_key, (time(), result), None)
|
||||
|
|
@ -83,7 +85,7 @@ def _patch_compiler(original):
|
|||
def inner(compiler, *args, **kwargs):
|
||||
execute_query_func = lambda: original(compiler, *args, **kwargs)
|
||||
if not cachalot_settings.CACHALOT_ENABLED \
|
||||
or isinstance(compiler, WRITE_COMPILERS):
|
||||
or compiler.__class__ in WRITE_COMPILERS:
|
||||
return execute_query_func()
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -70,8 +70,8 @@ def _get_tables_from_sql(connection, lowercased_sql):
|
|||
|
||||
def _find_subqueries(children):
|
||||
for child in children:
|
||||
if isinstance(child, SubqueryConstraint):
|
||||
if isinstance(child.query_object, Query):
|
||||
if child.__class__ is SubqueryConstraint:
|
||||
if child.query_object.__class__ is Query:
|
||||
yield child.query_object
|
||||
else:
|
||||
yield child.query_object.query
|
||||
|
|
@ -80,9 +80,9 @@ def _find_subqueries(children):
|
|||
if DJANGO_GTE_1_7:
|
||||
if hasattr(child, 'rhs'):
|
||||
rhs = child.rhs
|
||||
elif isinstance(child, tuple):
|
||||
elif child.__class__ is tuple:
|
||||
rhs = child[-1]
|
||||
if isinstance(rhs, Query):
|
||||
if rhs.__class__ is Query:
|
||||
yield rhs
|
||||
elif hasattr(rhs, 'query'):
|
||||
yield rhs.query
|
||||
|
|
@ -102,7 +102,7 @@ def _get_tables(query, db_alias):
|
|||
for subquery in subquery_constraints:
|
||||
tables.update(_get_tables(subquery, db_alias))
|
||||
if query.extra_select or hasattr(query, 'subquery') \
|
||||
or any(isinstance(c, ExtraWhere) for c in query.where.children):
|
||||
or any(c.__class__ is ExtraWhere for c in query.where.children):
|
||||
sql = query.get_compiler(db_alias).as_sql()[0].lower()
|
||||
additional_tables = _get_tables_from_sql(connections[db_alias], sql)
|
||||
tables.update(additional_tables)
|
||||
|
|
|
|||
Loading…
Reference in a new issue