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
7eea3e35
Commit
7eea3e35
authored
9 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Make @cached cache deferreds rather than the deferreds' values
parent
39e21ea5
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
synapse/storage/_base.py
+8
-13
8 additions, 13 deletions
synapse/storage/_base.py
synapse/util/async.py
+7
-2
7 additions, 2 deletions
synapse/util/async.py
tests/storage/test__base.py
+7
-4
7 additions, 4 deletions
tests/storage/test__base.py
with
22 additions
and
19 deletions
synapse/storage/_base.py
+
8
−
13
View file @
7eea3e35
...
...
@@ -15,6 +15,7 @@
import
logging
from
synapse.api.errors
import
StoreError
from
synapse.util.async
import
ObservableDeferred
from
synapse.util.logutils
import
log_function
from
synapse.util.logcontext
import
preserve_context_over_fn
,
LoggingContext
from
synapse.util.lrucache
import
LruCache
...
...
@@ -173,33 +174,27 @@ class CacheDescriptor(object):
)
@functools.wraps
(
self
.
orig
)
@defer.inlineCallbacks
def
wrapped
(
*
args
,
**
kwargs
):
arg_dict
=
inspect
.
getcallargs
(
self
.
orig
,
obj
,
*
args
,
**
kwargs
)
keyargs
=
[
arg_dict
[
arg_nm
]
for
arg_nm
in
self
.
arg_names
]
try
:
cached_result
=
cache
.
get
(
*
keyargs
)
if
DEBUG_CACHES
:
actual_result
=
yield
self
.
function_to_call
(
obj
,
*
args
,
**
kwargs
)
if
actual_result
!=
cached_result
:
logger
.
error
(
"
Stale cache entry %s%r: cached: %r, actual %r
"
,
self
.
orig
.
__name__
,
keyargs
,
cached_result
,
actual_result
,
)
raise
ValueError
(
"
Stale cache entry
"
)
defer
.
returnValue
(
cached_result
)
return
cached_result
.
observe
()
except
KeyError
:
# Get the sequence number of the cache before reading from the
# database so that we can tell if the cache is invalidated
# while the SELECT is executing (SYN-369)
sequence
=
cache
.
sequence
ret
=
yield
self
.
function_to_call
(
obj
,
*
args
,
**
kwargs
)
ret
=
defer
.
maybeDeferred
(
self
.
function_to_call
,
obj
,
*
args
,
**
kwargs
)
ret
=
ObservableDeferred
(
ret
,
consumeErrors
=
False
)
cache
.
update
(
sequence
,
*
(
keyargs
+
[
ret
]))
defer
.
returnValue
(
ret
)
return
ret
.
observe
(
)
wrapped
.
invalidate
=
cache
.
invalidate
wrapped
.
invalidate_all
=
cache
.
invalidate_all
...
...
This diff is collapsed.
Click to expand it.
synapse/util/async.py
+
7
−
2
View file @
7eea3e35
...
...
@@ -51,7 +51,7 @@ class ObservableDeferred(object):
object
.
__setattr__
(
self
,
"
_observers
"
,
set
())
def
callback
(
r
):
self
.
_result
=
(
True
,
r
)
object
.
__setattr__
(
self
,
"
_result
"
,
(
True
,
r
)
)
while
self
.
_observers
:
try
:
self
.
_observers
.
pop
().
callback
(
r
)
...
...
@@ -60,7 +60,7 @@ class ObservableDeferred(object):
return
r
def
errback
(
f
):
self
.
_result
=
(
False
,
f
)
object
.
__setattr__
(
self
,
"
_result
"
,
(
False
,
f
)
)
while
self
.
_observers
:
try
:
self
.
_observers
.
pop
().
errback
(
f
)
...
...
@@ -97,3 +97,8 @@ class ObservableDeferred(object):
def
__setattr__
(
self
,
name
,
value
):
setattr
(
self
.
_deferred
,
name
,
value
)
def
__repr__
(
self
):
return
"
<ObservableDeferred object at %s, result=%r, _deferred=%r>
"
%
(
id
(
self
),
self
.
_result
,
self
.
_deferred
,
)
This diff is collapsed.
Click to expand it.
tests/storage/test__base.py
+
7
−
4
View file @
7eea3e35
...
...
@@ -17,6 +17,8 @@
from
tests
import
unittest
from
twisted.internet
import
defer
from
synapse.util.async
import
ObservableDeferred
from
synapse.storage._base
import
Cache
,
cached
...
...
@@ -178,19 +180,20 @@ class CacheDecoratorTestCase(unittest.TestCase):
self
.
assertTrue
(
callcount
[
0
]
>=
14
,
msg
=
"
Expected callcount >= 14, got %d
"
%
(
callcount
[
0
]))
@defer.inlineCallbacks
def
test_prefill
(
self
):
callcount
=
[
0
]
d
=
defer
.
succeed
(
123
)
class
A
(
object
):
@cached
()
def
func
(
self
,
key
):
callcount
[
0
]
+=
1
return
key
return
d
a
=
A
()
a
.
func
.
prefill
(
"
foo
"
,
123
)
a
.
func
.
prefill
(
"
foo
"
,
ObservableDeferred
(
d
)
)
self
.
assertEquals
(
(
yield
a
.
func
(
"
foo
"
)
),
123
)
self
.
assertEquals
(
a
.
func
(
"
foo
"
)
.
result
,
d
.
result
)
self
.
assertEquals
(
callcount
[
0
],
0
)
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