Skip to content
Back to top
View this page
Edit this page

# Cache

class streamlink.cache.Cache(filename, key_prefix='', disabled=False)

  Bases: `object`

  Caches Python values as JSON and prunes expired entries.

  Parameters:

    - filename (str | Path) -- A file name or `Path` object, relative to the cache directory

    - key_prefix (str) -- Optional prefix for each key to be retrieved from or stored in the cache

    - disabled (bool)

  get(key, default=None)

    Attempt to retrieve the given key from the cache.

    Prunes the cache of all expired key-value pairs before retrieving the key's value.

    Parameters:

      - key (str) -- A specific key name

      - default (Any | None) -- An optional default value if no key was stored, or if it has expired

    Returns:

      The retrieved value or optional default value

    Return type:

      Any

  get_all()

    Retrieve all cached key-value pairs.

    Prunes the cache of all expired key-value pairs first.

    Returns:

      A dictionary of all cached key-value pairs.

    Return type:

      dict[str, Any]

  set(key, value, expires=604800, expires_at=None)

    Store the given value using the key name and expiration time.

    Prunes the cache of all expired key-value pairs before setting the new key-value pair.

    Parameters:

      - key (str) -- A specific key name

      - value (Any) -- Any kind of value that can be JSON-serialized

      - expires (float) -- Expiration time in seconds, with the default being one week

      - expires_at (datetime | None) -- Optional expiration date, which overrides the expiration time

    Return type:

      None