Cache newly written state attribute ids (#68355)

This commit is contained in:
J. Nick Koston 2022-03-22 21:21:56 -10:00 committed by GitHub
parent df3a163a66
commit 08d6a3d9d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1116,9 +1116,18 @@ class Recorder(threading.Thread):
if dbstate in self.event_session:
self.event_session.expunge(dbstate)
self._pending_expunge = []
self._pending_state_attributes = {}
self.event_session.commit()
# We just committed the state attributes to the database
# and we now know the attributes_ids. We can save
# a many selects for matching attributes by loading them
# into the LRU cache now.
for state_attr in self._pending_state_attributes.values():
self._state_attributes_ids[
state_attr.shared_attrs
] = state_attr.attributes_id
self._pending_state_attributes = {}
# Expire is an expensive operation (frequently more expensive
# than the flush and commit itself) so we only
# do it after EXPIRE_AFTER_COMMITS commits