will cache .count() call in Paginator but not later in articles fetch.
There are three possible actions - get, fetch and count. You can
pass any subset of this ops to .cache() method even empty to turn off caching.
There are, however, a shortcut for it:

Using local function gives additional advantage: we can filter queryset used
in @cached_as() to make invalidation more granular. We also add an
extra to make diffrent keys for calls with same category but diffrent
count.

Invalidation

Cacheops uses both time and event-driven invalidation. The event-driven one
listens on model signals and invalidates appropriate caches on Model.save()
and .delete().

Invalidation tries to be granular which means it won’t invalidate a queryset
that cannot be influenced by added/updated/deleted object judjing by query
conditions. Most time this will do what you want, if it’s not you can use one
of the following:

CAVEATS

Conditions other than __exact or __in don’t provide more granularity for
invalidation.

Conditions on related models don’t provide it either.

Update of “selected_related” object does not invalidate cache for queryset.

Mass updates don’t trigger invalidation.

ORDER BY and LIMIT/OFFSET don’t affect invalidation.

Doesn’t work with RawQuerySet.

Conditions on subqueries don’t affect invalidation.

Doesn’t work right with multi-table inheritance.

Aggregates is not implemented yet.

Timeout in queryset and @cached_as() cannot be larger than default.

Here 1, 3, 5, 10 are part of design compromise, trying to solve them will make
things complicated and slow. 2 and 7 can be implemented if needed, but it’s
probably counter-productive since one can just break queries into simple ones,
which cache better. 4 is a deliberate choice, making it “right” will flush
cache too much when update conditions are orthogonal to most queries conditions.
6 can be cached as SomeModel.objects.all() but @cached_as() someway covers that
and is more flexible. 8 is postponed until it will gain more interest or a champion willing to
implement it emerge.

Performance tips

Here come some performance tips to make cacheops and Django ORM faster.

When you use cache you pickle and unpickle lots of django model instances, which could be slow. You can optimize django models serialization with django-pickling.

Constructing querysets is rather slow in django, mainly because most of QuerySet methods clone self, then change it and return a clone. Original queryset is usually thrown away. Cacheops adds .inplace() method, which makes queryset mutating, preventing useless cloning:

More to 2, there is unfixed bug in django 1.4-,
which sometimes make queryset cloning very slow. You can use any patch from this ticket to fix it.

Use template fragment caching when possible, it’s way more fast because you don’t need to generate anything. Also pickling/unpickling a string is much faster than list of model instances. Cacheops doesn’t provide extension for django’s built-in templates for now, but you can adapt django.templatetags.cache to work with cacheops fairly easily (send me a pull request if you do).

Run separate redis instance for cache with disabled persistence. You can manually call SAVE or BGSAVE to stay hot upon server restart.

If you filter queryset on many different or complex conditions cache could degrade performance (comparing to uncached db calls) in consequence of frequent cache misses. Disable cache in such cases entirely or on some heurestics which detect if this request would be probably hit. E.g. enable cache if only some primary fields are used in filter.

Caching querysets with large amount of filters also slows down all subsequent invalidation on that model. You can disable caching if more than some amount of fields is used in filter simultaneously.

TODO

fast mode: store cache in local memory, but check in with redis if it’s valid