11.7.drf的缓存
drf-extensions
Installation:
pip3 install drf-extensionsCacheResponseMixin
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
retrievemethod"DEFAULT_LIST_CACHE_KEY_FUNC" for
listmethod
By default those functions are using DefaultKeyConstructor and extends it:
With
RetrieveSqlQueryKeyBitfor "DEFAULT_OBJECT_CACHE_KEY_FUNC"With
ListSqlQueryKeyBitandPaginationKeyBitfor "DEFAULT_LIST_CACHE_KEY_FUNC"
You can change those settings for custom cache key generation:
REST_FRAMEWORK_EXTENSIONS = {
'DEFAULT_OBJECT_CACHE_KEY_FUNC':
'rest_framework_extensions.utils.default_object_cache_key_func',
'DEFAULT_LIST_CACHE_KEY_FUNC':
'rest_framework_extensions.utils.default_list_cache_key_func',
}Mixin example usage:
设置cache过期时间
Last updated
Was this helpful?