'Pushes changes to an instance into the cache, and removes invalid (changed)
lookup values.'

def __post_save(self, instance, **kwargs):

    pk_name = instance._meta.pk.name
    pk_names = ('pk', pk_name)
    pk_val = instance.pk
    for key in self.cache_fields:
        if (key in pk_names):
            continue
        value = self.__value_for_field(instance, key)
        cache.set(key=self.__get_lookup_cache_key(**{key: value}), value=pk_val, timeout=self.cache_ttl, version=self.cache_version)
    db = instance._state.db
    instance._state.db = None
    try:
        cache.set(key=self.__get_lookup_cache_key(**{pk_name: pk_val}), value=instance, timeout=self.cache_ttl, version=self.cache_version)
    except Exception as e:
        logger.error(e, exc_info=True)
    instance._state.db = db
    if (instance in self.__cache):
        for key in self.cache_fields:
            if (key not in self.__cache[instance]):
                continue
            value = self.__cache[instance][key]
            current_value = self.__value_for_field(instance, key)
            if (value != current_value):
                cache.delete(key=self.__get_lookup_cache_key(**{key: value}), version=self.cache_version)
    self.__cache_state(instance)
