You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mixins.py 816B

1234567891011121314151617181920212223242526
  1. NOT_PROVIDED = object()
  2. class FieldCacheMixin:
  3. """Provide an API for working with the model's fields value cache."""
  4. def get_cache_name(self):
  5. raise NotImplementedError
  6. def get_cached_value(self, instance, default=NOT_PROVIDED):
  7. cache_name = self.get_cache_name()
  8. try:
  9. return instance._state.fields_cache[cache_name]
  10. except KeyError:
  11. if default is NOT_PROVIDED:
  12. raise
  13. return default
  14. def is_cached(self, instance):
  15. return self.get_cache_name() in instance._state.fields_cache
  16. def set_cached_value(self, instance, value):
  17. instance._state.fields_cache[self.get_cache_name()] = value
  18. def delete_cached_value(self, instance):
  19. del instance._state.fields_cache[self.get_cache_name()]