store: Split out the media cache loading logic from the load media method

This commit is contained in:
Damir Jelić 2021-05-17 10:36:52 +02:00
parent 90cdc55451
commit 2b359c22d2
3 changed files with 25 additions and 23 deletions

View File

@ -121,7 +121,7 @@ class ProxyDaemon:
self.hostname = self.homeserver.hostname
self.store = PanStore(self.data_dir)
accounts = self.store.load_users(self.name)
self.media_info = self.store.load_media(self.name)
self.media_info = self.store.load_media_cache(self.name)
self.upload_info = self.store.load_upload(self.name)
for user_id, device_id in accounts:

View File

@ -254,32 +254,34 @@ class PanStore:
hashes=media.hashes,
).on_conflict_ignore().execute()
@use_database
def load_media_cache(self, server):
server, _ = Servers.get_or_create(name=server)
media_cache = LRUCache(maxsize=MAX_LOADED_MEDIA)
for i, m in enumerate(server.media):
if i > MAX_LOADED_MEDIA:
break
media = MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes)
media_cache[(m.mxc_server, m.mxc_path)] = media
return media_cache
@use_database
def load_media(self, server, mxc_server=None, mxc_path=None):
server, _ = Servers.get_or_create(name=server)
if not mxc_path:
media_cache = LRUCache(maxsize=MAX_LOADED_MEDIA)
m = PanMediaInfo.get_or_none(
PanMediaInfo.server == server,
PanMediaInfo.mxc_server == mxc_server,
PanMediaInfo.mxc_path == mxc_path,
)
for i, m in enumerate(server.media):
if i > MAX_LOADED_MEDIA:
break
if not m:
return None
media = MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes)
media_cache[(m.mxc_server, m.mxc_path)] = media
return media_cache
else:
m = PanMediaInfo.get_or_none(
PanMediaInfo.server == server,
PanMediaInfo.mxc_server == mxc_server,
PanMediaInfo.mxc_path == mxc_path,
)
if not m:
return None
return MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes)
return MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes)
@use_database_atomic
def replace_fetcher_task(self, server, pan_user, old_task, new_task):

View File

@ -153,7 +153,7 @@ class TestClass(object):
def test_media_storage(self, panstore):
server_name = "test"
media_cache = panstore.load_media(server_name)
media_cache = panstore.load_media_cache(server_name)
assert not media_cache
event = self.encrypted_media_event
@ -171,7 +171,7 @@ class TestClass(object):
panstore.save_media(server_name, media)
media_cache = panstore.load_media(server_name)
media_cache = panstore.load_media_cache(server_name)
assert (mxc_server, mxc_path) in media_cache
media_info = media_cache[(mxc_server, mxc_path)]