Cache objects for faster re-access; Cache album.sum_bytes result
This commit is contained in:
		
							parent
							
								
									55ed6a6d28
								
							
						
					
					
						commit
						13040d559b
					
				
					 3 changed files with 23 additions and 7 deletions
				
			
		|  | @ -146,8 +146,8 @@ DEFAULT_DATADIR = '.\\_etiquette' | ||||||
| DEFAULT_CONFIGURATION = { | DEFAULT_CONFIGURATION = { | ||||||
|     'log_level': logging.DEBUG, |     'log_level': logging.DEBUG, | ||||||
| 
 | 
 | ||||||
|     'cache_size_album': 200, |     'cache_size_album': 1000, | ||||||
|     'cache_size_photo': 1000, |     'cache_size_photo': 100000, | ||||||
|     'cache_size_tag': 1000, |     'cache_size_tag': 1000, | ||||||
|     'cache_size_user': 200, |     'cache_size_user': 200, | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -210,6 +210,8 @@ class Album(ObjectBase, GroupableMixin): | ||||||
|         self.description = db_row['description'] |         self.description = db_row['description'] | ||||||
|         self.name = 'Album %s' % self.id |         self.name = 'Album %s' % self.id | ||||||
|         self.group_getter = self.photodb.get_album |         self.group_getter = self.photodb.get_album | ||||||
|  |         self._sum_bytes_photos = None | ||||||
|  |         self._sum_bytes_albums = None | ||||||
| 
 | 
 | ||||||
|     def __hash__(self): |     def __hash__(self): | ||||||
|         return hash(self.id) |         return hash(self.id) | ||||||
|  | @ -324,12 +326,14 @@ class Album(ObjectBase, GroupableMixin): | ||||||
|             self.photodb.commit() |             self.photodb.commit() | ||||||
| 
 | 
 | ||||||
|     def sum_bytes(self, recurse=True, string=False): |     def sum_bytes(self, recurse=True, string=False): | ||||||
|         if recurse: |         if self._sum_bytes_photos is None: | ||||||
|             photos = self.walk_photos() |             self._sum_bytes_photos = sum(photo.bytes for photo in self.photos()) | ||||||
|         else: |         total = self._sum_bytes_photos | ||||||
|             photos = self.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: |         if string: | ||||||
|             return bytestring.bytestring(total) |             return bytestring.bytestring(total) | ||||||
|  |  | ||||||
|  | @ -1410,6 +1410,17 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs | ||||||
|         if isinstance(thing_id, thing_map['class']): |         if isinstance(thing_id, thing_map['class']): | ||||||
|             thing_id = thing_id.id |             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'] |         query = 'SELECT * FROM %s WHERE id == ?' % thing_map['table'] | ||||||
|         cur = self.sql.cursor() |         cur = self.sql.cursor() | ||||||
|         cur.execute(query, [thing_id]) |         cur.execute(query, [thing_id]) | ||||||
|  | @ -1417,6 +1428,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs | ||||||
|         if thing is None: |         if thing is None: | ||||||
|             raise thing_map['exception'](thing_id) |             raise thing_map['exception'](thing_id) | ||||||
|         thing = thing_map['class'](self, thing) |         thing = thing_map['class'](self, thing) | ||||||
|  |         cache[thing_id] = thing | ||||||
|         return thing |         return thing | ||||||
| 
 | 
 | ||||||
|     def get_things(self, thing_type, orderby=None): |     def get_things(self, thing_type, orderby=None): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue