| 2 | | This module contains helper functions and decorators for controlling caching. |
|---|
| 3 | | It does so by managing the "Vary" header of responses. It includes functions |
|---|
| 4 | | to patch the header of response objects directly and decorators that change |
|---|
| 5 | | functions to do that header-patching themselves. |
|---|
| | 2 | This module contains helper functions for controlling caching. It does so by |
|---|
| | 3 | managing the "Vary" header of responses. It includes functions to patch the |
|---|
| | 4 | header of response objects directly and decorators that change functions to do |
|---|
| | 5 | that header-patching themselves. |
|---|
| 67 | | def vary_on_headers(*headers): |
|---|
| 68 | | """ |
|---|
| 69 | | A view decorator that adds the specified headers to the Vary header of the |
|---|
| 70 | | response. Usage: |
|---|
| 71 | | |
|---|
| 72 | | @vary_on_headers('Cookie', 'Accept-language') |
|---|
| 73 | | def index(request): |
|---|
| 74 | | ... |
|---|
| 75 | | |
|---|
| 76 | | Note that the header names are not case-sensitive. |
|---|
| 77 | | """ |
|---|
| 78 | | def decorator(func): |
|---|
| 79 | | def inner_func(*args, **kwargs): |
|---|
| 80 | | response = func(*args, **kwargs) |
|---|
| 81 | | patch_vary_headers(response, headers) |
|---|
| 82 | | return response |
|---|
| 83 | | return inner_func |
|---|
| 84 | | return decorator |
|---|
| 85 | | |
|---|
| 86 | | def vary_on_cookie(func): |
|---|
| 87 | | """ |
|---|
| 88 | | A view decorator that adds "Cookie" to the Vary header of a response. This |
|---|
| 89 | | indicates that a page's contents depends on cookies. Usage: |
|---|
| 90 | | |
|---|
| 91 | | @vary_on_cookie |
|---|
| 92 | | def index(request): |
|---|
| 93 | | ... |
|---|
| 94 | | """ |
|---|
| 95 | | def inner_func(*args, **kwargs): |
|---|
| 96 | | response = func(*args, **kwargs) |
|---|
| 97 | | patch_vary_headers(response, ('Cookie',)) |
|---|
| 98 | | return response |
|---|
| 99 | | return inner_func |
|---|
| 100 | | |
|---|