Remove get_cached_flat_dict, add get_cached_tag_export.

This commit is contained in:
voussoir 2020-09-14 05:49:24 -07:00
parent cf64c79809
commit f3bceb4688
3 changed files with 30 additions and 20 deletions

View file

@ -115,6 +115,9 @@ def comma_space_split(s):
return s return s
return re.split(r'[ ,]+', s.strip()) return re.split(r'[ ,]+', s.strip())
def dict_to_tuple(d):
return tuple(sorted(d.items()))
def generate_image_thumbnail(filepath, width, height): def generate_image_thumbnail(filepath, width, height):
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
raise FileNotFoundError(filepath) raise FileNotFoundError(filepath)

View file

@ -1245,7 +1245,7 @@ class Tag(ObjectBase, GroupableMixin):
ret = super().add_child(*args, **kwargs) ret = super().add_child(*args, **kwargs)
if ret is BAIL: if ret is BAIL:
return return
self.photodb._cached_tag_flat_dict = None self.photodb.caches['tag_exports'].clear()
return ret return ret
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@ -1254,7 +1254,7 @@ class Tag(ObjectBase, GroupableMixin):
ret = super().add_children(*args, **kwargs) ret = super().add_children(*args, **kwargs)
if ret is BAIL: if ret is BAIL:
return return
self.photodb._cached_tag_flat_dict = None self.photodb.caches['tag_exports'].clear()
return ret return ret
@decorators.required_feature('tag.edit') @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.log.debug('New synonym %s of %s', synname, self.name)
self.photodb._cached_tag_flat_dict = None self.photodb.caches['tag_exports'].clear()
data = { data = {
'name': synname, 'name': synname,
@ -1292,7 +1292,7 @@ class Tag(ObjectBase, GroupableMixin):
''' '''
mastertag = self.photodb.get_tag(name=mastertag) 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 # Migrate the old tag's synonyms to the new one
# UPDATE is safe for this operation because there is no chance of duplicates. # UPDATE is safe for this operation because there is no chance of duplicates.
@ -1345,12 +1345,11 @@ class Tag(ObjectBase, GroupableMixin):
@decorators.transaction @decorators.transaction
def delete(self, *, delete_children=False): def delete(self, *, delete_children=False):
self.photodb.log.debug('Deleting %s', self) self.photodb.log.debug('Deleting %s', self)
self.photodb._cached_tag_flat_dict = None
super().delete(delete_children=delete_children) super().delete(delete_children=delete_children)
self.photodb.sql_delete(table='photo_tag_rel', pairs={'tagid': self.id}) 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='tag_synonyms', pairs={'mastername': self.name})
self.photodb.sql_delete(table='tags', pairs={'id': self.id}) 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() self._uncache()
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@ -1386,7 +1385,7 @@ class Tag(ObjectBase, GroupableMixin):
ret = super().remove_child(*args, **kwargs) ret = super().remove_child(*args, **kwargs)
if ret is BAIL: if ret is BAIL:
return return
self.photodb._cached_tag_flat_dict = None self.photodb.caches['tag_exports'].clear()
return ret return ret
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@ -1409,7 +1408,7 @@ class Tag(ObjectBase, GroupableMixin):
if syn_exists is None: if syn_exists is None:
raise exceptions.NoSuchSynonym(synname) 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}) self.photodb.sql_delete(table='tag_synonyms', pairs={'name': synname})
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@ -1430,7 +1429,7 @@ class Tag(ObjectBase, GroupableMixin):
else: else:
raise exceptions.TagExists(new_name) raise exceptions.TagExists(new_name)
self.photodb._cached_tag_flat_dict = None self.photodb.caches['tag_exports'].clear()
data = { data = {
'id': self.id, 'id': self.id,

View file

@ -224,6 +224,22 @@ class PDBCacheManagerMixin:
thing_cache[thing_id] = thing thing_cache[thing_id] = thing
return 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): def get_root_things(self, thing_type):
''' '''
For Groupable types, yield things which have no parent. For Groupable types, yield things which have no parent.
@ -673,7 +689,7 @@ class PDBPhotoMixin:
tag_expression = None tag_expression = None
else: else:
giveback_tag_expression = str(tag_expression_tree) 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) tag_match_function = searchhelpers.tag_expression_matcher_builder(frozen_children)
else: else:
giveback_tag_expression = None giveback_tag_expression = None
@ -1127,7 +1143,7 @@ class PDBTagMixin:
author_id = self.get_user_id_or_none(author) author_id = self.get_user_id_or_none(author)
self._cached_tag_flat_dict = None self.caches['tag_exports'].clear()
data = { data = {
'id': tag_id, 'id': tag_id,
@ -1641,14 +1657,12 @@ class PhotoDB(
self.load_config() self.load_config()
self.log.setLevel(self.config['log_level']) self.log.setLevel(self.config['log_level'])
# OTHER
self._cached_tag_flat_dict = None
self.caches = { self.caches = {
'album': cacheclass.Cache(maxlen=self.config['cache_size']['album']), 'album': cacheclass.Cache(maxlen=self.config['cache_size']['album']),
'bookmark': cacheclass.Cache(maxlen=self.config['cache_size']['bookmark']), 'bookmark': cacheclass.Cache(maxlen=self.config['cache_size']['bookmark']),
'photo': cacheclass.Cache(maxlen=self.config['cache_size']['photo']), 'photo': cacheclass.Cache(maxlen=self.config['cache_size']['photo']),
'tag': cacheclass.Cache(maxlen=self.config['cache_size']['tag']), 'tag': cacheclass.Cache(maxlen=self.config['cache_size']['tag']),
'tag_exports': cacheclass.Cache(maxlen=100),
'user': cacheclass.Cache(maxlen=self.config['cache_size']['user']), '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') self.sql_update(table='id_numbers', pairs=pairs, where_key='tab')
return new_id 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): def load_config(self):
(config, needs_rewrite) = configlayers.load_file( (config, needs_rewrite) = configlayers.load_file(
filepath=self.config_filepath, filepath=self.config_filepath,