{"id":13415561,"url":"https://github.com/Suor/django-cacheops","last_synced_at":"2025-03-14T23:30:49.534Z","repository":{"id":37470627,"uuid":"1846638","full_name":"Suor/django-cacheops","owner":"Suor","description":"A slick ORM cache with automatic granular event-driven invalidation.","archived":false,"fork":false,"pushed_at":"2024-10-09T12:29:04.000Z","size":1386,"stargazers_count":2115,"open_issues_count":20,"forks_count":227,"subscribers_count":40,"default_branch":"master","last_synced_at":"2024-10-29T10:52:11.632Z","etag":null,"topics":["caching","django","orm","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Suor.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-06-04T12:25:47.000Z","updated_at":"2024-10-29T01:08:19.000Z","dependencies_parsed_at":"2023-02-09T11:31:23.221Z","dependency_job_id":"320a4d2f-8d71-48c5-9d10-cb3b5d813d63","html_url":"https://github.com/Suor/django-cacheops","commit_stats":{"total_commits":1161,"total_committers":70,"mean_commits":"16.585714285714285","dds":"0.15503875968992253","last_synced_commit":"eb8218b5fc602fa8d89a3fc4317eb9482f42b71d"},"previous_names":[],"tags_count":64,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suor%2Fdjango-cacheops","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suor%2Fdjango-cacheops/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suor%2Fdjango-cacheops/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suor%2Fdjango-cacheops/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Suor","download_url":"https://codeload.github.com/Suor/django-cacheops/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237739830,"owners_count":19358625,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","django","orm","python"],"created_at":"2024-07-30T21:00:50.305Z","updated_at":"2025-03-14T23:30:49.193Z","avatar_url":"https://github.com/Suor.png","language":"Python","readme":"Cacheops |Build Status|\n========\n\nA slick app that supports automatic or manual queryset caching and `automatic\ngranular event-driven invalidation \u003chttp://suor.github.io/blog/2014/03/09/on-orm-cache-invalidation/\u003e`_.\n\nIt uses `redis \u003chttp://redis.io/\u003e`_ as backend for ORM cache and redis or\nfilesystem for simple time-invalidated one.\n\nAnd there is more to it:\n\n- decorators to cache any user function or view as a queryset or by time\n- extensions for django and jinja2 templates\n- transparent transaction support\n- dog-pile prevention mechanism\n- a couple of hacks to make django faster\n\n.. contents:: Contents\n    :local:\n    :backlinks: top\n\nRequirements\n++++++++++++\n\nPython 3.7+, Django 3.2+ and Redis 4.0+.\n\n\nInstallation\n++++++++++++\n\nUsing pip:\n\n.. code:: bash\n\n    $ pip install django-cacheops\n\n    # Or from github directly\n    $ pip install git+https://github.com/Suor/django-cacheops.git@master\n\n\nSetup\n+++++\n\nAdd ``cacheops`` to your ``INSTALLED_APPS``.\n\nSetup redis connection and enable caching for desired models:\n\n.. code:: python\n\n    CACHEOPS_REDIS = {\n        'host': 'localhost', # redis-server is on same machine\n        'port': 6379,        # default redis port\n        'db': 1,             # SELECT non-default redis database\n                             # using separate redis db or redis instance\n                             # is highly recommended\n\n        'socket_timeout': 3,   # connection timeout in seconds, optional\n        'password': '...',     # optional\n        'unix_socket_path': '' # replaces host and port\n    }\n\n    # Alternatively the redis connection can be defined using a URL:\n    CACHEOPS_REDIS = \"redis://localhost:6379/1\"\n    # or\n    CACHEOPS_REDIS = \"unix://path/to/socket?db=1\"\n    # or with password (note a colon)\n    CACHEOPS_REDIS = \"redis://:password@localhost:6379/1\"\n\n    # If you want to use sentinel, specify this variable\n    CACHEOPS_SENTINEL = {\n        'locations': [('localhost', 26379)], # sentinel locations, required\n        'service_name': 'mymaster',          # sentinel service name, required\n        'socket_timeout': 0.1,               # connection timeout in seconds, optional\n        'db': 0                              # redis database, default: 0\n        ...                                  # everything else is passed to Sentinel()\n    }\n\n    # Use your own redis client class, should be compatible or subclass redis.Redis\n    CACHEOPS_CLIENT_CLASS = 'your.redis.ClientClass'\n\n    CACHEOPS = {\n        # Automatically cache any User.objects.get() calls for 15 minutes\n        # This also includes .first() and .last() calls,\n        # as well as request.user or post.author access,\n        # where Post.author is a foreign key to auth.User\n        'auth.user': {'ops': 'get', 'timeout': 60*15},\n\n        # Automatically cache all gets and queryset fetches\n        # to other django.contrib.auth models for an hour\n        'auth.*': {'ops': {'fetch', 'get'}, 'timeout': 60*60},\n\n        # Cache all queries to Permission\n        # 'all' is an alias for {'get', 'fetch', 'count', 'aggregate', 'exists'}\n        'auth.permission': {'ops': 'all', 'timeout': 60*60},\n\n        # Enable manual caching on all other models with default timeout of an hour\n        # Use Post.objects.cache().get(...)\n        #  or Tags.objects.filter(...).order_by(...).cache()\n        # to cache particular ORM request.\n        # Invalidation is still automatic\n        '*.*': {'ops': (), 'timeout': 60*60},\n\n        # And since ops is empty by default you can rewrite last line as:\n        '*.*': {'timeout': 60*60},\n\n        # NOTE: binding signals has its overhead, like preventing fast mass deletes,\n        #       you might want to only register whatever you cache and dependencies.\n\n        # Finally you can explicitely forbid even manual caching with:\n        'some_app.*': None,\n    }\n\nYou can configure default profile setting with ``CACHEOPS_DEFAULTS``. This way you can rewrite the config above:\n\n.. code:: python\n\n    CACHEOPS_DEFAULTS = {\n        'timeout': 60*60\n    }\n    CACHEOPS = {\n        'auth.user': {'ops': 'get', 'timeout': 60*15},\n        'auth.*': {'ops': ('fetch', 'get')},\n        'auth.permission': {'ops': 'all'},\n        '*.*': {},\n    }\n\nUsing ``'*.*'`` with non-empty ``ops`` is **not recommended**\nsince it will easily cache something you don't intent to or even know about like migrations tables.\nThe better approach will be restricting by app with ``'app_name.*'``.\n\nBesides ``ops`` and ``timeout`` options you can also use:\n\n``local_get: True``\n    To cache simple gets for this model in process local memory.\n    This is very fast, but is not invalidated in any way until process is restarted.\n    Still could be useful for extremely rarely changed things.\n\n``cache_on_save=True | 'field_name'``\n    To write an instance to cache upon save.\n    Cached instance will be retrieved on ``.get(field_name=...)`` request.\n    Setting to ``True`` causes caching by primary key.\n\nAdditionally, you can tell cacheops to degrade gracefully on redis fail with:\n\n.. code:: python\n\n    CACHEOPS_DEGRADE_ON_FAILURE = True\n\nThere is also a possibility to make all cacheops methods and decorators no-op, e.g. for testing:\n\n.. code:: python\n\n    from django.test import override_settings\n\n    @override_settings(CACHEOPS_ENABLED=False)\n    def test_something():\n        # ...\n        assert cond\n\n\nUsage\n+++++\n\n| **Automatic caching**\n\nIt's automatic you just need to set it up.\n\n\n| **Manual caching**\n\nYou can force any queryset to use cache by calling its ``.cache()`` method:\n\n.. code:: python\n\n    Article.objects.filter(tag=2).cache()\n\n\nHere you can specify which ops should be cached for the queryset, for example, this code:\n\n.. code:: python\n\n    qs = Article.objects.filter(tag=2).cache(ops=['count'])\n    paginator = Paginator(objects, ipp)\n    articles = list(pager.page(page_num)) # hits database\n\n\nwill cache count call in ``Paginator`` but not later articles fetch.\nThere are five possible actions - ``get``, ``fetch``, ``count``, ``aggregate`` and ``exists``.\nYou can pass any subset of this ops to ``.cache()`` method even empty - to turn off caching.\nThere is, however, a shortcut for the latter:\n\n.. code:: python\n\n    qs = Article.objects.filter(visible=True).nocache()\n    qs1 = qs.filter(tag=2)       # hits database\n    qs2 = qs.filter(category=3)  # hits it once more\n\n\nIt is useful when you want to disable automatic caching on particular queryset.\n\nYou can also override default timeout for particular queryset with ``.cache(timeout=...)``.\n\n\n| **Function caching**\n\nYou can cache and invalidate result of a function the same way as a queryset.\nCached results of the next function will be invalidated on any ``Article`` change,\naddition or deletion:\n\n.. code:: python\n\n    from cacheops import cached_as\n\n    @cached_as(Article, timeout=120)\n    def article_stats():\n        return {\n            'tags': list(Article.objects.values('tag').annotate(Count('id')))\n            'categories': list(Article.objects.values('category').annotate(Count('id')))\n        }\n\n\nNote that we are using list on both querysets here, it's because we don't want\nto cache queryset objects but their results.\n\nAlso note that if you want to filter queryset based on arguments,\ne.g. to make invalidation more granular, you can use a local function:\n\n.. code:: python\n\n    def articles_block(category, count=5):\n        qs = Article.objects.filter(category=category)\n\n        @cached_as(qs, extra=count)\n        def _articles_block():\n            articles = list(qs.filter(photo=True)[:count])\n            if len(articles) \u003c count:\n                articles += list(qs.filter(photo=False)[:count-len(articles)])\n            return articles\n\n        return _articles_block()\n\nWe added ``extra`` here to make different keys for calls with same ``category`` but different\n``count``. Cache key will also depend on function arguments, so we could just pass ``count`` as\nan argument to inner function. We also omitted ``timeout`` here, so a default for the model\nwill be used.\n\nAnother possibility is to make function cache invalidate on changes to any one of several models:\n\n.. code:: python\n\n    @cached_as(Article.objects.filter(public=True), Tag)\n    def article_stats():\n        return {...}\n\nAs you can see, we can mix querysets and models here.\n\n\n| **View caching**\n\nYou can also cache and invalidate a view as a queryset. This works mostly the same way as function\ncaching, but only path of the request parameter is used to construct cache key:\n\n.. code:: python\n\n    from cacheops import cached_view_as\n\n    @cached_view_as(News)\n    def news_index(request):\n        # ...\n        return render(...)\n\nYou can pass ``timeout``, ``extra`` and several samples the same way as to ``@cached_as()``. Note that you can pass a function as ``extra``:\n\n.. code:: python\n\n    @cached_view_as(News, extra=lambda req: req.user.is_staff)\n    def news_index(request):\n        # ... add extra things for staff\n        return render(...)\n\nA function passed as ``extra`` receives the same arguments as the cached function.\n\nClass based views can also be cached:\n\n.. code:: python\n\n    class NewsIndex(ListView):\n        model = News\n\n    news_index = cached_view_as(News, ...)(NewsIndex.as_view())\n\n\nInvalidation\n++++++++++++\n\nCacheops uses both time and event-driven invalidation. The event-driven one\nlistens on model signals and invalidates appropriate caches on ``Model.save()``, ``.delete()``\nand m2m changes.\n\nInvalidation tries to be granular which means it won't invalidate a queryset\nthat cannot be influenced by added/updated/deleted object judging by query\nconditions. Most of the time this will do what you want, if it won't you can use\none of the following:\n\n.. code:: python\n\n    from cacheops import invalidate_obj, invalidate_model, invalidate_all\n\n    invalidate_obj(some_article)  # invalidates queries affected by some_article\n    invalidate_model(Article)     # invalidates all queries for model\n    invalidate_all()              # flush redis cache database\n\nAnd last there is ``invalidate`` command::\n\n    ./manage.py invalidate articles.Article.34  # same as invalidate_obj\n    ./manage.py invalidate articles.Article     # same as invalidate_model\n    ./manage.py invalidate articles   # invalidate all models in articles\n\nAnd the one that FLUSHES cacheops redis database::\n\n    ./manage.py invalidate all\n\nDon't use that if you share redis database for both cache and something else.\n\n\n| **Turning off and postponing invalidation**\n\nThere is also a way to turn off invalidation for a while:\n\n.. code:: python\n\n    from cacheops import no_invalidation\n\n    with no_invalidation:\n        # ... do some changes\n        obj.save()\n\nAlso works as decorator:\n\n.. code:: python\n\n    @no_invalidation\n    def some_work(...):\n        # ... do some changes\n        obj.save()\n\nCombined with ``try ... finally`` it could be used to postpone invalidation:\n\n.. code:: python\n\n    try:\n        with no_invalidation:\n            # ...\n    finally:\n        invalidate_obj(...)\n        # ... or\n        invalidate_model(...)\n\nPostponing invalidation can speed up batch jobs.\n\n\n| **Mass updates**\n\nNormally `qs.update(...)` doesn't emit any events and thus doesn't trigger invalidation.\nAnd there is no transparent and efficient way to do that: trying to act on conditions will\ninvalidate too much if update conditions are orthogonal to many queries conditions,\nand to act on specific objects we will need to fetch all of them,\nwhich `QuerySet.update()` users generally try to avoid.\n\nIn the case you actually want to perform the latter cacheops provides a shortcut:\n\n.. code:: python\n\n    qs.invalidated_update(...)\n\nNote that all the updated objects are fetched twice, prior and post the update.\n\nComponents\n++++++++++\n\n\nSimple time-invalidated cache\n-----------------------------\n\nTo cache result of a function call or a view for some time use:\n\n.. code:: python\n\n    from cacheops import cached, cached_view\n\n    @cached(timeout=number_of_seconds)\n    def top_articles(category):\n        return ... # Some costly queries\n\n    @cached_view(timeout=number_of_seconds)\n    def top_articles(request, category=None):\n        # Some costly queries\n        return HttpResponse(...)\n\n\n``@cached()`` will generate separate entry for each combination of decorated function and its\narguments. Also you can use ``extra`` same way as in ``@cached_as()``, most useful for nested\nfunctions:\n\n.. code:: python\n\n    @property\n    def articles_json(self):\n        @cached(timeout=10*60, extra=self.category_id)\n        def _articles_json():\n            ...\n            return json.dumps(...)\n\n        return _articles_json()\n\n\nYou can manually invalidate or update a result of a cached function:\n\n.. code:: python\n\n    top_articles.invalidate(some_category)\n    top_articles.key(some_category).set(new_value)\n\n\nTo invalidate cached view you can pass absolute uri instead of request:\n\n.. code:: python\n\n    top_articles.invalidate('http://example.com/page', some_category)\n\n\nCacheops also provides get/set primitives for simple cache:\n\n.. code:: python\n\n    from cacheops import cache\n\n    cache.set(cache_key, data, timeout=None)\n    cache.get(cache_key)\n    cache.delete(cache_key)\n\n\n``cache.get`` will raise ``CacheMiss`` if nothing is stored for given key:\n\n.. code:: python\n\n    from cacheops import cache, CacheMiss\n\n    try:\n        result = cache.get(key)\n    except CacheMiss:\n        ... # deal with it\n\n\nFile Cache\n----------\n\nFile based cache can be used the same way as simple time-invalidated one:\n\n.. code:: python\n\n    from cacheops import file_cache\n\n    @file_cache.cached(timeout=number_of_seconds)\n    def top_articles(category):\n        return ... # Some costly queries\n\n    @file_cache.cached_view(timeout=number_of_seconds)\n    def top_articles(request, category):\n        # Some costly queries\n        return HttpResponse(...)\n\n    # later, on appropriate event\n    top_articles.invalidate(some_category)\n    # or\n    top_articles.key(some_category).set(some_value)\n\n    # primitives\n    file_cache.set(cache_key, data, timeout=None)\n    file_cache.get(cache_key)\n    file_cache.delete(cache_key)\n\n\nIt has several improvements upon django built-in file cache, both about high load.\nFirst, it's safe against concurrent writes. Second, it's invalidation is done as separate task,\nyou'll need to call this from crontab for that to work::\n\n    /path/manage.py cleanfilecache\n    /path/manage.py cleanfilecache /path/to/non-default/cache/dir\n\n\nDjango templates integration\n----------------------------\n\nCacheops provides tags to cache template fragments. They mimic ``@cached_as``\nand ``@cached`` decorators, however, they require explicit naming of each fragment:\n\n.. code:: django\n\n    {% load cacheops %}\n\n    {% cached_as \u003cqueryset\u003e \u003ctimeout\u003e \u003cfragment_name\u003e [\u003cextra1\u003e \u003cextra2\u003e ...] %}\n        ... some template code ...\n    {% endcached_as %}\n\n    {% cached \u003ctimeout\u003e \u003cfragment_name\u003e [\u003cextra1\u003e \u003cextra2\u003e ...] %}\n        ... some template code ...\n    {% endcached %}\n\nYou can use ``None`` for timeout in ``@cached_as`` to use it's default value for model.\n\nTo invalidate cached fragment use:\n\n.. code:: python\n\n    from cacheops import invalidate_fragment\n\n    invalidate_fragment(fragment_name, extra1, ...)\n\nIf you have more complex fragment caching needs, cacheops provides a helper to\nmake your own template tags which decorate a template fragment in a way\nanalogous to decorating a function with ``@cached`` or ``@cached_as``.\nThis is **experimental** feature for now.\n\nTo use it create ``myapp/templatetags/mycachetags.py`` and add something like this there:\n\n.. code:: python\n\n    from cacheops import cached_as, CacheopsLibrary\n\n    register = CacheopsLibrary()\n\n    @register.decorator_tag(takes_context=True)\n    def cache_menu(context, menu_name):\n        from django.utils import translation\n        from myapp.models import Flag, MenuItem\n\n        request = context.get('request')\n        if request and request.user.is_staff():\n            # Use noop decorator to bypass caching for staff\n            return lambda func: func\n\n        return cached_as(\n            # Invalidate cache if any menu item or a flag for menu changes\n            MenuItem,\n            Flag.objects.filter(name='menu'),\n            # Vary for menu name and language, also stamp it as \"menu\" to be safe\n            extra=(\"menu\", menu_name, translation.get_language()),\n            timeout=24 * 60 * 60\n        )\n\n``@decorator_tag`` here creates a template tag behaving the same as returned decorator\nupon wrapped template fragment. Resulting template tag could be used as follows:\n\n.. code:: django\n\n    {% load mycachetags %}\n\n    {% cache_menu \"top\" %}\n        ... the top menu template code ...\n    {% endcache_menu %}\n\n    ... some template code ..\n\n    {% cache_menu \"bottom\" %}\n        ... the bottom menu template code ...\n    {% endcache_menu %}\n\n\nJinja2 extension\n----------------\n\nAdd ``cacheops.jinja2.cache`` to your extensions and use:\n\n.. code:: jinja\n\n    {% cached_as \u003cqueryset\u003e [, timeout=\u003ctimeout\u003e] [, extra=\u003ckey addition\u003e] %}\n        ... some template code ...\n    {% endcached_as %}\n\nor\n\n.. code:: jinja\n\n    {% cached [timeout=\u003ctimeout\u003e] [, extra=\u003ckey addition\u003e] %}\n        ...\n    {% endcached %}\n\nTags work the same way as corresponding decorators.\n\n\nSpecial topics\n++++++++++++++\n\nTransactions\n------------\n\nCacheops transparently supports transactions. This is implemented by following simple rules:\n\n1. Once transaction is dirty (has changes) caching turns off. The reason is that the state of database at this point is only visible to current transaction and should not affect other users and vice versa.\n\n2. Any invalidating calls are scheduled to run on the outer commit of transaction.\n\n3. Savepoints and rollbacks are also handled appropriately.\n\nMind that simple and file cache don't turn itself off in transactions but work as usual.\n\n\nDog-pile effect prevention\n--------------------------\n\nThere is optional locking mechanism to prevent several threads or processes simultaneously performing same heavy task. It works with ``@cached_as()`` and querysets:\n\n.. code:: python\n\n    @cached_as(qs, lock=True)\n    def heavy_func(...):\n        # ...\n\n    for item in qs.cache(lock=True):\n        # ...\n\nIt is also possible to specify ``lock: True`` in ``CACHEOPS`` setting but that would probably be a waste. Locking has no overhead on cache hit though.\n\n\nMultiple database support\n-------------------------\n\nBy default cacheops considers query result is same for same query, not depending\non database queried. That could be changed with ``db_agnostic`` cache profile option:\n\n.. code:: python\n\n    CACHEOPS = {\n        'some.model': {'ops': 'get', 'db_agnostic': False, 'timeout': ...}\n    }\n\n\nSharing redis instance\n----------------------\n\nCacheops provides a way to share a redis instance by adding prefix to cache keys:\n\n.. code:: python\n\n    CACHEOPS_PREFIX = lambda query: ...\n    # or\n    CACHEOPS_PREFIX = 'some.module.cacheops_prefix'\n\nA most common usage would probably be a prefix by host name:\n\n.. code:: python\n\n    # get_request() returns current request saved to threadlocal by some middleware\n    cacheops_prefix = lambda _: get_request().get_host()\n\nA ``query`` object passed to callback also enables reflection on used databases and tables:\n\n.. code:: python\n\n    def cacheops_prefix(query):\n        query.dbs    # A list of databases queried\n        query.tables # A list of tables query is invalidated on\n\n        if set(query.tables) \u003c= HELPER_TABLES:\n            return 'helper:'\n        if query.tables == ['blog_post']:\n            return 'blog:'\n\n\nCustom serialization\n--------------------\n\nCacheops uses ``pickle`` by default, employing it's default protocol. But you can specify your own\nit might be any module or a class having ``.dumps()`` and ``.loads()`` functions. For example you can use ``dill`` instead, which can serialize more things like anonymous functions:\n\n.. code:: python\n\n    CACHEOPS_SERIALIZER = 'dill'\n\nOne less obvious use is to fix pickle protocol, to use cacheops cache across python versions:\n\n.. code:: python\n\n    import pickle\n\n    class CACHEOPS_SERIALIZER:\n        dumps = lambda data: pickle.dumps(data, 3)\n        loads = pickle.loads\n\n\nUsing memory limit\n------------------\n\nCacheops offers an \"insideout\" mode, which idea is instead of conj sets contatining cache keys, cache values contain a checksum of random stamps stored in conj keys, which are checked on each read to stay the same. To use that add to settings:\n\n.. code:: python\n\n    CACHEOPS_INSIDEOUT = True  # Might become default in future\n\nAnd set up ``maxmemory`` and ``maxmemory-policy`` in redis config::\n\n    maxmemory 4gb\n    maxmemory-policy volatile-lru  # or other volatile-*\n\nNote that using any of ``allkeys-*`` policies might drop important invalidation structures of cacheops and lead to stale cache.\n\n\nMemory usage cleanup\n--------------------\n\n**This does not apply to \"insideout\" mode. This issue doesn't happen there.**\n\nIn some cases, cacheops may leave some conjunction keys of expired cache keys in redis without being able to invalidate them. Those will still expire with age, but in the meantime may cause issues like slow invalidation (even \"BUSY Redis ...\") and extra memory usage. To prevent that it is advised to not cache complex queries, see `Perfomance tips \u003c#performance-tips\u003e`_, 5.\n\nCacheops ships with a ``cacheops.reap_conjs`` function that can clean up these keys,\nignoring conjunction sets with some reasonable size. It can be called using the ``reapconjs`` management command::\n\n    ./manage.py reapconjs --chunk-size=100 --min-conj-set-size=10000  # with custom values\n    ./manage.py reapconjs                                             # with default values (chunks=1000, min size=1000)\n\nThe command is a small wrapper that calls a function with the main logic. You can also call it from your code, for example from a Celery task:\n\n.. code:: python\n\n    from cacheops import reap_conjs\n\n    @app.task\n    def reap_conjs_task():\n        reap_conjs(\n            chunk_size=2000,\n            min_conj_set_size=100,\n        )\n\n\nKeeping stats\n-------------\n\nCacheops provides ``cache_read`` and ``cache_invalidated`` signals for you to keep track.\n\nCache read signal is emitted immediately after each cache lookup. Passed arguments are: ``sender`` - model class if queryset cache is fetched,\n``func`` - decorated function and ``hit`` - fetch success as boolean value.\n\nHere is a simple stats implementation:\n\n.. code:: python\n\n    from cacheops.signals import cache_read\n    from statsd.defaults.django import statsd\n\n    def stats_collector(sender, func, hit, **kwargs):\n        event = 'hit' if hit else 'miss'\n        statsd.incr('cacheops.%s' % event)\n\n    cache_read.connect(stats_collector)\n\nCache invalidation signal is emitted after object, model or global invalidation passing ``sender`` and ``obj_dict`` args. Note that during normal operation cacheops only uses object invalidation, calling it once for each model create/delete and twice for update: passing old and new object dictionary.\n\n\nTroubleshooting\n+++++++++++++++\n\nCAVEATS\n-------\n\n1. Conditions other than ``__exact``, ``__in`` and ``__isnull=True`` don't make invalidation\n   more granular.\n2. Conditions on TextFields, FileFields and BinaryFields don't make it either.\n   One should not test on their equality anyway. See `CACHEOPS_SKIP_FIELDS` though.\n3. Update of \"select_related\" object does not invalidate cache for queryset.\n   Use ``.prefetch_related()`` instead.\n4. Mass updates don't trigger invalidation by default. But see ``.invalidated_update()``.\n5. Sliced queries are invalidated as non-sliced ones.\n6. Doesn't work with ``.raw()`` and other sql queries.\n7. Conditions on subqueries don't affect invalidation.\n8. Doesn't work right with multi-table inheritance.\n\nHere 1, 2, 3, 5 are part of the design compromise, trying to solve them will make\nthings complicated and slow. 7 can be implemented if needed, but it's\nprobably counter-productive since one can just break queries into simpler ones,\nwhich cache better. 4 is a deliberate choice, making it \"right\" will flush\ncache too much when update conditions are orthogonal to most queries conditions,\nsee, however, `.invalidated_update()`. 8 is postponed until it will gain\nmore interest or a champion willing to implement it emerges.\n\nAll unsupported things could still be used easily enough with the help of ``@cached_as()``.\n\n\nPerformance tips\n----------------\n\nHere come some performance tips to make cacheops and Django ORM faster.\n\n1. 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 \u003chttp://github.com/Suor/django-pickling\u003e`_.\n\n2. Constructing querysets is rather slow in django, mainly because most of ``QuerySet`` methods clone self, then change it and return the clone. Original queryset is usually thrown away. Cacheops adds ``.inplace()`` method, which makes queryset mutating, preventing useless cloning:\n\n   .. code:: python\n\n    items = Item.objects.inplace().filter(category=12).order_by('-date')[:20]\n\n   You can revert queryset to cloning state using ``.cloning()`` call. Note that this is a micro-optimization technique. Using it is only desirable in the hottest places, not everywhere.\n\n3. 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 a list of model instances.\n\n4. Run separate redis instance for cache with disabled `persistence \u003chttp://redis.io/topics/persistence\u003e`_. You can manually call `SAVE \u003chttp://redis.io/commands/save\u003e`_ or `BGSAVE \u003chttp://redis.io/commands/bgsave\u003e`_ to stay hot upon server restart.\n\n5. 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 heuristics which detect if this request would be probably hit. E.g. enable cache if only some primary fields are used in filter.\n\n   Caching querysets with large amount of filters also slows down all subsequent invalidation on that model (negligable for \"insideout\" mode). You can disable caching if more than some amount of fields is used in filter simultaneously.\n\n6. Split database queries into smaller ones when you cache them. This goes against usual approach, but this allows invalidation to be more granular: smaller parts will be invalidated independently and each part will invalidate more precisely.\n\n   .. code:: python\n\n    Post.objects.filter(category__slug=\"foo\")\n    # A single database query, but will be invalidated not only on\n    # any Category with .slug == \"foo\" change, but also for any Post change\n\n    Post.objects.filter(category=Category.objects.get(slug=\"foo\"))\n    # Two queries, each invalidates only on a granular event:\n    # either category.slug == \"foo\" or Post with .category_id == \u003cwhatever is there\u003e\n\n\nWriting a test\n--------------\n\nWriting a test for an issue you are experiencing can speed up its resolution a lot.\nHere is how you do that. I suppose you have some application code causing it.\n\n1. Make a fork.\n2. Install all from ``requirements-test.txt``.\n3. Ensure you can run tests with ``pytest``.\n4. Copy relevant models code to ``tests/models.py``.\n5. Go to ``tests/tests.py`` and paste code causing exception to ``IssueTests.test_{issue_number}``.\n6. Execute ``pytest -k {issue_number}`` and see it failing.\n7. Cut down model and test code until error disappears and make a step back.\n8. Commit changes and make a pull request.\n\n\nTODO\n++++\n\n- faster .get() handling for simple cases such as get by pk/id, with simple key calculation\n- integrate previous one with prefetch_related()\n- shard cache between multiple redises\n- respect subqueries?\n- respect headers in @cached_view*?\n- group invalidate_obj() calls?\n- a postpone invalidation context manager/decorator?\n- fast mode: store cache in local memory, but check in with redis if it's valid\n- an interface for complex fields to extract exact on parts or transforms: ArrayField.len =\u003e field__len=?, ArrayField[0] =\u003e field__0=?, JSONField['some_key'] =\u003e field__some_key=?\n- custom cache eviction strategy in lua\n- cache a string directly (no pickle) for direct serving (custom key function?)\n\n\n.. |Build Status| image:: https://github.com/Suor/django-cacheops/actions/workflows/ci.yml/badge.svg\n   :target: https://github.com/Suor/django-cacheops/actions/workflows/ci.yml?query=branch%3Amaster\n","funding_links":[],"categories":["Third-Party Packages","Python","Caching","资源列表","缓存","Uncategorized","Django Utilities","Caching [🔝](#readme)","Awesome Python"],"sub_categories":["Caching","缓存","JWT","Tools","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSuor%2Fdjango-cacheops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSuor%2Fdjango-cacheops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSuor%2Fdjango-cacheops/lists"}