From c64c644c8947d11a07c2424f034fe0756b4211b8 Mon Sep 17 00:00:00 2001 From: Clinton Ebadi Date: Sun, 26 Apr 2015 19:24:59 -0400 Subject: [PATCH] use dictionary for stream cache Key on songID. Avoids iterating over the entire set for every cache miss. --- default.py | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/default.py b/default.py index cce1bbd..2eba189 100644 --- a/default.py +++ b/default.py @@ -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 -- 2.20.1