Token stores¶
When your site logs in to the broker, the broker returns a session token. A token store saves that token. The next connection reads the token from the store and reuses the session, instead of logging in again.
Pick the store with the MOMOTOR_BROKER.TOKEN_STORE_CLASS
setting. The default store saves the token in the database. Most projects
can keep the default.
Pick a token store¶
The package provides four stores:
ModelTokenStoresaves the token in a database model. This is the default. Use it when the token must survive process restarts and be shared between processes.CachedTokenStoresaves the token in Django’s cache framework. Use it when your processes already share a cache, and losing the token when the cache clears is acceptable. A lost token only means the site logs in again.InMemoryTokenStorekeeps the token in memory only. Each process gets its own token. Use it for a single-process application.DummyTokenStoredoes not save the token at all. Every connection logs in again. Use it in tests.
The model and cache stores also keep the token in memory. They read the database or cache only when no token is in memory.
Provided stores¶
- class momotor.django.token_store.model.ModelTokenStore(settings, *, loop=None, executor=None, token_model=<class 'momotor.django.models.MomotorToken'>)¶
Token store that saves tokens in the
MomotorTokendatabase model.This is the default token store. The token is kept in memory first. The database named by
MOMOTOR_BROKER.TOKEN_DATABASE_NAMEis only read when no token is in memory. Use this store when the token must survive process restarts and be shared between processes.- Parameters:
token_model (
type[MomotorToken]) – The model class used to store the token. Defaults tomomotor.django.models.MomotorToken. Pass a compatible model class throughBrokerConnectionto store tokens in your own model.
- async delete()¶
Delete the token
- async get()¶
Get the current token from the store
- executor¶
The executor argument as provided to the constructor
- loop¶
The loop argument as provided to the constructor
- settings¶
The settings argument as provided to the constructor
- class momotor.django.token_store.cache.CachedTokenStore(settings, *, loop=None, executor=None)¶
Token store that saves tokens in a Django cache.
The token is kept in memory first. The cache named by
MOMOTOR_BROKER.TOKEN_CACHE_NAMEis only read when no token is in memory, under the key fromMOMOTOR_BROKER.TOKEN_KEY. Use this store when several processes share a cache and should reuse one broker session token.- async delete()¶
Delete the token
- async get()¶
Get the current token from the store
- executor¶
The executor argument as provided to the constructor
- loop¶
The loop argument as provided to the constructor
- settings¶
The settings argument as provided to the constructor
- class momotor.django.token_store.memory.InMemoryTokenStore(settings, *, loop=None, executor=None)¶
Token store that saves the token in memory only.
The token is lost when the process stops, and each process keeps its own token. Use this store for a single-process application, or as the base class for a store that also keeps the token in an external resource.
- async delete()¶
Delete the token
- async get()¶
Get the current token from the store
- executor¶
The executor argument as provided to the constructor
- loop¶
The loop argument as provided to the constructor
- settings¶
The settings argument as provided to the constructor
- class momotor.django.token_store.dummy.DummyTokenStore(settings, *, loop=None, executor=None, **kwargs)¶
Token store that does not save tokens at all.
Every connection creates a new broker session. Use this store in tests, or when no token may be kept between connections.
- async delete()¶
Delete the token
- async get()¶
Get the current token from the store
- executor¶
The executor argument as provided to the constructor
- loop¶
The loop argument as provided to the constructor
- settings¶
The settings argument as provided to the constructor
Write a custom token store¶
Write a custom store when your project keeps shared state somewhere else,
such as a secrets service. Subclass
BaseTokenStore and implement
the three async methods get, set, and delete:
from momotor.django.token_store.base import BaseTokenStore
class DictTokenStore(BaseTokenStore):
store = {}
async def get(self):
return self.store.get(self.settings['API_KEY'])
async def set(self, token):
self.store[self.settings['API_KEY']] = token
async def delete(self):
self.store.pop(self.settings['API_KEY'], None)
Then point the setting at your class:
MOMOTOR_BROKER = {
'API_KEY': 'your-api-key',
'API_SECRET': 'your-api-secret',
'TOKEN_STORE_CLASS': 'myapp.tokens.DictTokenStore',
}
The constructor receives the MOMOTOR_BROKER dictionary as
settings. Extra keyword arguments passed to
BrokerConnection are passed on to
your store, so your store can accept its own options.
The built-in model and cache stores use Django’s async database and cache
APIs. The loop and executor constructor arguments do not control
those calls. A custom store that must run blocking code can use
_run_in_executor().
- class momotor.django.token_store.base.BaseTokenStore(settings, *, loop=None, executor=None, **kwargs)¶
Abstract base class for token stores. The store is provided with the
MOMOTOR_BROKERsettings and an asyncio loop andexecutor.BrokerConnectionuses this token store. Any additional keyword arguments provided toBrokerConnectionat creation are passed on to the token store to allow for additional parameters to custom token stores.The built-in cache and model stores use Django’s asynchronous cache and database APIs, so
loopandexecutordo not control those calls. Custom stores can run blocking code with_run_in_executor().Changed in version 6.0.0: The built-in cache and model stores no longer run database and cache operations in the supplied executor. They use Django’s execution context instead.
- Parameters:
settings (
dict) – TheMOMOTOR_BROKERdictionary from the settingsloop – The asyncio loop to use the executor with. If None, uses the current active event loop.
executor (
Executor|None) – An executor to run I/O blocking tasks on. If None, uses the current loop’s default executor.kwargs – The additional token store arguments passed to
BrokerConnection
- _run_in_executor(func, *args, **kwargs)¶
Run a function in the executor provided to __init__
- Parameters:
func (
Callable) – function to runargs – arguments to the function
kwargs – keyword arguments to the function
- Returns:
return value of the function
- async delete()¶
Delete the token
- async get()¶
Get the current token from the store
- executor¶
The executor argument as provided to the constructor
- loop¶
The loop argument as provided to the constructor
- settings¶
The settings argument as provided to the constructor