From f3bceb468866c62c950b1a10275c01de1b21bc93 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 14 Sep 2020 05:49:24 -0700 Subject: [PATCH] Remove get_cached_flat_dict, add get_cached_tag_export. --- etiquette/helpers.py | 3 +++ etiquette/objects.py | 17 ++++++++--------- etiquette/photodb.py | 30 +++++++++++++++++++----------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 818ea5a..b25f027 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -115,6 +115,9 @@ def comma_space_split(s): return s return re.split(r'[ ,]+', s.strip()) +def dict_to_tuple(d): + return tuple(sorted(d.items())) + def generate_image_thumbnail(filepath, width, height): if not os.path.isfile(filepath): raise FileNotFoundError(filepath) diff --git a/etiquette/objects.py b/etiquette/objects.py index e1c3088..0257ca8 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -1245,7 +1245,7 @@ class Tag(ObjectBase, GroupableMixin): ret = super().add_child(*args, **kwargs) if ret is BAIL: return - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() return ret @decorators.required_feature('tag.edit') @@ -1254,7 +1254,7 @@ class Tag(ObjectBase, GroupableMixin): ret = super().add_children(*args, **kwargs) if ret is BAIL: return - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() return ret @decorators.required_feature('tag.edit') @@ -1269,7 +1269,7 @@ class Tag(ObjectBase, GroupableMixin): self.photodb.log.debug('New synonym %s of %s', synname, self.name) - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() data = { 'name': synname, @@ -1292,7 +1292,7 @@ class Tag(ObjectBase, GroupableMixin): ''' mastertag = self.photodb.get_tag(name=mastertag) - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() # Migrate the old tag's synonyms to the new one # UPDATE is safe for this operation because there is no chance of duplicates. @@ -1345,12 +1345,11 @@ class Tag(ObjectBase, GroupableMixin): @decorators.transaction def delete(self, *, delete_children=False): self.photodb.log.debug('Deleting %s', self) - self.photodb._cached_tag_flat_dict = None super().delete(delete_children=delete_children) self.photodb.sql_delete(table='photo_tag_rel', pairs={'tagid': self.id}) self.photodb.sql_delete(table='tag_synonyms', pairs={'mastername': self.name}) self.photodb.sql_delete(table='tags', pairs={'id': self.id}) - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() self._uncache() @decorators.required_feature('tag.edit') @@ -1386,7 +1385,7 @@ class Tag(ObjectBase, GroupableMixin): ret = super().remove_child(*args, **kwargs) if ret is BAIL: return - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() return ret @decorators.required_feature('tag.edit') @@ -1409,7 +1408,7 @@ class Tag(ObjectBase, GroupableMixin): if syn_exists is None: raise exceptions.NoSuchSynonym(synname) - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() self.photodb.sql_delete(table='tag_synonyms', pairs={'name': synname}) @decorators.required_feature('tag.edit') @@ -1430,7 +1429,7 @@ class Tag(ObjectBase, GroupableMixin): else: raise exceptions.TagExists(new_name) - self.photodb._cached_tag_flat_dict = None + self.photodb.caches['tag_exports'].clear() data = { 'id': self.id, diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 8462616..df0d99f 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -224,6 +224,22 @@ class PDBCacheManagerMixin: thing_cache[thing_id] = thing return thing + def get_cached_tag_export(self, function, **kwargs): + if isinstance(function, str): + function = getattr(tag_export, function) + kwargs['tags'] = tuple(kwargs['tags']) + key = (function.__name__,) + helpers.dict_to_tuple(kwargs) + print(key, key in self.caches['tag_exports']) + try: + exp = self.caches['tag_exports'][key] + print('Export was cached') + return exp + except KeyError: + print('Export was not cached') + exp = function(**kwargs) + self.caches['tag_exports'][key] = exp + return exp + def get_root_things(self, thing_type): ''' For Groupable types, yield things which have no parent. @@ -673,7 +689,7 @@ class PDBPhotoMixin: tag_expression = None else: giveback_tag_expression = str(tag_expression_tree) - frozen_children = self.get_cached_tag_flat_dict() + frozen_children = self.get_cached_tag_export('flat_dict', tags=self.get_root_tags()) tag_match_function = searchhelpers.tag_expression_matcher_builder(frozen_children) else: giveback_tag_expression = None @@ -1127,7 +1143,7 @@ class PDBTagMixin: author_id = self.get_user_id_or_none(author) - self._cached_tag_flat_dict = None + self.caches['tag_exports'].clear() data = { 'id': tag_id, @@ -1641,14 +1657,12 @@ class PhotoDB( self.load_config() self.log.setLevel(self.config['log_level']) - # OTHER - self._cached_tag_flat_dict = None - self.caches = { 'album': cacheclass.Cache(maxlen=self.config['cache_size']['album']), 'bookmark': cacheclass.Cache(maxlen=self.config['cache_size']['bookmark']), 'photo': cacheclass.Cache(maxlen=self.config['cache_size']['photo']), 'tag': cacheclass.Cache(maxlen=self.config['cache_size']['tag']), + 'tag_exports': cacheclass.Cache(maxlen=100), 'user': cacheclass.Cache(maxlen=self.config['cache_size']['user']), } @@ -1727,12 +1741,6 @@ class PhotoDB( self.sql_update(table='id_numbers', pairs=pairs, where_key='tab') return new_id - def get_cached_tag_flat_dict(self): - if self._cached_tag_flat_dict is None: - self._cached_tag_flat_dict = tag_export.flat_dict(self.get_root_tags()) - print(len(self._cached_tag_flat_dict)) - return self._cached_tag_flat_dict - def load_config(self): (config, needs_rewrite) = configlayers.load_file( filepath=self.config_filepath,