<s> from __future__ import unicode_literals <EOL> from django . conf import settings <EOL> from django . db import connections <EOL> from django . utils . six import string_types <EOL> from . cache import cachalot_caches <EOL> from . signals import post_invalidation <EOL> from . utils import _get_table_cache_key , _invalidate_tables <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _cache_db_tables_iterator ( tables , cache_alias , db_alias ) : <EOL> no_tables = not tables <EOL> cache_aliases = settings . CACHES if cache_alias is None else ( cache_alias , ) <EOL> db_aliases = settings . DATABASES if db_alias is None else ( db_alias , ) <EOL> for db_alias in db_aliases : <EOL> if no_tables : <EOL> tables = connections [ db_alias ] . introspection . table_names ( ) <EOL> if tables : <EOL> for cache_alias in cache_aliases : <EOL> yield cache_alias , db_alias , tables <EOL> def _get_tables ( tables_or_models ) : <EOL> return [ o if isinstance ( o , string_types ) else o . _meta . db_table <EOL> for o in tables_or_models ] <EOL> def invalidate ( * tables_or_models , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cache_alias = kwargs . pop ( '<STR_LIT>' , None ) <EOL> db_alias = kwargs . pop ( '<STR_LIT>' , None ) <EOL> for k in kwargs : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % k ) <EOL> invalidated = set ( ) <EOL> for cache_alias , db_alias , tables in _cache_db_tables_iterator ( <EOL> _get_tables ( tables_or_models ) , cache_alias , db_alias ) : <EOL> _invalidate_tables ( <EOL> cachalot_caches . get_cache ( cache_alias , db_alias ) , db_alias , tables ) <EOL> invalidated . update ( tables ) <EOL> for table in invalidated : <EOL> post_invalidation . send ( table , db_alias = db_alias ) <EOL> def get_last_invalidation ( * tables_or_models , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cache_alias = kwargs . pop ( '<STR_LIT>' , None ) <EOL> db_alias = kwargs . pop ( '<STR_LIT>' , None ) <EOL> for k in kwargs : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % k ) <EOL> last_invalidation = <NUM_LIT:0.0> <EOL> for cache_alias , db_alias , tables in _cache_db_tables_iterator ( <EOL> _get_tables ( tables_or_models ) , cache_alias , db_alias ) : <EOL> table_cache_keys = [ _get_table_cache_key ( db_alias , t ) for t in tables ] <EOL> invalidations = cachalot_caches . get_cache ( <EOL> cache_alias , db_alias ) . get_many ( table_cache_keys ) . values ( ) <EOL> if invalidations : <EOL> current_last_invalidation = max ( invalidations ) <EOL> if current_last_invalidation > last_invalidation : <EOL> last_invalidation = current_last_invalidation <EOL> return last_invalidation </s>
