From a2ad4af79a1435648c96b99308edeb04fd112da1 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 30 Mar 2018 22:29:30 +0200 Subject: [PATCH] =?UTF-8?q?Use=20slixmpp=E2=80=99s=20new=20cache=20module?= =?UTF-8?q?=20for=20avatars=20too.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poezio/core/core.py | 4 +++- poezio/core/handlers.py | 42 ++++++++++++----------------------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/poezio/core/core.py b/poezio/core/core.py index 8089408c..7c6ad886 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -18,6 +18,7 @@ import sys import time from slixmpp.xmlstream.handler import Callback +from slixmpp.util import FileSystemPerJidCache from poezio import connection from poezio import decorators @@ -30,7 +31,7 @@ from poezio import windows from poezio.bookmarks import BookmarkList from poezio.common import safeJID -from poezio.config import config, firstrun +from poezio.config import config, firstrun, CACHE_DIR from poezio.contact import Contact, Resource from poezio.daemon import Executor from poezio.fifo import Fifo @@ -76,6 +77,7 @@ class Core(object): self.bookmarks = BookmarkList() self.debug = False self.remote_fifo = None + self.avatar_cache = FileSystemPerJidCache(CACHE_DIR, 'avatars', binary=True) # a unique buffer used to store global information # that are displayed in almost all tabs, in an # information window. diff --git a/poezio/core/handlers.py b/poezio/core/handlers.py index 8c799b58..c73fad1f 100644 --- a/poezio/core/handlers.py +++ b/poezio/core/handlers.py @@ -395,19 +395,15 @@ class HandlerCore: except Exception: log.debug('Failed getting metadata from 0084:', exc_info=True) return - cache_dir = path.join(CACHE_DIR, 'avatars', jid) for info in metadata: avatar_hash = info['id'] # First check whether we have it in cache. - cached_path = path.join(cache_dir, avatar_hash) - try: - with open(cached_path, 'rb') as avatar_file: - contact.avatar = avatar_file.read() - log.debug('Using cached avatar') + cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash) + if cached_avatar: + contact.avatar = cached_avatar + log.debug('Using cached avatar for %s', jid) return - except OSError: - pass # If we didn’t have any, query the data instead. if not info['url']: @@ -429,16 +425,11 @@ class HandlerCore: log.debug('Received %s avatar: %s', jid, info['type']) # Now we save the data on the file system to not have to request it again. - try: - makedirs(cache_dir, exist_ok=True) - with open(cached_path, 'wb') as avatar_file: - avatar_file.write(contact.avatar) - except OSError: + if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar): log.debug( - 'Failed writing %s avatar to cache:', + 'Failed writing %s’s avatar to cache:', jid, exc_info=True) - pass return @asyncio.coroutine @@ -451,15 +442,11 @@ class HandlerCore: log.debug('Received vCard avatar update from %s: %s', jid, avatar_hash) # First check whether we have it in cache. - cache_dir = path.join(CACHE_DIR, 'avatars', jid) - cached_path = path.join(cache_dir, avatar_hash) - try: - with open(cached_path, 'rb') as avatar_file: - contact.avatar = avatar_file.read() - log.debug('Using cached avatar') + cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash) + if cached_avatar: + contact.avatar = cached_avatar + log.debug('Using cached avatar for %s', jid) return - except OSError: - pass # If we didn’t have any, query the vCard instead. try: @@ -476,13 +463,8 @@ class HandlerCore: log.debug('Received %s avatar: %s', jid, avatar['TYPE']) # Now we save the data on the file system to not have to request it again. - try: - makedirs(cache_dir, exist_ok=True) - with open(cached_path, 'wb') as avatar_file: - avatar_file.write(contact.avatar) - except OSError: - log.debug('Failed writing %s avatar to cache:', jid, exc_info=True) - pass + if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar): + log.debug('Failed writing %s’s avatar to cache:', jid, exc_info=True) def on_nick_received(self, message): """