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

Commit

Permalink
Merge pull request #665 from matrix-org/erikj/dont_cache_events
Browse files Browse the repository at this point in the history
Only cache events in the event cache
  • Loading branch information
erikjohnston committed Mar 23, 2016
2 parents 0e7363e + d87a846 commit e14baa7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions synapse/storage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _persist_event_txn(self, txn, event, context,
# We purposefully do this first since if we include a `current_state`
# key, we *want* to update the `current_state_events` table
if current_state:
txn.call_after(self.get_current_state_for_key.invalidate_all)
txn.call_after(self._get_current_state_for_key.invalidate_all)
txn.call_after(self.get_rooms_for_user.invalidate_all)
txn.call_after(self.get_users_in_room.invalidate, (event.room_id,))
txn.call_after(self.get_joined_hosts_for_room.invalidate, (event.room_id,))
Expand Down Expand Up @@ -466,7 +466,7 @@ def event_dict(event):
for event, _ in state_events_and_contexts:
if not context.rejected:
txn.call_after(
self.get_current_state_for_key.invalidate,
self._get_current_state_for_key.invalidate,
(event.room_id, event.type, event.state_key,)
)

Expand Down
16 changes: 9 additions & 7 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
# limitations under the License.

from ._base import SQLBaseStore
from synapse.util.caches.descriptors import (
cached, cachedInlineCallbacks, cachedList
)
from synapse.util.caches.descriptors import cached, cachedList

from twisted.internet import defer

Expand Down Expand Up @@ -155,8 +153,14 @@ def f(txn):
events = yield self._get_events(event_ids, get_prev_content=False)
defer.returnValue(events)

@cachedInlineCallbacks(num_args=3)
@defer.inlineCallbacks
def get_current_state_for_key(self, room_id, event_type, state_key):
event_ids = yield self._get_current_state_for_key(room_id, event_type, state_key)
events = yield self._get_events(event_ids, get_prev_content=False)
defer.returnValue(events)

@cached(num_args=3)
def _get_current_state_for_key(self, room_id, event_type, state_key):
def f(txn):
sql = (
"SELECT event_id FROM current_state_events"
Expand All @@ -167,9 +171,7 @@ def f(txn):
txn.execute(sql, args)
results = txn.fetchall()
return [r[0] for r in results]
event_ids = yield self.runInteraction("get_current_state_for_key", f)
events = yield self._get_events(event_ids, get_prev_content=False)
defer.returnValue(events)
return self.runInteraction("get_current_state_for_key", f)

def _get_state_groups_from_groups(self, groups, types):
"""Returns dictionary state_group -> (dict of (type, state_key) -> event id)
Expand Down
33 changes: 19 additions & 14 deletions synapse/storage/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from twisted.internet import defer

from ._base import SQLBaseStore
from synapse.util.caches.descriptors import cachedInlineCallbacks
from synapse.util.caches.descriptors import cached
from synapse.api.constants import EventTypes
from synapse.types import RoomStreamToken
from synapse.util.logcontext import preserve_fn
Expand Down Expand Up @@ -465,9 +465,25 @@ def f(txn):

defer.returnValue((events, token))

@cachedInlineCallbacks(num_args=4)
@defer.inlineCallbacks
def get_recent_events_for_room(self, room_id, limit, end_token, from_token=None):
rows, token = yield self.get_recent_event_ids_for_room(
room_id, limit, end_token, from_token
)

logger.debug("stream before")
events = yield self._get_events(
[r["event_id"] for r in rows],
get_prev_content=True
)
logger.debug("stream after")

self._set_before_and_after(events, rows)

defer.returnValue((events, token))

@cached(num_args=4)
def get_recent_event_ids_for_room(self, room_id, limit, end_token, from_token=None):
end_token = RoomStreamToken.parse_stream_token(end_token)

if from_token is None:
Expand Down Expand Up @@ -517,21 +533,10 @@ def get_recent_events_for_room_txn(txn):

return rows, token

rows, token = yield self.runInteraction(
return self.runInteraction(
"get_recent_events_for_room", get_recent_events_for_room_txn
)

logger.debug("stream before")
events = yield self._get_events(
[r["event_id"] for r in rows],
get_prev_content=True
)
logger.debug("stream after")

self._set_before_and_after(events, rows)

defer.returnValue((events, token))

@defer.inlineCallbacks
def get_room_events_max_id(self, direction='f'):
token = yield self._stream_id_gen.get_max_token()
Expand Down

0 comments on commit e14baa7

Please sign in to comment.