Caching¶
The caching system is opt-in on a per-Datatable
basis.
Each Datatable can specify in its Meta
options a value for the cache_type
option.
Caching Strategies¶
The possible values are available as constants on datatableview.datatables.cache_types
. Regardless of strategy, your Settings will control which Django-defined caching backend to use, and therefore the expiry time and other backend characteristics.
cache_types.DEFAULT
¶
A stand-in for whichever strategy DATATABLEVIEW_DEFAULT_CACHE_TYPE
in your Settings specifies. That setting defaults to SIMPLE
.
cache_types.SIMPLE
¶
Passes the object_list
(usually a queryset) directly to the cache backend for pickling. This is a more faithful caching strategy than PK_LIST
but becomes noticeably slower as the number of cached objects grows.
cache_types.PK_LIST
¶
Assumes that object_list
is a queryset and stores in the cache only the list of pk
values for each object. Reading from the cache therefore requires a database query to re-initialize the queryset, but because that query may be substantially faster than producing the original queryset, it is tolerated.
Because this strategy must regenerate the queryset, extra information on the original queryset will be lost, such as calls to select_related()
, prefetch_related()
, and annotate()
.
cache_types.NONE
¶
An explicit option that disables a caching strategy for a table. Useful when subclassing a Datatable to provide customized options.
Settings¶
There are a few project settings you can use to control features of the caching system when activated on a Datatable
.
DATATABLEVIEW_CACHE_BACKEND
¶
Default: | 'default' |
---|
The name of the Django CACHES
backend to use. This is where cache expiry information will be
specified.
DATATABLEVIEW_CACHE_PREFIX
¶
Default: | 'datatableview_' |
---|
The prefix added to every cache key generated by a table’s get_cache_key()
value.
DATATABLEVIEW_DEFAULT_CACHE_TYPE
¶
Default: | 'simple' (datatableview.datatables.cache_types.SIMPLE ) |
---|
The caching strategy to use when a Datatable’s Meta option cache_type
is set to cache_types.DEFAULT
.
DATATABLEVIEW_CACHE_KEY_HASH
¶
Default: | True |
---|
Controls whether the values that go into the cache key will be hashed or placed directly into the cache key string.
This may be required for caching backends with requirements about cache key length.
When False
, a cache key might resemble the following:
datatableview_datatable_myproj.myapp.datatables.MyDatatable__view_myproj.myapp.views.MyView__user_77
When True
, the cache key will be a predictable length, and might resemble the following:
datatableview_datatable_3da541559918a808c2402bba5012f6c60b27661c__view_1161e6ffd3637b302a5cd74076283a7bd1fc20d3__user_77
DATATABLEVIEW_CACHE_KEY_HASH_LENGTH
¶
Default: | None |
---|
When DATATABLEVIEW_CACHE_KEY_HASH is True
, setting this to an integer will slice each hash substring to the first N characters, allowing you to further control the cache key length.
For example, if set to 10
, the hash-enabled cache key might resemble:
datatableview_datatable_3da5415599__view_1161e6ffd3__user_77