-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merges invalidate_all, invalidate_tables and invalidate_models, while…
… optimising it.
- Loading branch information
1 parent
d93e4c3
commit abc00b1
Showing
5 changed files
with
39 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,64 @@ | ||
# coding: utf-8 | ||
|
||
from __future__ import unicode_literals | ||
from collections import defaultdict | ||
|
||
from django.conf import settings | ||
from django.db import connections | ||
from django.utils.six import string_types | ||
|
||
from .cache import cachalot_caches | ||
from .utils import _get_table_cache_key, _invalidate_table_cache_keys | ||
|
||
|
||
__all__ = ('invalidate_tables', 'invalidate_models', 'invalidate_all') | ||
__all__ = ('invalidate',) | ||
|
||
|
||
def _aliases_iterator(cache_alias, db_alias): | ||
def _get_table_cache_keys_per_cache(tables, cache_alias, db_alias): | ||
no_tables = not tables | ||
cache_aliases = settings.CACHES if cache_alias is None else (cache_alias,) | ||
db_aliases = settings.DATABASES if db_alias is None else (db_alias,) | ||
for cache_alias in cache_aliases: | ||
for db_alias in db_aliases: | ||
yield cache_alias, db_alias | ||
table_cache_keys_per_cache = defaultdict(list) | ||
for db_alias in db_aliases: | ||
if no_tables: | ||
tables = connections[db_alias].introspection.table_names() | ||
for cache_alias in cache_aliases: | ||
table_cache_keys = [ | ||
_get_table_cache_key(db_alias, t) for t in tables] | ||
if table_cache_keys: | ||
table_cache_keys_per_cache[cache_alias].extend(table_cache_keys) | ||
return table_cache_keys_per_cache | ||
|
||
|
||
def invalidate_tables(tables, cache_alias=None, db_alias=None): | ||
""" | ||
Clears what was cached by django-cachalot implying one or more SQL tables | ||
from ``tables``. | ||
If ``cache_alias`` is specified, it only clears the SQL queries stored | ||
on this cache, otherwise queries from all caches are cleared. | ||
If ``db_alias`` is specified, it only clears the SQL queries executed | ||
on this database, otherwise queries from all databases are cleared. | ||
:arg tables: SQL tables names | ||
:type tables: iterable of strings | ||
:arg cache_alias: Alias from the Django ``CACHES`` setting | ||
:type cache_alias: string or NoneType | ||
:arg db_alias: Alias from the Django ``DATABASES`` setting | ||
:type db_alias: string or NoneType | ||
:returns: Nothing | ||
:rtype: NoneType | ||
""" | ||
def _get_tables(tables_or_models): | ||
return [o if isinstance(o, string_types) else o._meta.db_table | ||
for o in tables_or_models] | ||
|
||
for cache_alias, db_alias in _aliases_iterator(cache_alias, db_alias): | ||
table_cache_keys = [_get_table_cache_key(db_alias, t) for t in tables] | ||
cache = cachalot_caches.get_cache(cache_alias) | ||
_invalidate_table_cache_keys(cache, table_cache_keys) | ||
|
||
|
||
def invalidate_models(models, cache_alias=None, db_alias=None): | ||
""" | ||
Shortcut for ``invalidate_tables`` where you can specify Django models | ||
instead of SQL table names. | ||
:arg models: Django models | ||
:type models: iterable of ``django.db.models.Model`` subclasses | ||
:arg cache_alias: Alias from the Django ``CACHES`` setting | ||
:type cache_alias: string or NoneType | ||
:arg db_alias: Alias from the Django ``DATABASES`` setting | ||
:type db_alias: string or NoneType | ||
:returns: Nothing | ||
:rtype: NoneType | ||
""" | ||
|
||
invalidate_tables([model._meta.db_table for model in models], | ||
cache_alias, db_alias) | ||
|
||
|
||
def invalidate_all(cache_alias=None, db_alias=None): | ||
def invalidate(tables_or_models=(), cache_alias=None, db_alias=None): | ||
""" | ||
Clears everything that was cached by django-cachalot. | ||
Clears what was cached by django-cachalot implying one or more SQL tables | ||
or models from ``tables_or_models``. If ``tables_or_models`` | ||
is not specified, all tables found in the database are invalidated. | ||
If ``cache_alias`` is specified, it only clears the SQL queries stored | ||
on this cache, otherwise queries from all caches are cleared. | ||
If ``db_alias`` is specified, it only clears the SQL queries executed | ||
on this database, otherwise queries from all databases are cleared. | ||
:arg tables_or_models: SQL tables names | ||
:type tables_or_models: iterable of strings or models, or NoneType | ||
:arg cache_alias: Alias from the Django ``CACHES`` setting | ||
:type cache_alias: string or NoneType | ||
:arg db_alias: Alias from the Django ``DATABASES`` setting | ||
:type cache_alias: string or NoneType | ||
:type db_alias: string or NoneType | ||
:returns: Nothing | ||
:rtype: NoneType | ||
""" | ||
|
||
for cache_alias, db_alias in _aliases_iterator(cache_alias, db_alias): | ||
tables = connections[db_alias].introspection.table_names() | ||
table_cache_keys = [_get_table_cache_key(db_alias, t) for t in tables] | ||
table_cache_keys_per_cache = _get_table_cache_keys_per_cache( | ||
_get_tables(tables_or_models), cache_alias, db_alias) | ||
for cache_alias, table_cache_keys in table_cache_keys_per_cache.items(): | ||
_invalidate_table_cache_keys(cachalot_caches.get_cache(cache_alias), | ||
table_cache_keys) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters