Misc Utilities

Misc utilities in Authlib to make things simple.

Flask Cache

class authlib.flask.cache.Cache(app, config_prefix='AUTHLIB', **kwargs)

Cache module based on werkzeug.contrib.cache. This is a mixed version of NullCache, SimpleCache, FileSystemCache, MemcachedCache, and RedisCache.

Parameters:
  • app – Flask app instance.
  • config_prefix – Define a prefix for Flask app config.
  • kwargs – Extra parameters.

You need to configure a type of the cache, and its related configurations. The default config_prefix is AUTHLIB, so it requires a config of:

AUTHLIB_CACHE_TYPE = 'simple'

If config_prefix is something else, like EXAMPLE, it would be:

EXAMPLE_CACHE_TYPE = 'simple'

The available cache types are:

  • null: It will not cache anything. No configuration.

  • simple: It caches things in memory. The only configuration is threshold:

    AUTHLIB_CACHE_THRESHOLD = 500
    
  • memcache: It caches things in Memcache. Available configurations:

    AUTHLIB_CACHE_MEMCACHED_SERVERS = []
    AUTHLIB_CACHE_KEY_PREFIX = None
    
  • redis: It caches things in Redis. Available configurations:

    AUTHLIB_CACHE_REDIS_HOST = 'localhost'
    AUTHLIB_CACHE_REDIS_PORT = 6379
    AUTHLIB_CACHE_REDIS_PASSWORD = None
    AUTHLIB_CACHE_REDIS_DB = 0
    AUTHLIB_CACHE_KEY_PREFIX = None
    
  • filesystem: It caches things in local filesystem. Available configurations:

    AUTHLIB_CACHE_DIR = ''  # required
    AUTHLIB_CACHE_THRESHOLD = 500
    
add(key, value, timeout=None)

Works like set() but does not overwrite the values of already existing keys.

Parameters:
  • key – the key to set
  • value – the value for the key
  • timeout – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 idicates that the cache never expires.
Returns:

Same as set(), but also False for already existing keys.

clear()

Clears the cache. Keep in mind that not all caches support completely clearing the cache.

Returns:Whether the cache has been cleared.
dec(key, delta=1)

Decrements the value of a key by delta. If the key does not yet exist it is initialized with -delta.

For supporting caches this is an atomic operation.

Parameters:
  • key – the key to increment.
  • delta – the delta to subtract.
Returns:

The new value or None for backend errors.

delete(key)

Delete key from the cache.

Parameters:key – the key to delete.
Returns:Whether the key existed and has been deleted.
delete_many(*keys)

Deletes multiple keys at once.

Parameters:keys – The function accepts multiple keys as positional arguments.
Returns:Whether all given keys have been deleted.
Return type:boolean
get(key)

Look up key in the cache and return the value for it.

Parameters:key – the key to be looked up.
Returns:The value if it exists and is readable, else None.
get_dict(*keys)

Like get_many() but return a dict:

d = cache.get_dict("foo", "bar")
foo = d["foo"]
bar = d["bar"]
Parameters:keys – The function accepts multiple keys as positional arguments.
get_many(*keys)

Returns a list of values for the given keys. For each key a item in the list is created:

foo, bar = cache.get_many("foo", "bar")

Has the same error handling as get().

Parameters:keys – The function accepts multiple keys as positional arguments.
has(key)

Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend.

This method is optional and may not be implemented on all caches.

Parameters:key – the key to check
inc(key, delta=1)

Increments the value of a key by delta. If the key does not yet exist it is initialized with delta.

For supporting caches this is an atomic operation.

Parameters:
  • key – the key to increment.
  • delta – the delta to add.
Returns:

The new value or None for backend errors.

set(key, value, timeout=None)

Add a new key/value to the cache (overwrites value, if key already exists in the cache).

Parameters:
  • key – the key to set
  • value – the value for the key
  • timeout – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 idicates that the cache never expires.
Returns:

True if key has been updated, False for backend errors. Pickling errors, however, will raise a subclass of pickle.PickleError.

set_many(mapping, timeout=None)

Sets multiple keys and values from a mapping.

Parameters:
  • mapping – a mapping with the keys/values to set.
  • timeout – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 idicates that the cache never expires.
Returns:

Whether all given keys have been set.