Signal receiver for models to invalidate model cache of sender and related models.
    Model cache is invalidated by generating new key for each model.

    Parameters
    ~~~~~~~~~~
    sender
        The model class
    instance
        The actual instance being saved.

def invalidate_model_cache(sender, instance, **kwargs):
    """
    Signal receiver for models to invalidate model cache of sender and related models.
    Model cache is invalidated by generating new key for each model.

    Parameters
    ~~~~~~~~~~
    sender
        The model class
    instance
        The actual instance being saved.
    """
    logger.debug('Received post_save/post_delete signal from sender {0}'.format(sender))
    if django.VERSION >= (1, 8):
        related_tables = set(
            [f.related_model._meta.db_table for f in sender._meta.get_fields()
             if f.related_model is not None
             and (((f.one_to_many or f.one_to_one) and f.auto_created)
                 or f.many_to_one or (f.many_to_many and not f.auto_created))])
    else:
        related_tables = set([rel.model._meta.db_table for rel in sender._meta.get_all_related_objects()])
        # temporary fix for m2m relations with an intermediate model, goes away after better join caching
        related_tables |= set([field.rel.to._meta.db_table for field in sender._meta.fields if issubclass(type(field), RelatedField)])
    logger.debug('Related tables of sender {0} are {1}'.format(sender, related_tables))
    update_model_cache(sender._meta.db_table)
    for related_table in related_tables:
        update_model_cache(related_table)