11.7.drf的缓存
drf-extensions
Installation:
CacheResponseMixin
It is common to cache standard viewset retrieve
and list
methods. That is why CacheResponseMixin
exists. Just mix it into viewset implementation and those methods will use functions, defined in REST_FRAMEWORK_EXTENSIONS
settings:
"DEFAULT_OBJECT_CACHE_KEY_FUNC" for
retrieve
method"DEFAULT_LIST_CACHE_KEY_FUNC" for
list
method
By default those functions are using DefaultKeyConstructor and extends it:
With
RetrieveSqlQueryKeyBit
for "DEFAULT_OBJECT_CACHE_KEY_FUNC"With
ListSqlQueryKeyBit
andPaginationKeyBit
for "DEFAULT_LIST_CACHE_KEY_FUNC"
You can change those settings for custom cache key generation:
Mixin example usage:
设置cache过期时间
Last updated
Was this helpful?