Add caches for things requested by the pushers

This commit is contained in:
Mark Haines 2015-05-21 15:14:26 +01:00
parent d61ce3f670
commit 53447e9cd3
7 changed files with 45 additions and 34 deletions

View File

@ -531,9 +531,7 @@ class RoomListHandler(BaseHandler):
chunk = yield self.store.get_rooms(is_public=True) chunk = yield self.store.get_rooms(is_public=True)
results = yield defer.gatherResults( results = yield defer.gatherResults(
[ [
self.store.get_users_in_room( self.store.get_users_in_room(room["room_id"])
room_id=room["room_id"],
)
for room in chunk for room in chunk
], ],
consumeErrors=True, consumeErrors=True,

View File

@ -84,25 +84,20 @@ class Pusher(object):
rules = baserules.list_with_base_rules(rawrules, user) rules = baserules.list_with_base_rules(rawrules, user)
room_id = ev['room_id']
# get *our* member event for display name matching # get *our* member event for display name matching
member_events_for_room = yield self.store.get_current_state(
room_id=ev['room_id'],
event_type='m.room.member',
state_key=None
)
my_display_name = None my_display_name = None
room_member_count = 0 our_member_event = yield self.store.get_current_state(
for mev in member_events_for_room: room_id=room_id,
if mev.content['membership'] != 'join': event_type='m.room.member',
continue state_key=self.user_name,
)
if our_member_event:
my_display_name = our_member_event[0].content.get("displayname")
# This loop does two things: room_members = yield self.store.get_users_in_room(room_id)
# 1) Find our current display name room_member_count = len(room_members)
if mev.state_key == self.user_name and 'displayname' in mev.content:
my_display_name = mev.content['displayname']
# and 2) Get the number of people in that room
room_member_count += 1
for r in rules: for r in rules:
if r['rule_id'] in enabled_map: if r['rule_id'] in enabled_map:

View File

@ -180,6 +180,7 @@ def cached(max_entries=1000, num_args=1, lru=False):
defer.returnValue(ret) defer.returnValue(ret)
wrapped.invalidate = cache.invalidate wrapped.invalidate = cache.invalidate
wrapped.invalidate_all = cache.invalidate_all
wrapped.prefill = cache.prefill wrapped.prefill = cache.prefill
return wrapped return wrapped

View File

@ -108,6 +108,10 @@ class EventsStore(SQLBaseStore):
# key, we *want* to update the `current_state_events` table # key, we *want* to update the `current_state_events` table
if current_state: 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)
txn.call_after(self.get_room_name_and_aliases, event.room_id)
self._simple_delete_txn( self._simple_delete_txn(
txn, txn,
@ -116,13 +120,6 @@ class EventsStore(SQLBaseStore):
) )
for s in current_state: for s in current_state:
if s.type == EventTypes.Member:
txn.call_after(
self.get_rooms_for_user.invalidate, s.state_key
)
txn.call_after(
self.get_joined_hosts_for_room.invalidate, s.room_id
)
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
"current_state_events", "current_state_events",
@ -341,6 +338,14 @@ class EventsStore(SQLBaseStore):
self.get_current_state_for_key.invalidate, self.get_current_state_for_key.invalidate,
event.room_id, event.type, event.state_key event.room_id, event.type, event.state_key
) )
if (event.type == EventTypes.Name
or event.type == EventTypes.Aliases):
txn.call_after(
self.get_room_name_and_aliases.invalidate,
event.room_id
)
self._simple_upsert_txn( self._simple_upsert_txn(
txn, txn,
"current_state_events", "current_state_events",

View File

@ -13,9 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections from ._base import SQLBaseStore, cached
from ._base import SQLBaseStore, Table
from twisted.internet import defer from twisted.internet import defer
import logging import logging
@ -41,6 +39,7 @@ class PushRuleStore(SQLBaseStore):
defer.returnValue(rows) defer.returnValue(rows)
@cached()
@defer.inlineCallbacks @defer.inlineCallbacks
def get_push_rules_enabled_for_user(self, user_name): def get_push_rules_enabled_for_user(self, user_name):
results = yield self._simple_select_list( results = yield self._simple_select_list(
@ -151,6 +150,10 @@ class PushRuleStore(SQLBaseStore):
txn.execute(sql, (user_name, priority_class, new_rule_priority)) txn.execute(sql, (user_name, priority_class, new_rule_priority))
txn.call_after(
self.get_push_rules_enabled_for_user.invalidate, user_name
)
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
table=PushRuleTable.table_name, table=PushRuleTable.table_name,
@ -179,6 +182,10 @@ class PushRuleStore(SQLBaseStore):
new_rule['priority_class'] = priority_class new_rule['priority_class'] = priority_class
new_rule['priority'] = new_prio new_rule['priority'] = new_prio
txn.call_after(
self.get_push_rules_enabled_for_user.invalidate, user_name
)
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
table=PushRuleTable.table_name, table=PushRuleTable.table_name,
@ -201,6 +208,7 @@ class PushRuleStore(SQLBaseStore):
{'user_name': user_name, 'rule_id': rule_id}, {'user_name': user_name, 'rule_id': rule_id},
desc="delete_push_rule", desc="delete_push_rule",
) )
self.get_push_rules_enabled_for_user.invalidate(user_name)
@defer.inlineCallbacks @defer.inlineCallbacks
def set_push_rule_enabled(self, user_name, rule_id, enabled): def set_push_rule_enabled(self, user_name, rule_id, enabled):
@ -210,6 +218,9 @@ class PushRuleStore(SQLBaseStore):
{'enabled': 1 if enabled else 0}, {'enabled': 1 if enabled else 0},
desc="set_push_rule_enabled", desc="set_push_rule_enabled",
) )
txn.call_after(
self.get_push_rules_enabled_for_user.invalidate, user_name
)
class RuleNotFoundException(Exception): class RuleNotFoundException(Exception):
@ -220,7 +231,7 @@ class InconsistentRuleException(Exception):
pass pass
class PushRuleTable(Table): class PushRuleTable(object):
table_name = "push_rules" table_name = "push_rules"
fields = [ fields = [
@ -233,10 +244,8 @@ class PushRuleTable(Table):
"actions", "actions",
] ]
EntryType = collections.namedtuple("PushRuleEntry", fields)
class PushRuleEnableTable(object):
class PushRuleEnableTable(Table):
table_name = "push_rules_enable" table_name = "push_rules_enable"
fields = [ fields = [

View File

@ -17,7 +17,7 @@ from twisted.internet import defer
from synapse.api.errors import StoreError from synapse.api.errors import StoreError
from ._base import SQLBaseStore from ._base import SQLBaseStore, cached
import collections import collections
import logging import logging
@ -186,6 +186,7 @@ class RoomStore(SQLBaseStore):
} }
) )
@cached()
@defer.inlineCallbacks @defer.inlineCallbacks
def get_room_name_and_aliases(self, room_id): def get_room_name_and_aliases(self, room_id):
def f(txn): def f(txn):

View File

@ -66,6 +66,7 @@ class RoomMemberStore(SQLBaseStore):
txn.call_after(self.get_rooms_for_user.invalidate, target_user_id) txn.call_after(self.get_rooms_for_user.invalidate, target_user_id)
txn.call_after(self.get_joined_hosts_for_room.invalidate, event.room_id) txn.call_after(self.get_joined_hosts_for_room.invalidate, event.room_id)
txn.call_after(self.get_users_in_room.invalidate, event.room_id)
def get_room_member(self, user_id, room_id): def get_room_member(self, user_id, room_id):
"""Retrieve the current state of a room member. """Retrieve the current state of a room member.
@ -87,6 +88,7 @@ class RoomMemberStore(SQLBaseStore):
return self.runInteraction("get_room_member", f) return self.runInteraction("get_room_member", f)
@cached()
def get_users_in_room(self, room_id): def get_users_in_room(self, room_id):
def f(txn): def f(txn):