Cache objects for faster re-access; Cache album.sum_bytes result

This commit is contained in:
voussoir 2017-03-22 22:54:17 -07:00
parent 55ed6a6d28
commit 13040d559b
3 changed files with 23 additions and 7 deletions

View file

@ -146,8 +146,8 @@ DEFAULT_DATADIR = '.\\_etiquette'
DEFAULT_CONFIGURATION = {
'log_level': logging.DEBUG,
'cache_size_album': 200,
'cache_size_photo': 1000,
'cache_size_album': 1000,
'cache_size_photo': 100000,
'cache_size_tag': 1000,
'cache_size_user': 200,

View file

@ -210,6 +210,8 @@ class Album(ObjectBase, GroupableMixin):
self.description = db_row['description']
self.name = 'Album %s' % self.id
self.group_getter = self.photodb.get_album
self._sum_bytes_photos = None
self._sum_bytes_albums = None
def __hash__(self):
return hash(self.id)
@ -324,12 +326,14 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.commit()
def sum_bytes(self, recurse=True, string=False):
if recurse:
photos = self.walk_photos()
else:
photos = self.photos()
if self._sum_bytes_photos is None:
self._sum_bytes_photos = sum(photo.bytes for photo in self.photos())
total = self._sum_bytes_photos
total = sum(photo.bytes for photo in photos)
if recurse:
if self._sum_bytes_albums is None:
self._sum_bytes_albums = sum(a.sum_bytes(recurse=True) for a in self.children())
total += self._sum_bytes_albums
if string:
return bytestring.bytestring(total)

View file

@ -1410,6 +1410,17 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
if isinstance(thing_id, thing_map['class']):
thing_id = thing_id.id
cache = {
'album': self._album_cache,
'photo': self._photo_cache,
'tag': self._tag_cache,
}[thing_type]
try:
val = cache[thing_id]
return val
except KeyError:
pass
query = 'SELECT * FROM %s WHERE id == ?' % thing_map['table']
cur = self.sql.cursor()
cur.execute(query, [thing_id])
@ -1417,6 +1428,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
if thing is None:
raise thing_map['exception'](thing_id)
thing = thing_map['class'](self, thing)
cache[thing_id] = thing
return thing
def get_things(self, thing_type, orderby=None):