From 609ff072146b85e9ef5152225262f93e5e7b0ffc Mon Sep 17 00:00:00 2001 From: j Date: Sat, 23 Jan 2016 17:47:33 +0530 Subject: [PATCH] dont use like query to remove resize cache --- oml/item/icons.py | 78 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/oml/item/icons.py b/oml/item/icons.py index e663521..99c3a6a 100644 --- a/oml/item/icons.py +++ b/oml/item/icons.py @@ -65,31 +65,60 @@ class Icons(dict): def __setitem__(self, id, data): sql = 'INSERT OR REPLACE INTO icon values (?, ?)' - conn = self.connect() - c = conn.cursor() - data = sqlite3.Binary(data) - c.execute(sql, (id, data)) - conn.commit() - c.close() - conn.close() + try: + conn = self.connect() + c = conn.cursor() + data = sqlite3.Binary(data) + c.execute(sql, (id, data)) + conn.commit() + c.close() + conn.close() + except: + logger.debug('failed to insert icon %s', id) def __delitem__(self, id): sql = 'DELETE FROM icon WHERE id = ?' - conn = self.connect() - c = conn.cursor() - c.execute(sql, (id, )) - conn.commit() - c.close() - conn.close() + try: + conn = self.connect() + c = conn.cursor() + c.execute(sql, (id, )) + conn.commit() + c.close() + conn.close() + except: + logger.debug('failed to delete icon %s', id) def clear(self, prefix): - sql = 'DELETE FROM icon WHERE id LIKE ?' - conn = self.connect() - c = conn.cursor() - c.execute(sql, (prefix + '%', )) - conn.commit() - c.close() - conn.close() + try: + conn = self.connect() + c = conn.cursor() + sql = 'DELETE FROM icon WHERE id = ?' + for size in (64, 128, 256, 512, 1024): + id = '%s%s' % (prefix, size) + c.execute(sql, (id, )) + conn.commit() + c.close() + conn.close() + except: + logger.debug('failed to clear icon %s', id) + + def vacuum(self, ids): + conn = self.connect() + c = conn.cursor() + sql = 'SELECT id from icon' + c.execute(sql) + icons = [row[0] for row in c] + sql = 'DELETE FROM icon WHERE id = ?' + for i in icons: + id = i.split(':')[1] + if id not in ids: + c.execute(sql, (id, )) + conn.commit() + sql = 'VACUUM' + c.execute(sql) + conn.commit() + c.close() + conn.close() icons = Icons(icons_db_path) @@ -121,10 +150,12 @@ def get_icon_sync(id, type_, size): if not data: data = icons.default_cover() if size: - data = icons[skey] = resize_image(data, size=size) + data = resize_image(data, size=size) + icons[skey] = data size = None if size: - data = icons[skey] = resize_image(data, size=size) + data = resize_image(data, size=size) + icons[skey] = data data = bytes(data) or '' return data @@ -161,7 +192,8 @@ def get_icon_app(id, type_, size, callback): data = icons.default_cover() size = None if size: - data = icons[skey] = resize_image(data, size=size) + data = resize_image(data, size=size) + icons[skey] = data data = bytes(data) or '' callback(data)