Damián Nohales avatar Damián Nohales committed 5aa6765

Improved performance a memory usage on cover management.

Comments (0)

Files changed (2)

             self.filename = self.data["filename"]
         except KeyError:
             self.filename = self.get_default_filename()
-        
-        if self.get_cover_filename() == "":
-            self.set_cover_missed_pixbuf()
-        else:
-            self.cover_pixbuf = covercache.get_pixbuf(self)
             
         self.connect("download-initializing", self.on_download_initializing)
         self.connect("download-started", self.on_download_started)
         on AbstractSongList
         """
         newsong = Song(self.get_data())
-        newsong.set_cover_pixbuf(self.get_cover_pixbuf())
+        if newsong.get_cover_pixbuf() == None:
+            newsong.set_cover_pixbuf(self.get_cover_pixbuf())
         newsong.set_filename(self.get_filename())
         
         return newsong
         If returns None, means that we need to download the cover
         using download_cover
         """
+        if self.cover_pixbuf == None:
+            if self.get_cover_filename() == "":
+                self.set_cover_missed_pixbuf()
+            else:
+                self.set_cover_cached_pixbuf()
         return self.cover_pixbuf
         
     def set_cover_pixbuf(self, pixbuf):
         self.cover_pixbuf = pixbuf
         
     def set_cover_missed_pixbuf(self):
-        self.cover_pixbuf = gtk.gdk.pixbuf_new_from_file("%s/data/sdefault.png" % env().BASEPATH)
+        self.cover_pixbuf = covercache.get_missed_pixbuf()
+        
+    def set_cover_cached_pixbuf(self):
+        self.cover_pixbuf = covercache.get_pixbuf(self)
         
     def download_cover(self):
         """
         if self.song.get_cover_filename() in SongCoverThread.downloading_events:
             SongCoverThread.internal_lock.release()
             SongCoverThread.downloading_events[self.song.get_cover_filename()].wait()
-            self.song.cover_pixbuf = covercache.get_pixbuf(self.song)
+            self.song.set_cover_cached_pixbuf()
             if self.song.cover_pixbuf == None:
                 self.song.set_cover_missed_pixbuf()
             gobject.idle_add( self.song.emit, "cover-downloaded" )

lib/covercache.py

 import gtk
 from enviroment import env
 
+_missed_pixbuf = None
+_mem_cache = {}
+
 def get_filename(song):
     if song.get_cover_filename() == "":
         return None
         return os.path.join(env().get_config_directory(), "covers", song.get_cover_filename())
 
 def get_pixbuf(song):
-    if os.path.exists(get_filename(song)):
-        return gtk.gdk.pixbuf_new_from_file(get_filename(song))
+    global _mem_cache
+    
+    fname = get_filename(song)
+    if fname in _mem_cache:
+        return _mem_cache[fname] 
+    
+    if os.path.exists(fname):
+        with file(fname, 'a'):
+            os.utime(fname, None)
+        _mem_cache[fname] = gtk.gdk.pixbuf_new_from_file(fname)
+        return _mem_cache[fname]
     else:
         return None
 
+def get_missed_pixbuf():
+    global _missed_pixbuf
+    if _missed_pixbuf == None:
+        _missed_pixbuf = gtk.gdk.pixbuf_new_from_file("%s/data/sdefault.png" % env().BASEPATH)
+    return _missed_pixbuf
+
 def analyze_cache_limit():
     pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.