Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 5210268

Browse files
babolivierDavid Robertsonerikjohnstonclokep
authored
Add documentation for caching in a module (#14026)
* Add documentation for caching in a module * Changelog * Formatting * Wrap lines at a length that mdbook is happier with * Typo fix Co-authored-by: Erik Johnston <erik@matrix.org> * Link to recent version of the API In the longer term I'd like to see us generate markdown with Sphinx. * Refer to public `cached` decorator * Mark caching as being added in 1.74 Some of the underlying infrastructure was added in 1.69, but the public-facing `cached` decorator was only added in 1.74. It is the latter that I think we should be advertising. * Update docs/modules/writing_a_module.md Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com> --------- Co-authored-by: David Robertson <davidr@element.io> Co-authored-by: Erik Johnston <erik@matrix.org> Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
1 parent 93f7955 commit 5210268

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

changelog.d/14026.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document how to use caches in a module.

docs/modules/writing_a_module.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,59 @@ the callback name as the argument name and the function as its value. A
8383

8484
Callbacks for each category can be found on their respective page of the
8585
[Synapse documentation website](https://matrix-org.github.io/synapse).
86+
87+
## Caching
88+
89+
_Added in Synapse 1.74.0._
90+
91+
Modules can leverage Synapse's caching tools to manage their own cached functions. This
92+
can be helpful for modules that need to repeatedly request the same data from the database
93+
or a remote service.
94+
95+
Functions that need to be wrapped with a cache need to be decorated with a `@cached()`
96+
decorator (which can be imported from `synapse.module_api`) and registered with the
97+
[`ModuleApi.register_cached_function`](https://github.com/matrix-org/synapse/blob/release-v1.77/synapse/module_api/__init__.py#L888)
98+
API when initialising the module. If the module needs to invalidate an entry in a cache,
99+
it needs to use the [`ModuleApi.invalidate_cache`](https://github.com/matrix-org/synapse/blob/release-v1.77/synapse/module_api/__init__.py#L904)
100+
API, with the function to invalidate the cache of and the key(s) of the entry to
101+
invalidate.
102+
103+
Below is an example of a simple module using a cached function:
104+
105+
```python
106+
from typing import Any
107+
from synapse.module_api import cached, ModuleApi
108+
109+
class MyModule:
110+
def __init__(self, config: Any, api: ModuleApi):
111+
self.api = api
112+
113+
# Register the cached function so Synapse knows how to correctly invalidate
114+
# entries for it.
115+
self.api.register_cached_function(self.get_user_from_id)
116+
117+
@cached()
118+
async def get_department_for_user(self, user_id: str) -> str:
119+
"""A function with a cache."""
120+
# Request a department from an external service.
121+
return await self.http_client.get_json(
122+
"https://int.example.com/users", {"user_id": user_id)
123+
)["department"]
124+
125+
async def do_something_with_users(self) -> None:
126+
"""Calls the cached function and then invalidates an entry in its cache."""
127+
128+
user_id = "@alice:example.com"
129+
130+
# Get the user. Since get_department_for_user is wrapped with a cache,
131+
# the return value for this user_id will be cached.
132+
department = await self.get_department_for_user(user_id)
133+
134+
# Do something with `department`...
135+
136+
# Let's say something has changed with our user, and the entry we have for
137+
# them in the cache is out of date, so we want to invalidate it.
138+
await self.api.invalidate_cache(self.get_department_for_user, (user_id,))
139+
```
140+
141+
See the [`cached` docstring](https://github.com/matrix-org/synapse/blob/release-v1.77/synapse/module_api/__init__.py#L190) for more details.

0 commit comments

Comments
 (0)