• Get Review Board
  • What's New
  • Products
  • Review Board Code review, image review, and document review
  • Documentation
  • Release Notes
  • Power Pack Enterprise integrations, reports, and enhanced document review
  • Try for 60 Days
  • Purchase
  • RBCommons Review Board as a Service, hosted by us
  • Pricing
  • RBTools Command line tools and Python API for Review Board
  • Documentation
  • Release Notes
  • Review Bot Automated code review, connecting tools you already use
  • Documentation
  • Release Notes
  • RB Gateway Manage Git and Mercurial repositories in your network
  • Documentation
  • Release Notes
  • Learn and Explore
  • What is Code Review?
  • Documentation
  • Frequently Asked Questions
  • Support Options
  • Third-Party Integrations
  • Demo
  • Review Board RBTools Power Pack Review Bot Djblets RB Gateway
    1. Djblets 4.x
    2. Version 5.x
    3. Version 4.x
    4. Version 3.x
    5. Version 2.x
    6. Version 2.0
    7. Version 1.0
    8. Version 0.9
    9. Djblets Documentation
    10. Module and Class References
    11. djblets.cache.backend
  • Home
  • Guides
  • Avatar Services Guides
  • Writing Avatar Services
  • Extension Guides
  • Writing Extensions
  • Testing Extensions
  • Feature Checks Guides
  • Introduction to Feature Checks
  • Writing Features
  • Writing Feature Checkers
  • Testing with Feature Checks
  • Integration Guides
  • Supporting Integrations
  • Writing Integrations
  • Privacy Compliance Guides
  • Getting and Checking Consent
  • Working with Personally Identifiable Information
  • Service Integrations
  • reCAPTCHA Guides
  • Using reCAPTCHA
  • Registries Guides
  • Writing Registries
  • Web API Guides
  • Writing Web API Resources
  • Adding OAuth2 Support
  • Module and Class References
  • djblets
  • djblets.deprecation
  • djblets.auth.forms
  • djblets.auth.ratelimit
  • djblets.auth.signals
  • djblets.auth.util
  • djblets.auth.views
  • djblets.avatars.errors
  • djblets.avatars.forms
  • djblets.avatars.registry
  • djblets.avatars.services
  • djblets.avatars.services.base
  • djblets.avatars.services.fallback
  • djblets.avatars.services.file_upload
  • djblets.avatars.services.gravatar
  • djblets.avatars.services.url
  • djblets.avatars.settings
  • djblets.cache.backend
  • djblets.cache.backend_compat
  • djblets.cache.context_processors
  • djblets.cache.errors
  • djblets.cache.forwarding_backend
  • djblets.cache.serials
  • djblets.cache.synchronizer
  • djblets.conditions
  • djblets.conditions.choices
  • djblets.conditions.conditions
  • djblets.conditions.errors
  • djblets.conditions.operators
  • djblets.conditions.values
  • djblets.configforms.forms
  • djblets.configforms.mixins
  • djblets.configforms.pages
  • djblets.configforms.registry
  • djblets.configforms.views
  • djblets.datagrid.grids
  • djblets.db.backends.mysql.base
  • djblets.db.fields
  • djblets.db.fields.base64_field
  • djblets.db.fields.counter_field
  • djblets.db.fields.json_field
  • djblets.db.fields.modification_timestamp_field
  • djblets.db.fields.relation_counter_field
  • djblets.db.managers
  • djblets.db.query
  • djblets.db.validators
  • djblets.extensions.admin
  • djblets.extensions.errors
  • djblets.extensions.extension
  • djblets.extensions.forms
  • djblets.extensions.hooks
  • djblets.extensions.loaders
  • djblets.extensions.manager
  • djblets.extensions.middleware
  • djblets.extensions.models
  • djblets.extensions.packaging
  • djblets.extensions.resources
  • djblets.extensions.settings
  • djblets.extensions.signals
  • djblets.extensions.staticfiles
  • djblets.extensions.testing
  • djblets.extensions.testing.testcases
  • djblets.extensions.urls
  • djblets.extensions.views
  • djblets.extensions.templatetags.djblets_extensions
  • djblets.features
  • djblets.features.checkers
  • djblets.features.decorators
  • djblets.features.errors
  • djblets.features.feature
  • djblets.features.level
  • djblets.features.registry
  • djblets.features.testing
  • djblets.features.templatetags.features
  • djblets.feedview.views
  • djblets.feedview.templatetags.feedtags
  • djblets.forms.fields
  • djblets.forms.fieldsets
  • djblets.forms.forms
  • djblets.forms.forms.key_value_form
  • djblets.forms.widgets
  • djblets.gravatars
  • djblets.gravatars.templatetags.gravatars
  • djblets.http.middleware
  • djblets.http.responses
  • djblets.integrations.errors
  • djblets.integrations.forms
  • djblets.integrations.hooks
  • djblets.integrations.integration
  • djblets.integrations.manager
  • djblets.integrations.mixins
  • djblets.integrations.models
  • djblets.integrations.urls
  • djblets.integrations.views
  • djblets.log
  • djblets.log.middleware
  • djblets.log.siteconfig
  • djblets.log.urls
  • djblets.log.views
  • djblets.mail.dmarc
  • djblets.mail.message
  • djblets.mail.testing
  • djblets.mail.utils
  • djblets.markdown
  • djblets.markdown.extensions.escape_html
  • djblets.markdown.extensions.wysiwyg
  • djblets.markdown.extensions.wysiwyg_email
  • djblets.pipeline.compilers.es6
  • djblets.pipeline.compilers.less
  • djblets.pipeline.compilers.mixins
  • djblets.pipeline.compilers.rollup
  • djblets.pipeline.compilers.typescript
  • djblets.pipeline.settings
  • djblets.privacy.consent
  • djblets.privacy.consent.base
  • djblets.privacy.consent.common
  • djblets.privacy.consent.errors
  • djblets.privacy.consent.forms
  • djblets.privacy.consent.hooks
  • djblets.privacy.consent.registry
  • djblets.privacy.consent.tracker
  • djblets.privacy.models
  • djblets.privacy.pii
  • djblets.privacy.templatetags.djblets_privacy
  • djblets.recaptcha.mixins
  • djblets.recaptcha.siteconfig
  • djblets.recaptcha.templatetags.djblets_recaptcha
  • djblets.recaptcha.widgets
  • djblets.registries
  • djblets.registries.errors
  • djblets.registries.importer
  • djblets.registries.mixins
  • djblets.registries.registry
  • djblets.registries.signals
  • djblets.secrets
  • djblets.secrets.crypto
  • djblets.secrets.token_generators
  • djblets.secrets.token_generators.base
  • djblets.secrets.token_generators.legacy_sha1
  • djblets.secrets.token_generators.registry
  • djblets.secrets.token_generators.vendor_checksum
  • djblets.siteconfig
  • djblets.siteconfig.admin
  • djblets.siteconfig.context_processors
  • djblets.siteconfig.django_settings
  • djblets.siteconfig.forms
  • djblets.siteconfig.managers
  • djblets.siteconfig.middleware
  • djblets.siteconfig.models
  • djblets.siteconfig.signals
  • djblets.siteconfig.views
  • djblets.template.caches
  • djblets.template.context
  • djblets.template.loaders.conditional_cached
  • djblets.template.loaders.namespaced_app_dirs
  • djblets.testing.decorators
  • djblets.testing.testcases
  • djblets.testing.testrunners
  • djblets.urls.context_processors
  • djblets.urls.decorators
  • djblets.urls.patterns
  • djblets.urls.resolvers
  • djblets.urls.root
  • djblets.urls.staticfiles
  • djblets.util.compat.django.core.cache
  • djblets.util.compat.python.past
  • djblets.util.contextmanagers
  • djblets.util.dates
  • djblets.util.decorators
  • djblets.util.filesystem
  • djblets.util.functional
  • djblets.util.html
  • djblets.util.http
  • djblets.util.humanize
  • djblets.util.json_utils
  • djblets.util.properties
  • djblets.util.serializers
  • djblets.util.symbols
  • djblets.util.templatetags.djblets_deco
  • djblets.util.templatetags.djblets_email
  • djblets.util.templatetags.djblets_forms
  • djblets.util.templatetags.djblets_images
  • djblets.util.templatetags.djblets_js
  • djblets.util.templatetags.djblets_utils
  • djblets.util.typing
  • djblets.util.views
  • djblets.views.generic.base
  • djblets.views.generic.etag
  • djblets.webapi.auth
  • djblets.webapi.auth.backends
  • djblets.webapi.auth.backends.api_tokens
  • djblets.webapi.auth.backends.base
  • djblets.webapi.auth.backends.basic
  • djblets.webapi.auth.backends.oauth2_tokens
  • djblets.webapi.auth.views
  • djblets.webapi.decorators
  • djblets.webapi.encoders
  • djblets.webapi.errors
  • djblets.webapi.fields
  • djblets.webapi.managers
  • djblets.webapi.models
  • djblets.webapi.oauth2_scopes
  • djblets.webapi.resources
  • djblets.webapi.resources.base
  • djblets.webapi.resources.group
  • djblets.webapi.resources.registry
  • djblets.webapi.resources.root
  • djblets.webapi.resources.user
  • djblets.webapi.resources.mixins.api_tokens
  • djblets.webapi.resources.mixins.forms
  • djblets.webapi.resources.mixins.oauth2_tokens
  • djblets.webapi.resources.mixins.queries
  • djblets.webapi.responses
  • djblets.webapi.signals
  • djblets.webapi.testing
  • djblets.webapi.testing.decorators
  • djblets.webapi.testing.testcases
  • General Index
  • Python Module Index
  • Release Notes
  • This documentation covers Djblets 4.x. You can select a version above or view the latest documentation.

    djblets.cache.backend¶

    Utility functions for working with memory caching backends.

    These functions are designed to integrate with a cache backend using Django’s cache framework. They handle creating caching keys unique to the install and caching more complex data efficiently (such as the results of iterators and large data normally too big for the cache).

    cache_memoize_iter(key, items_or_callable, expiration=2592000, force_overwrite=False, compress_large_data=True, use_encryption=None, encryption_key=None)[source]¶

    Memoize an iterable list of items inside the configured cache.

    If the provided list of items is a function, the function must return a an iterable object, such as a list or a generator.

    If a generator is provided, directly or through a function, then each item will be immediately yielded to the caller as they’re retrieved, and the cached entries will be built up as the items are processed.

    The data is assumed to be big enough that it must be pickled, optionally compressed, and stored as chunks in the cache.

    Data can be encrypted using AES encryption, for safe storage of potentially sensitive information or state. This adds to the processing time slightly, but can be important for cache keys containing sensitive information or that impact access control, particularly in the event that the cache is compromised or shared between services.

    The result from this function is always a generator. Note that it’s important that the generator be allowed to continue until completion, or the data won’t be retrievable from the cache.

    Changed in version 3.0: Added support for encrypting keys and data through the use_encryption and encryption_key arguments.

    Parameters:
    • key (unicode) – The key to use in the cache.

    • items_or_callable (list or callable) – A list of items or callable returning a list of items to cache, if the key is not already found in cache.

    • expiration (int) – The expiration time for the key, in seconds.

    • force_overwrite (bool) – If True, the value will always be computed and stored regardless of whether it exists in the cache already.

    • compress_large_data (bool) – If True, the data will be zlib-compressed.

    • use_encryption (bool, optional) –

      Whether to use encryption when storing or reading data.

      If reading data, and if the data cannot be decrypted with the given key, then the data will be considered to have fallen out of cache.

      This defaults to False, but can be forced on for all cached data by setting settings.DJBLETS_CACHE_FORCE_ENCRYPTION=True.

      New in version 3.0.

    • encryption_key (bytes or str, optional) –

      An explicit AES encryption key to use when passing use_encryption=True.

      This defaults the value in settings.DJBLETS_CACHE_DEFAULT_ENCRYPTION_KEY, if set, or to the default AES encryption key for the server as provided by djblets.secrets.crypto.get_default_aes_encryption_key().

      New in version 3.0.

    Yields:

    object – The list of items from the cache or from items_or_callable if uncached.

    cache_memoize(key, lookup_callable, expiration=2592000, force_overwrite=False, large_data=False, compress_large_data=True, use_generator=False, use_encryption=None, encryption_key=None)[source]¶

    Memoize the results of a callable inside the configured cache.

    Data can be encrypted using AES encryption, for safe storage of potentially sensitive information or state. This adds to the processing time slightly, but can be important for cache keys containing sensitive information or that impact access control, particularly in the event that the cache is compromised or shared between services.

    Changed in version 3.0: Added support for encrypting keys and data through the use_encryption and encryption_key arguments.

    Changed in version 2.2.4: Added support for non-iterable value types.

    Parameters:
    • key (unicode) – The key to use in the cache.

    • lookup_callable (callable) – A callable to execute in the case where the data did not exist in the cache.

    • expiration (int) – The expiration time for the key, in seconds.

    • force_overwrite (bool) – If True, the value will always be computed and stored regardless of whether it exists in the cache already.

    • large_data (bool) – If True, the resulting data will be pickled, gzipped, and (potentially) split up into megabyte-sized chunks. This is useful for very large, computationally intensive hunks of data which we don’t want to store in a database due to the way things are accessed.

    • compress_large_data (bool) – Compresses the data with zlib compression when large_data is True.

    • use_generator (bool, deprecated) – This parameter is no longer used and will be removed in Djblets 3.0.

    • use_encryption (bool, optional) –

      Whether to use encryption when storing or reading data.

      If reading data, and if the data cannot be decrypted with the given key, then the data will be considered to have fallen out of cache.

      This defaults to False, but can be forced on for all cached data by setting settings.DJBLETS_CACHE_FORCE_ENCRYPTION=True.

      New in version 3.0.

    • encryption_key (bytes or str, optional) –

      An explicit AES encryption key to use when passing use_encryption=True.

      This defaults the value in settings.DJBLETS_CACHE_DEFAULT_ENCRYPTION_KEY, if set, or to the default AES encryption key for the server as provided by djblets.secrets.crypto.get_default_aes_encryption_key().

      New in version 3.0.

    Returns:

    The cached data, or the result of lookup_callable if uncached.

    Return type:

    object

    make_cache_key(key, use_encryption=None, encryption_key=None)[source]¶

    Create a cache key guaranteed to avoid conflicts and size limits.

    The cache key will be prefixed by the site’s domain, and will be changed to an SHA256 hash if it’s larger than the maximum key size or contains characters not compatible with the cache backend.

    Changed in version 3.0:

    • Added support for encrypting keys through the use_encryption and encryption_key arguments.

    • Changed the hash format for keys to use SHA256 instead of MD5. This will invalidate all old keys in cache, but reduces chances of collision.

    • Keys will automatically use the hash format if they contain characters unsupported by the cache backend.

    • The return type is now str, to generate keys that are more suitable for modern versions of Django.

    Parameters:
    • key (str) – The base key to generate a cache key from.

    • use_encryption (bool, optional) –

      Whether to generate an encrypted key.

      This will generate a HMAC digest of the key. There will be no identifying information in the resulting key.

      This defaults to False, but can be forced on for all cached data by setting settings.DJBLETS_CACHE_FORCE_ENCRYPTION=True.

      New in version 3.0.

    • encryption_key (bytes, optional) –

      An explicit AES encryption key to use when passing use_encryption=True.

      This defaults the value in settings.DJBLETS_CACHE_DEFAULT_ENCRYPTION_KEY, if set, or to the default AES encryption key for the server as provided by djblets.secrets.crypto.get_default_aes_encryption_key().

      New in version 3.0.

    Returns:

    A cache key suitable for use with the cache backend.

    Return type:

    str

    Keep up with the latest Review Board releases, security updates, and helpful information.

    About
    News
    Demo
    RBCommons Hosting
    Integrations
    Happy Users
    Support Options
    Documentation
    FAQ
    User Manual
    RBTools
    Administration Guide
    Power Pack
    Release Notes
    Downloads
    Review Board
    RBTools
    Djblets
    Power Pack
    Package Store
    PGP Signatures
    Contributing
    Bug Tracker
    Submit Patches
    Development Setup
    Wiki
    Follow Us
    Mailing Lists
    Reddit
    Twitter
    Mastodon
    Facebook
    YouTube

    Copyright © 2006-2025 Beanbag, Inc. All rights reserved.

    Terms of Service — Privacy Policy — AI Ethics Policy — Branding

    On this page

    • [Top]
    • cache_memoize_iter()
    • cache_memoize()
    • make_cache_key()