Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
3854d0f9
Unverified
Commit
3854d0f9
authored
2 years ago
by
Brendan Abolivier
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add a `cached` helper to the module API (#14663)
parent
a4ca7706
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/14663.feature
+1
-0
1 addition, 0 deletions
changelog.d/14663.feature
synapse/module_api/__init__.py
+39
-1
39 additions, 1 deletion
synapse/module_api/__init__.py
with
40 additions
and
1 deletion
changelog.d/14663.feature
0 → 100644
+
1
−
0
View file @
3854d0f9
Add
a
`cached` function to `synapse.module_api` that returns a decorator to cache return values of functions.
This diff is collapsed.
Click to expand it.
synapse/module_api/__init__.py
+
39
−
1
View file @
3854d0f9
...
...
@@ -18,6 +18,7 @@ from typing import (
TYPE_CHECKING
,
Any
,
Callable
,
Collection
,
Dict
,
Generator
,
Iterable
,
...
...
@@ -126,7 +127,7 @@ from synapse.types import (
from
synapse.types.state
import
StateFilter
from
synapse.util
import
Clock
from
synapse.util.async_helpers
import
maybe_awaitable
from
synapse.util.caches.descriptors
import
CachedFunction
,
cached
from
synapse.util.caches.descriptors
import
CachedFunction
,
cached
as
_cached
from
synapse.util.frozenutils
import
freeze
if
TYPE_CHECKING
:
...
...
@@ -136,6 +137,7 @@ if TYPE_CHECKING:
T
=
TypeVar
(
"
T
"
)
P
=
ParamSpec
(
"
P
"
)
F
=
TypeVar
(
"
F
"
,
bound
=
Callable
[...,
Any
])
"""
This package defines the
'
stable
'
API which can be used by extension modules which
...
...
@@ -185,6 +187,42 @@ class UserIpAndAgent:
last_seen
:
int
def
cached
(
*
,
max_entries
:
int
=
1000
,
num_args
:
Optional
[
int
]
=
None
,
uncached_args
:
Optional
[
Collection
[
str
]]
=
None
,
)
->
Callable
[[
F
],
CachedFunction
[
F
]]:
"""
Returns a decorator that applies a memoizing cache around the function. This
decorator behaves similarly to functools.lru_cache.
Example:
@cached()
def foo(
'
a
'
,
'
b
'
):
...
Added in Synapse v1.74.0.
Args:
max_entries: The maximum number of entries in the cache. If the cache is full
and a new entry is added, the least recently accessed entry will be evicted
from the cache.
num_args: The number of positional arguments (excluding `self`) to use as cache
keys. Defaults to all named args of the function.
uncached_args: A list of argument names to not use as the cache key. (`self` is
always ignored.) Cannot be used with num_args.
Returns:
A decorator that applies a memoizing cache around the function.
"""
return
_cached
(
max_entries
=
max_entries
,
num_args
=
num_args
,
uncached_args
=
uncached_args
,
)
class
ModuleApi
:
"""
A proxy object that gets passed to various plugin modules so they
can register new users etc if necessary.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment