API

Use these tools to interact with django-cachalot, especially if you face raw queries limits or if you need to create a cache key from the last table invalidation timestamp.

cachalot.api.invalidate(*tables_or_models, **kwargs)[source]

Clears what was cached by django-cachalot implying one or more SQL tables or models from tables_or_models. If tables_or_models is not specified, all tables found in the database (including those outside Django) are invalidated.

If cache_alias is specified, it only clears the SQL queries stored on this cache, otherwise queries from all caches are cleared.

If db_alias is specified, it only clears the SQL queries executed on this database, otherwise queries from all databases are cleared.

Parameters:
  • tables_or_models (tuple of strings or models) – SQL tables names, models or models lookups (or a combination)
  • cache_alias (string or NoneType) – Alias from the Django CACHES setting
  • db_alias (string or NoneType) – Alias from the Django DATABASES setting
Returns:

Nothing

Return type:

NoneType

cachalot.api.get_last_invalidation(*tables_or_models, **kwargs)[source]

Returns the timestamp of the most recent invalidation of the given tables_or_models. If tables_or_models is not specified, all tables found in the database (including those outside Django) are used.

If cache_alias is specified, it only fetches invalidations in this cache, otherwise invalidations in all caches are fetched.

If db_alias is specified, it only fetches invalidations for this database, otherwise invalidations for all databases are fetched.

Parameters:
  • tables_or_models (tuple of strings or models) – SQL tables names, models or models lookups (or a combination)
  • cache_alias (string or NoneType) – Alias from the Django CACHES setting
  • db_alias (string or NoneType) – Alias from the Django DATABASES setting
Returns:

The timestamp of the most recent invalidation

Return type:

float

cachalot.api.cachalot_disabled(all_queries=False)[source]

Context manager for temporarily disabling cachalot. If you evaluate the same queryset a second time, like normally for Django querysets, this will access the variable that saved it in-memory. For example:

with cachalot_disabled():
    qs = Test.objects.filter(blah=blah)
    # Does a single query to the db
    list(qs)  # Evaluates queryset
    # Because the qs was evaluated, it's
    # saved in memory:
    list(qs)  # this does 0 queries.
    # This does 1 query to the db
    list(Test.objects.filter(blah=blah))

If you evaluate the queryset outside the context manager, any duplicate query will use the cached result unless an object creation happens in between the original and duplicate query.

Parameters:all_queries (bool) – Any query, including already evaluated queries, are re-evaluated.