dogpile.cache

repository·main·Indexed 43 days ago

https://github.com/sqlalchemy/dogpile.cache

A high-performance caching framework and API designed to prevent cache stampedes using a specialized locking mechanism called the dogpile lock. It features region-based configuration, a decorator API, customizable key generation, and support for multiple backends including Memcached, Redis, anydbm, and Python dictionaries.

Tokens
566
Snippets
0
Records
3
Agent score
5%

What's inside dogpile.cache

  1. Key features of dogpile.cache

    main

    The dogpile.cache library provides several advanced caching capabilities:

    • Region-based Configuration: Encourages defining "regions" upfront. Each region encapsulates a storage backend, specific configuration options, and a default expiration time.
    • Standard & Decorator APIs: Provides a standard get/set/delete API as well as a function decorator API for easy integration.
    • Customizable Key Generation:
      • Key Generators: Pluggable components used with the decorator API to determine how function arguments map to cache keys.
      • Key Manglers: Pluggable components for transforming keys (e.g., encoding or SHA-1 hashing) per region.
    • Distributed Locking: Backends can implement their own distributed locks tailored to their storage engine (e.g., Memcached backends use Memcached itself for coordination, while anydbm uses lockfiles). If a backend does not provide a distributed lock, dogpile defaults to a standard thread mutex.
    • Extensible Backend System: New backends can be implemented by providing get, set, and delete methods. They can be registered directly or via setuptools entry points.
  2. Understand the dogpile and dogpile.cache subsystems

    main

    Dogpile is composed of two distinct but integrated subsystems:

    1. dogpile: Provides the core "dogpile lock" control structure. This mechanism ensures that only one thread is selected as the "creator" of a resource. While the creator is generating the new resource, other threads can continue to use the previous version of the resource. If no previous version exists, other threads will block until the new object is available.

    2. dogpile.cache: A caching API that provides a generic interface for various caching backends. It integrates these backends with the dogpile locking mechanism to prevent cache stampedes (the "dogpile effect").

    dogpile.cache is designed as a modern, more efficient replacement for the Beaker caching system.

  3. Available dogpile.cache backends

    main

    The library includes several built-in backends:

    • Memcached: Three different implementations (python-memcached, pylibmc, and bmemcached).
    • Redis: A backend based on Redis.
    • anydbm: A backend based on Python's anydbm module.
    • Dictionary: A plain Python dictionary backend (useful for in-memory testing or simple use cases).