Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
806f380a
Commit
806f380a
authored
10 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Make LruCache thread safe, as its used for event cache
parent
a5c72780
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/util/lrucache.py
+20
-0
20 additions, 0 deletions
synapse/util/lrucache.py
with
20 additions
and
0 deletions
synapse/util/lrucache.py
+
20
−
0
View file @
806f380a
...
...
@@ -14,6 +14,10 @@
# limitations under the License.
from
functools
import
wraps
import
threading
class
LruCache
(
object
):
"""
Least-recently-used cache.
"""
# TODO(mjark) Add mutex for linked list for thread safety.
...
...
@@ -24,6 +28,16 @@ class LruCache(object):
PREV
,
NEXT
,
KEY
,
VALUE
=
0
,
1
,
2
,
3
lock
=
threading
.
Lock
()
def
synchronized
(
f
):
@wraps
(
f
)
def
inner
(
*
args
,
**
kwargs
):
with
lock
:
return
f
(
*
args
,
**
kwargs
)
return
inner
def
add_node
(
key
,
value
):
prev_node
=
list_root
next_node
=
prev_node
[
NEXT
]
...
...
@@ -51,6 +65,7 @@ class LruCache(object):
next_node
[
PREV
]
=
prev_node
cache
.
pop
(
node
[
KEY
],
None
)
@synchronized
def
cache_get
(
key
,
default
=
None
):
node
=
cache
.
get
(
key
,
None
)
if
node
is
not
None
:
...
...
@@ -59,6 +74,7 @@ class LruCache(object):
else
:
return
default
@synchronized
def
cache_set
(
key
,
value
):
node
=
cache
.
get
(
key
,
None
)
if
node
is
not
None
:
...
...
@@ -69,6 +85,7 @@ class LruCache(object):
if
len
(
cache
)
>
max_size
:
delete_node
(
list_root
[
PREV
])
@synchronized
def
cache_set_default
(
key
,
value
):
node
=
cache
.
get
(
key
,
None
)
if
node
is
not
None
:
...
...
@@ -79,6 +96,7 @@ class LruCache(object):
delete_node
(
list_root
[
PREV
])
return
value
@synchronized
def
cache_pop
(
key
,
default
=
None
):
node
=
cache
.
get
(
key
,
None
)
if
node
:
...
...
@@ -87,9 +105,11 @@ class LruCache(object):
else
:
return
default
@synchronized
def
cache_len
():
return
len
(
cache
)
@synchronized
def
cache_contains
(
key
):
return
key
in
cache
...
...
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