code: def on_post_save(sender, **kwargs):
      
      if not invalidate:
          return
      if kwargs.get("raw", False):
          return
      if sender is MigrationRecorder.Migration:
          return
      if issubclass(sender, Model):
          obj = kwargs["instance"]
          if isinstance(obj, Model):
              # get_for_model itself is cached
              try:
                  ct = ContentType.objects.get_for_model(sender)
              except RuntimeError:
                  # This happens when ultracache is being used by another product
                  # during a test run.
                  return
  
              if kwargs.get("created", False):
                  # Expire cache keys that contain objects of this content type
                  key = "ucache-ct-%s" % ct.id
                  to_delete = cache.get(key, [])
                  if to_delete:
                      try:
                          cache.delete_many(to_delete)
                      except NotImplementedError:
                          for k in to_delete:
                              cache.delete(k)
                  cache.delete(key)
  
                  # Purge paths in reverse caching proxy that contain objects of
                  # this content type.
                  key = "ucache-ct-pth-%s" % ct.id
                  if purger is not None:
                      for li in cache.get(key, []):
                          purger(li[0], li[1])
                  cache.delete(key)
  
              else:
                  # Expire cache keys
                  key = "ucache-%s-%s" % (ct.id, obj.pk)
                  to_delete = cache.get(key, [])
                  if to_delete:
                      try:
                          cache.delete_many(to_delete)
                      except NotImplementedError:
                          for k in to_delete:
                              cache.delete(k)
                  cache.delete(key)
  
                  # Purge paths in reverse caching proxy
                  key = "ucache-pth-%s-%s" % (ct.id, obj.pk)
                  if purger is not None:
                      for li in cache.get(key, []):
                          purger(li[0], li[1])
                  cache.delete(key)
docs: Expire ultracache cache keys affected by this object