mirror of https://github.com/sgoudham/Enso-Bot.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
816 B
Python
27 lines
816 B
Python
5 years ago
|
NOT_PROVIDED = object()
|
||
|
|
||
|
|
||
|
class FieldCacheMixin:
|
||
|
"""Provide an API for working with the model's fields value cache."""
|
||
|
|
||
|
def get_cache_name(self):
|
||
|
raise NotImplementedError
|
||
|
|
||
|
def get_cached_value(self, instance, default=NOT_PROVIDED):
|
||
|
cache_name = self.get_cache_name()
|
||
|
try:
|
||
|
return instance._state.fields_cache[cache_name]
|
||
|
except KeyError:
|
||
|
if default is NOT_PROVIDED:
|
||
|
raise
|
||
|
return default
|
||
|
|
||
|
def is_cached(self, instance):
|
||
|
return self.get_cache_name() in instance._state.fields_cache
|
||
|
|
||
|
def set_cached_value(self, instance, value):
|
||
|
instance._state.fields_cache[self.get_cache_name()] = value
|
||
|
|
||
|
def delete_cached_value(self, instance):
|
||
|
del instance._state.fields_cache[self.get_cache_name()]
|