Skip to content

Commit 90923f4

Browse files
committed
docs: add cache support documentation
- Added cache_support.rst - Updated 'index.rst' to include the new page in the toctree - Documented related cache settings in settings.rst
1 parent 056001f commit 90923f4

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

docs/cache_support.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Cache Support
2+
==============
3+
4+
SimpleJWT provides optional cache support for improving performance.
5+
Currently, caching is available for:
6+
7+
- Blacklisted refresh tokens
8+
- Blacklisted token families
9+
10+
To enable caching in SimpleJWT, you must first configure Django's ``CACHES`` setting:
11+
12+
.. code-block:: python
13+
14+
CACHES = {
15+
"default": {
16+
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
17+
"LOCATION": "unique-name",
18+
},
19+
"redis": {
20+
"BACKEND": "django_redis.cache.RedisCache",
21+
"LOCATION": "redis://127.0.0.1:6379/0",
22+
"OPTIONS": {
23+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
24+
}
25+
}
26+
}
27+
28+
In this example, two cache backends are defined. You can choose which one to use by
29+
setting the ``SJWT_CACHE_NAME`` option in your SimpleJWT configuration. For this case,
30+
it could be either `"default"` or `"redis"`.
31+
32+
.. Note::
33+
Ensure that your Django `CACHES` setting includes a cache matching the alias
34+
defined by `SJWT_CACHE_NAME`.
35+
36+
37+
Blacklist Cache
38+
----------------
39+
40+
When enabled via the appropriate settings, blacklisted refresh tokens and token families
41+
will be cached. This reduces the number of database queries when verifying whether a token
42+
or family is blacklisted.
43+
44+
.. code-block:: python
45+
46+
SIMPLE_JWT = {
47+
...
48+
49+
"SJWT_CACHE_NAME": "default",
50+
"CACHE_BLACKLISTED_REFRESH_TOKENS": True,
51+
"CACHE_BLACKLISTED_FAMILIES": True,
52+
"CACHE_TTL_BLACKLISTED_REFRESH_TOKENS": 3600, #time in seconds
53+
"CACHE_TTL_BLACKLISTED_FAMILIES": 3600, #time in seconds
54+
"CACHE_KEY_PREFIX_BLACKLISTED_REFRESH_TOKENS": "sjwt_brt",
55+
"CACHE_KEY_PREFIX_BLACKLISTED_FAMILIES": "sjwt_btf",
56+
57+
...
58+
}
59+
60+
61+
Cache keys follow this format:
62+
63+
- Refresh token: ``sjwt_brt:<jti>``
64+
- Token family: ``sjwt_btf:<family_id>``
65+

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Contents
5353
blacklist_app
5454
family_app
5555
stateless_user_authentication
56+
cache_support
5657
development_and_contributing
5758
drf_yasg_integration
5859
rest_framework_simplejwt

docs/settings.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Some of Simple JWT's behavior can be customized through settings variables in
3434
"JWK_URL": None,
3535
"LEEWAY": 0,
3636
37+
"SJWT_CACHE_NAME": "default",
38+
"CACHE_BLACKLISTED_REFRESH_TOKENS": False,
39+
"CACHE_BLACKLISTED_FAMILIES": False,
40+
"CACHE_TTL_BLACKLISTED_REFRESH_TOKENS": 3600, # time is seconds
41+
"CACHE_TTL_BLACKLISTED_FAMILIES": 3600, # time in seconds
42+
"CACHE_KEY_PREFIX_BLACKLISTED_REFRESH_TOKENS": "sjwt_brt",
43+
"CACHE_KEY_PREFIX_BLACKLISTED_FAMILIES": "sjwt_btf",
44+
3745
"AUTH_HEADER_TYPES": ("Bearer",),
3846
"AUTH_HEADER_NAME": "HTTP_AUTHORIZATION",
3947
"USER_ID_FIELD": "id",
@@ -222,6 +230,47 @@ integer for seconds or a ``datetime.timedelta``. Please reference
222230
https://pyjwt.readthedocs.io/en/latest/usage.html#expiration-time-claim-exp
223231
for more information.
224232

233+
``SJWT_CACHE_NAME``
234+
---------------------
235+
236+
Specifies the Django cache alias to use. This must match a defined entry
237+
in Django's ``CACHES`` setting.
238+
239+
Learn more about :doc:`/cache_support`.
240+
241+
``CACHE_BLACKLISTED_REFRESH_TOKENS``
242+
--------------------------------------
243+
244+
When set to ``True``, enables caching of blacklisted refresh tokens.
245+
Blacklisted refresh token entries will be cached for a period defined
246+
by ``CACHE_TTL_BLACKLISTED_REFRESH_TOKENS``.
247+
248+
``CACHE_BLACKLISTED_FAMILIES``
249+
--------------------------------
250+
251+
When set to ``True``, enables caching of blacklisted token families.
252+
Blacklisted family entries will be cached for a period defined
253+
by ``CACHE_TTL_BLACKLISTED_FAMILIES``.
254+
255+
``CACHE_TTL_BLACKLISTED_REFRESH_TOKENS``
256+
------------------------------------------
257+
258+
Time-to-live (TTL) in seconds for cached refresh token blacklist entries.
259+
260+
``CACHE_TTL_BLACKLISTED_FAMILIES``
261+
------------------------------------
262+
263+
Time-to-live (TTL) in seconds for cached token family blacklist entries.
264+
265+
``CACHE_KEY_PREFIX_BLACKLISTED_REFRESH_TOKENS``
266+
-------------------------------------------------
267+
268+
Prefix used for cache keys when storing blacklisted refresh tokens.
269+
270+
``CACHE_KEY_PREFIX_BLACKLISTED_FAMILIES``
271+
-------------------------------------------
272+
273+
Prefix used for cache keys when storing blacklisted token families.
225274

226275
``AUTH_HEADER_TYPES``
227276
---------------------

0 commit comments

Comments
 (0)