use dictionary for stream cache
authorClinton Ebadi <clinton@unknownlamer.org>
Sun, 26 Apr 2015 23:24:59 +0000 (19:24 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Sun, 26 Apr 2015 23:24:59 +0000 (19:24 -0400)
Key on songID. Avoids iterating over the entire set for every cache
miss.

default.py

index cce1bbd..2eba189 100644 (file)
@@ -930,21 +930,22 @@ class Grooveshark:
     def _getSongStream(self, songid):
         idSong = int(songid)
         stream = None
-        streams = []
+        streams = {}
         path = os.path.join(cacheDir, 'streams.dmp')
         try:
             f = open(path, 'rb')
             streams = pickle.load(f)
-            for song in streams:
-                if song[0] == idSong:
-                    duration = song[1]
-                    url = song[2]
-                    key = song[3]
-                    server = song[4]
-                    stream = [idSong, duration, url, key, server]
-                    if __debugging__ :
-                        xbmc.log("Found " + str(idSong) + " in stream cache")
-                    break;
+            song = streams[songid]
+
+            duration = song[1]
+            url = song[2]
+            key = song[3]
+            server = song[4]
+            stream = [idSong, duration, url, key, server]
+
+            if __debugging__ :
+                xbmc.log("Found " + str(idSong) + " in stream cache")
+
             f.close()
         except:
             pass
@@ -964,7 +965,7 @@ class Grooveshark:
         
     def _addSongStream(self, stream):
         streams = self._getStreams()           
-        streams.append(stream)                
+        streams[int(stream[0])] = stream
         path = os.path.join(cacheDir, 'streams.dmp')
         try:
             f = open(path, 'wb')
@@ -980,21 +981,17 @@ class Grooveshark:
         stream[1] = self._setDuration(stream[1])
         streams = self._getStreams()
         path = os.path.join(cacheDir, 'streams.dmp')
-        i = 0
 
-        for song in streams:
-            if song[0] == idStream:
-                streams[i] = stream
-                try:
-                    f = open(path, 'wb')
-                    pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
-                    f.close()
-                    if __debugging__ :
-                        xbmc.log("Updated " + str(idStream) + " in stream cache")
-                    break;
-                except:
-                    xbmc.log("An error occurred setting stream")                    
-            i = i + 1
+        try:
+            streams[idStream] = stream
+            f = open(path, 'wb')
+            pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+            f.close()
+            if __debugging__ :
+                xbmc.log("Updated " + str(idStream) + " in stream cache")
+
+        except:
+            xbmc.log("An error occurred setting stream")
     
     def _getStreams(self):
         path = os.path.join(cacheDir, 'streams.dmp')
@@ -1003,7 +1000,7 @@ class Grooveshark:
             streams = pickle.load(f)
             f.close()
         except:
-            streams = []
+            streams = {}
             pass
         return streams