### Install django-cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Installs the django-cacheops package using pip, either from PyPI or directly from its GitHub repository. ```bash $ pip install django-cacheops # Or from github directly $ pip install git+https://github.com/Suor/django-cacheops.git@master ``` -------------------------------- ### File Cache Primitives Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows the basic set, get, and delete operations for the file cache. ```python from cacheops import file_cache file_cache.set(cache_key, data, timeout=None) file_cache.get(cache_key) file_cache.delete(cache_key) ``` -------------------------------- ### Cache Get/Set Primitives Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates the basic cache primitives for setting, getting, and deleting data from the cache. ```python from cacheops import cache cache.set(cache_key, data, timeout=None) cache.get(cache_key) cache.delete(cache_key) ``` -------------------------------- ### Dynamic Cacheops Prefix Based on Query Reflection Source: https://github.com/suor/django-cacheops/blob/master/README.rst Provides an example of a dynamic cacheops prefix function that inspects the query object to determine the prefix. This allows for context-aware caching based on databases queried and tables involved. ```python def cacheops_prefix(query): query.dbs # A list of databases queried query.tables # A list of tables query is invalidated on if set(query.tables) <= HELPER_TABLES: return 'helper:' if query.tables == ['blog_post']: return 'blog:' ``` -------------------------------- ### Create Custom Django Template Tag with Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Provides an example of creating a custom Django template tag using cacheops' `decorator_tag`. This allows for complex caching logic, such as conditional caching for staff users or varying cache keys based on context and language. ```python from cacheops import cached_as, CacheopsLibrary register = CacheopsLibrary() @register.decorator_tag(takes_context=True) def cache_menu(context, menu_name): from django.utils import translation from myapp.models import Flag, MenuItem request = context.get('request') if request and request.user.is_staff(): # Use noop decorator to bypass caching for staff return lambda func: func return cached_as( # Invalidate cache if any menu item or a flag for menu changes MenuItem, Flag.objects.filter(name='menu'), # Vary for menu name and language, also stamp it as "menu" to be safe extra=("menu", menu_name, translation.get_language()), timeout=24 * 60 * 60 ) ``` -------------------------------- ### Granular Cache Invalidation with Smaller Queries Source: https://github.com/suor/django-cacheops/blob/master/README.rst Illustrates how splitting database queries can lead to more granular cache invalidation. The first example shows a single query that invalidates on broader changes, while the second uses two queries for more precise invalidation based on specific events. ```python Post.objects.filter(category__slug="foo") # A single database query, but will be invalidated not only on # any Category with .slug == "foo" change, but also for any Post change ``` ```python Post.objects.filter(category=Category.objects.get(slug="foo")) # Two queries, each invalidates only on a granular event: # either category.slug == "foo" or Post with .category_id == ``` -------------------------------- ### Define Caching Rules in Django Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Configures automatic caching rules for Django models using the CACHEOPS setting. It specifies operations ('get', 'fetch', 'all'), timeouts, and model patterns. It also demonstrates how to disable caching for specific models. ```python CACHEOPS = { # Automatically cache any User.objects.get() calls for 15 minutes # This also includes .first() and .last() calls, # as well as request.user or post.author access, # where Post.author is a foreign key to auth.User 'auth.user': {'ops': 'get', 'timeout': 60*15}, # Automatically cache all gets and queryset fetches # to other django.contrib.auth models for an hour 'auth.*': {'ops': {'fetch', 'get'}, 'timeout': 60*60}, # Cache all queries to Permission # 'all' is an alias for {'get', 'fetch', 'count', 'aggregate', 'exists'} 'auth.permission': {'ops': 'all', 'timeout': 60*60}, # Enable manual caching on all other models with default timeout of an hour # Use Post.objects.cache().get(...) # or Tags.objects.filter(...).order_by(...).cache() # to cache particular ORM request. # Invalidation is still automatic '*.*': {'ops': (), 'timeout': 60*60}, # And since ops is empty by default you can rewrite last line as: '*.*': {'timeout': 60*60}, # NOTE: binding signals has its overhead, like preventing fast mass deletes, # you might want to only register whatever you cache and dependencies. # Finally you can explicitely forbid even manual caching with: 'some_app.*': None, } ``` -------------------------------- ### Cache Specific Queryset Operations Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache only specific operations (e.g., 'count') for a queryset using the .cache() method with the 'ops' argument. This example caches the count operation but not the subsequent fetching of articles. ```python qs = Article.objects.filter(tag=2).cache(ops=['count']) # ... later usage ... ``` -------------------------------- ### Call Cacheops Reap Conjunctions from Code Source: https://github.com/suor/django-cacheops/blob/master/README.rst Programmatically calls the cacheops.reap_conjs function, for example, from a Celery task, with specified chunk size and minimum conjunction set size. ```python from cacheops import reap_conjs @app.task def reap_conjs_task(): reap_conjs( chunk_size=2000, min_conj_set_size=100, ) ``` -------------------------------- ### Configure Redis Connection for Django Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Sets up the Redis connection parameters for django-cacheops in Django's settings. It shows configurations using host/port, a Redis URL, a Unix socket path, and Sentinel for high availability. It also covers specifying a custom Redis client class. ```python CACHEOPS_REDIS = { 'host': 'localhost', # redis-server is on same machine 'port': 6379, # default redis port 'db': 1, # SELECT non-default redis database # using separate redis db or redis instance # is highly recommended 'socket_timeout': 3, # connection timeout in seconds, optional 'password': '...', # optional 'unix_socket_path': '' # replaces host and port } # Alternatively the redis connection can be defined using a URL: CACHEOPS_REDIS = "redis://localhost:6379/1" # or CACHEOPS_REDIS = "unix://path/to/socket?db=1" # or with password (note a colon) CACHEOPS_REDIS = "redis://:password@localhost:6379/1" # If you want to use sentinel, specify this variable CACHEOPS_SENTINEL = { 'locations': [('localhost', 26379)], # sentinel locations, required 'service_name': 'mymaster', # sentinel service name, required 'socket_timeout': 0.1, # connection timeout in seconds, optional 'db': 0 # redis database, default: 0 ... # everything else is passed to Sentinel() } # Use your own redis client class, should be compatible or subclass redis.Redis CACHEOPS_CLIENT_CLASS = 'your.redis.ClientClass' ``` -------------------------------- ### Optimize Queryset Cloning with inplace() Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates how to use the `.inplace()` method to make querysets mutating, thereby preventing unnecessary cloning and improving performance. The `.cloning()` method can be used to revert to the default cloning behavior. This is a micro-optimization for critical code paths. ```python items = Item.objects.inplace().filter(category=12).order_by('-date')[:20] ``` -------------------------------- ### Share Redis Instance with Cacheops Prefix Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows how to share a Redis instance among different applications or cache scopes by defining a prefix for cache keys. This can be done using a string or a callable that generates the prefix dynamically. ```python CACHEOPS_PREFIX = lambda query: ... # or CACHEOPS_PREFIX = 'some.module.cacheops_prefix' ``` -------------------------------- ### Configure Cacheops for Multiple Databases Source: https://github.com/suor/django-cacheops/blob/master/README.rst Explains how to configure cacheops to be database-agnostic or database-specific. The `db_agnostic` option in the `CACHEOPS` setting controls whether cache keys are generated independently of the database queried. ```python CACHEOPS = { 'some.model': {'ops': 'get', 'db_agnostic': False, 'timeout': ...} } ``` -------------------------------- ### Jinja2 Cached Tag Usage Source: https://github.com/suor/django-cacheops/blob/master/README.rst Illustrates the usage of cacheops' Jinja2 extension for template fragment caching. It shows the syntax for `cached_as` and `cached` tags with optional timeout and extra key parameters. ```jinja {% cached_as [, timeout=] [, extra=] %} ... some template code ... {% endcached_as %} or {% cached [timeout=] [, extra=] %} ... {% endcached %} ``` -------------------------------- ### Advanced Caching Options in Django Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Explains advanced caching configurations in django-cacheops, including `local_get` for in-process memory caching and `cache_on_save` for caching instances upon saving, which can be triggered by primary key or a specific field. ```python ``local_get: True`` To cache simple gets for this model in process local memory. This is very fast, but is not invalidated in any way until process is restarted. Still could be useful for extremely rarely changed things. ``cache_on_save=True | 'field_name'`` To write an instance to cache upon save. Cached instance will be retrieved on ``.get(field_name=...)`` request. Setting to ``True`` causes caching by primary key. ``` -------------------------------- ### Handling Cache Miss Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows how to handle a `CacheMiss` exception when trying to retrieve data that is not present in the cache. ```python from cacheops import cache, CacheMiss try: result = cache.get(key) except CacheMiss: ... # deal with it ``` -------------------------------- ### Redis Configuration for Insideout Mode Source: https://github.com/suor/django-cacheops/blob/master/README.rst Configures Redis 'maxmemory' and 'maxmemory-policy' for cacheops 'insideout' mode. Using 'allkeys-*' policies may lead to stale cache. ```bash maxmemory 4gb maxmemory-policy volatile-lru # or other volatile-* ``` -------------------------------- ### Clean File Cache Command Source: https://github.com/suor/django-cacheops/blob/master/README.rst Provides the management command to clean the file cache, optionally specifying a non-default cache directory. ```bash /path/manage.py cleanfilecache /path/manage.py cleanfilecache /path/to/non-default/cache/dir ``` -------------------------------- ### File Cache Manual Invalidation and Update Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates manual invalidation and updating of cached items using the file cache. ```python top_articles.invalidate(some_category) # or top_articles.key(some_category).set(some_value) ``` -------------------------------- ### File Cache Decorators Source: https://github.com/suor/django-cacheops/blob/master/README.rst Illustrates using `@file_cache.cached` and `@file_cache.cached_view` for file-based time-invalidated caching. ```python from cacheops import file_cache @file_cache.cached(timeout=number_of_seconds) def top_articles(category): return ... # Some costly queries @file_cache.cached_view(timeout=number_of_seconds) def top_articles(request, category): # Some costly queries return HttpResponse(...) ``` -------------------------------- ### Django Template Cache Fragment Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates the usage of `{% cached_as %}` template tag for caching fragments in Django templates. ```django {% load cacheops %} {% cached_as [ ...] %} ... some template code ... {% endcached_as %} ``` -------------------------------- ### Fix Pickle Protocol for Cross-Version Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Allows cacheops cache to be used across different Python versions by fixing the pickle protocol version. ```python import pickle class CACHEOPS_SERIALIZER: dumps = lambda data: pickle.dumps(data, 3) loads = pickle.loads ``` -------------------------------- ### Degrade Gracefully on Redis Fail Source: https://github.com/suor/django-cacheops/blob/master/README.rst Configure cacheops to degrade gracefully when Redis fails by setting the CACHEOPS_DEGRADE_ON_FAILURE option to True. ```python CACHEOPS_DEGRADE_ON_FAILURE = True ``` -------------------------------- ### Configure Cacheops Serializer Source: https://github.com/suor/django-cacheops/blob/master/README.rst Sets the serializer for cacheops. 'dill' can serialize more complex objects like anonymous functions. ```python CACHEOPS_SERIALIZER = 'dill' ``` -------------------------------- ### Mass Updates with Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Introduces the `invalidated_update` method for performing mass updates on QuerySets while ensuring cache invalidation. ```python qs.invalidated_update(...) ``` -------------------------------- ### Cache View with Dynamic Extra Parameter Source: https://github.com/suor/django-cacheops/blob/master/README.rst Provide a function to the 'extra' parameter of @cached_view_as to dynamically include request-specific information in the cache key, allowing for personalized caching. ```python from cacheops import cached_view_as @cached_view_as(News, extra=lambda req: req.user.is_staff) def news_index(request): # ... add extra things for staff return render(...) ``` -------------------------------- ### Configure Default Caching Profile in Django Cacheops Source: https://github.com/suor/django-cacheops/blob/master/README.rst Sets default caching parameters like timeout using CACHEOPS_DEFAULTS, which can then be overridden for specific models in the CACHEOPS setting. This simplifies configuration for common caching behaviors. ```python CACHEOPS_DEFAULTS = { 'timeout': 60*60 } CACHEOPS = { 'auth.user': {'ops': 'get', 'timeout': 60*15}, 'auth.*': {'ops': ('fetch', 'get')}, 'auth.permission': {'ops': 'all'}, '*.*': {}, } ``` -------------------------------- ### Run Cacheops Reap Conjunctions Management Command Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cleans up conjunction keys of expired cache keys in Redis that may cause slow invalidation or extra memory usage. Allows custom chunk size and minimum conjunction set size. ```bash ./manage.py reapconjs --chunk-size=100 --min-conj-set-size=10000 # with custom values ./manage.py reapconjs # with default values (chunks=1000, min size=1000) ``` -------------------------------- ### Cached Function with Extra Arguments Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows how to use the `extra` argument with the `@cached` decorator for caching based on additional parameters, useful for nested functions. ```python from cacheops import cached import json @property def articles_json(self): @cached(timeout=10*60, extra=self.category_id) def _articles_json(): ... # Fetch articles return json.dumps(...) return _articles_json() ``` -------------------------------- ### Use Custom Cache Tag in Django Template Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows how to load and use a custom template tag (`cache_menu`) within a Django template. The custom tag, defined using cacheops, caches the enclosed template fragment based on the provided arguments. ```django {% load mycachetags %} {% cache_menu "top" %} ... the top menu template code ... {% endcache_menu %} ... some template code .. {% cache_menu "bottom" %} ... the bottom menu template code ... {% endcache_menu %} ``` -------------------------------- ### Invalidate Model and All Cache Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates how to invalidate all queries for a specific model and how to flush the entire Redis cache database. ```python from cacheops import invalidate_model, invalidate_all invalidate_model(Article) # invalidates all queries for model invalidate_all() # flush redis cache database ``` -------------------------------- ### Prevent Dog-pile Effect with Cacheops Locking Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates how to prevent the dog-pile effect (multiple threads/processes performing the same heavy task simultaneously) using cacheops' locking mechanism. This can be applied to functions decorated with `@cached_as` or when iterating over querysets. ```python @cached_as(qs, lock=True) def heavy_func(...): # ... for item in qs.cache(lock=True): # ... ``` -------------------------------- ### Time-Invalided Cache Decorators Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates how to use `@cached` and `@cached_view` decorators to cache function and view results for a specified duration. ```python from cacheops import cached, cached_view @cached(timeout=number_of_seconds) def top_articles(category): return ... # Some costly queries @cached_view(timeout=number_of_seconds) def top_articles(request, category=None): # Some costly queries return HttpResponse(...) ``` -------------------------------- ### Connect Cache Read Signal for Stats Source: https://github.com/suor/django-cacheops/blob/master/README.rst Connects a stats collector function to the cacheops 'cache_read' signal to increment statsd counters for cache hits and misses. ```python from cacheops.signals import cache_read from statsd.defaults.django import statsd def stats_collector(sender, func, hit, **kwargs): event = 'hit' if hit else 'miss' statsd.incr('cacheops.%s' % event) cache_read.connect(stats_collector) ``` -------------------------------- ### Disable Cacheops for Testing Source: https://github.com/suor/django-cacheops/blob/master/README.rst Temporarily disable all cacheops functionality for testing purposes using Django's override_settings decorator. ```python from django.test import override_settings @override_settings(CACHEOPS_ENABLED=False) def test_something(): # ... assert cond ``` -------------------------------- ### Enable Cacheops Insideout Mode Source: https://github.com/suor/django-cacheops/blob/master/README.rst Enables 'insideout' mode for cacheops, where cache values contain checksums of random stamps stored in conjunction keys. ```python CACHEOPS_INSIDEOUT = True ``` -------------------------------- ### Manual Cache Invalidation and Update Source: https://github.com/suor/django-cacheops/blob/master/README.rst Illustrates how to manually invalidate cached function results or update cache entries using generated methods. ```python top_articles.invalidate(some_category) top_articles.key(some_category).set(new_value) ``` -------------------------------- ### Manage.py Invalidate Commands Source: https://github.com/suor/django-cacheops/blob/master/README.rst Shows the usage of the 'invalidate' management command for invalidating specific objects, models, or all models within an app. ```bash ./manage.py invalidate articles.Article.34 # same as invalidate_obj ./manage.py invalidate articles.Article # same as invalidate_model ./manage.py invalidate articles # invalidate all models in articles ./manage.py invalidate all # FLUSHES cacheops redis database ``` -------------------------------- ### Cache Class-Based View Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache class-based views by applying the @cached_view_as decorator to the view's as_view() method. This allows leveraging caching for CBVs. ```python from django.views.generic import ListView from cacheops import cached_view_as class NewsIndex(ListView): model = News news_index = cached_view_as(News)(NewsIndex.as_view()) ``` -------------------------------- ### Cache View with @cached_view_as Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache the output of a view function using the @cached_view_as decorator. The cache key is constructed using the request path and optionally other parameters. ```python from cacheops import cached_view_as @cached_view_as(News) def news_index(request): # ... return render(...) ``` -------------------------------- ### Manually Cache Queryset Source: https://github.com/suor/django-cacheops/blob/master/README.rst Force a specific queryset to be cached by calling the .cache() method on it. This allows explicit control over which operations are cached. ```python Article.objects.filter(tag=2).cache() ``` -------------------------------- ### Override Queryset Cache Timeout Source: https://github.com/suor/django-cacheops/blob/master/README.rst Set a custom cache timeout for a specific queryset by passing the 'timeout' argument to the .cache() method. ```python qs = Article.objects.filter(visible=True).cache(timeout=300) ``` -------------------------------- ### Cache Function with Granular Invalidation Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache function results with more granular invalidation by using a local function and passing arguments that affect the cache key. The 'extra' parameter can be used to include function arguments in the cache key. ```python from cacheops import cached_as def articles_block(category, count=5): qs = Article.objects.filter(category=category) @cached_as(qs, extra=count) def _articles_block(): articles = list(qs.filter(photo=True)[:count]) if len(articles) < count: articles += list(qs.filter(photo=False)[:count-len(articles)]) return articles return _articles_block() ``` -------------------------------- ### Invalidate Cached View Source: https://github.com/suor/django-cacheops/blob/master/README.rst Explains how to invalidate a cached view by providing the absolute URI and relevant arguments. ```python top_articles.invalidate('http://example.com/page', some_category) ``` -------------------------------- ### Disable Caching for Queryset Source: https://github.com/suor/django-cacheops/blob/master/README.rst Use the .nocache() method as a shortcut to disable automatic caching for a particular queryset. Subsequent operations on this queryset will hit the database. ```python qs = Article.objects.filter(visible=True).nocache() qs1 = qs.filter(tag=2) # hits database qs2 = qs.filter(category=3) # hits it once more ``` -------------------------------- ### Cache Function with Multiple Invalidation Models Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache function results based on changes to multiple models or querysets by passing them as arguments to the @cached_as decorator. ```python from cacheops import cached_as @cached_as(Article.objects.filter(public=True), Tag) def article_stats(): return {...} ``` -------------------------------- ### Turn Off Invalidation Temporarily Source: https://github.com/suor/django-cacheops/blob/master/README.rst Provides methods to temporarily disable cache invalidation using a context manager or a decorator, useful for batch operations. ```python from cacheops import no_invalidation with no_invalidation: # ... do some changes obj.save() ``` ```python from cacheops import no_invalidation @no_invalidation def some_work(...): # ... do some changes obj.save() ``` ```python from cacheops import invalidate_obj, invalidate_model, no_invalidation try: with no_invalidation: # ... finally: invalidate_obj(...) # ... or invalidate_model(...) ``` -------------------------------- ### Invalidate Cache Fragment in Django Source: https://github.com/suor/django-cacheops/blob/master/README.rst Demonstrates how to invalidate a specific cached fragment in Django using the `invalidate_fragment` function from the cacheops library. This is useful when the content of a cached template fragment needs to be updated or cleared. ```python from cacheops import invalidate_fragment invalidate_fragment(fragment_name, extra1, ...) ``` -------------------------------- ### Cache Function Results with @cached_as Source: https://github.com/suor/django-cacheops/blob/master/README.rst Cache the results of a function using the @cached_as decorator. The cache is invalidated on changes to the specified model (e.g., Article). ```python from cacheops import cached_as from django.db.models import Count @cached_as(Article, timeout=120) def article_stats(): return { 'tags': list(Article.objects.values('tag').annotate(Count('id'))), 'categories': list(Article.objects.values('category').annotate(Count('id'))) } ``` -------------------------------- ### Invalidate Specific Object Cache Source: https://github.com/suor/django-cacheops/blob/master/README.rst Manually invalidate the cache for all queries affected by a specific object using the invalidate_obj function. ```python from cacheops import invalidate_obj invalidate_obj(some_article) ``` -------------------------------- ### Invalidate All Caches Source: https://github.com/suor/django-cacheops/blob/master/README.rst Clear all cached data managed by cacheops using the invalidate_all function. Use this with caution as it affects all cached items. ```python from cacheops import invalidate_all invalidate_all() ``` -------------------------------- ### Invalidate Model Cache Source: https://github.com/suor/django-cacheops/blob/master/README.rst Invalidate all caches associated with a specific model using the invalidate_model function. This is a broader invalidation than invalidate_obj. ```python from cacheops import invalidate_model invalidate_model(Article) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.