| | 216 | Caching the current ``Site`` object |
| | 217 | =================================== |
| | 218 | |
| | 219 | As the current site is stored in the database, each call to ``Site.objects.get_current()`` could result in a database query. But Django is a little cleverer than that: on the first request, the current site is cached, and any subsequent call returns the cached data instead of hitting the database. |
| | 220 | |
| | 221 | If for any reason you want to force a database query, you can tell Django to clear the cache using ``Site.objects.clear_cache()``:: |
| | 222 | |
| | 223 | # First call; current site fetched from database. |
| | 224 | current_site = Site.objects.get_current() |
| | 225 | # ... |
| | 226 | |
| | 227 | # Second call; current site fetched from cache. |
| | 228 | current_site = Site.objects.get_current() |
| | 229 | # ... |
| | 230 | |
| | 231 | # Force a database query for the third call. |
| | 232 | Site.objects.clear_cache() |
| | 233 | current_site = Site.objects.get_current() |
| | 234 | |