Add cache expiry check by key update times to fix issue where some status updates would not return

correctly if they were done < cache_time from the previous status request
This commit is contained in:
Andrew Resch 2010-08-18 12:15:53 -07:00
parent 7d64f057c7
commit ca5eaf4270
1 changed files with 45 additions and 8 deletions

View File

@ -62,6 +62,9 @@ class SessionProxy(component.Component):
# Hold the torrents' status.. {torrent_id: [time, {status_dict}], ...} # Hold the torrents' status.. {torrent_id: [time, {status_dict}], ...}
self.torrents = {} self.torrents = {}
# Holds the time of the last key update.. {torrent_id: {key1, time, ...}, ...}
self.cache_times = {}
client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed) client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed)
client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed) client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed)
client.register_event_handler("TorrentAddedEvent", self.on_torrent_added) client.register_event_handler("TorrentAddedEvent", self.on_torrent_added)
@ -72,6 +75,9 @@ class SessionProxy(component.Component):
t = time.time() t = time.time()
for key, value in status.items(): for key, value in status.items():
self.torrents[key] = [t, value] self.torrents[key] = [t, value]
self.cache_times[key] = {}
for k, v in value.items():
self.cache_times[key][k] = t
return client.core.get_torrents_status({}, [], True).addCallback(on_torrent_status) return client.core.get_torrents_status({}, [], True).addCallback(on_torrent_status)
@ -117,20 +123,37 @@ class SessionProxy(component.Component):
""" """
if torrent_id in self.torrents: if torrent_id in self.torrents:
if time.time() - self.torrents[torrent_id][0] < self.cache_time: # Keep track of keys we need to request from the core
return succeed(self.create_status_dict([torrent_id], keys)[torrent_id]) keys_to_get = []
if not keys:
keys = self.torrents[torrent_id][1].keys()
for key in keys:
if time.time() - self.cache_times[torrent_id][key] > self.cache_time:
keys_to_get.append(key)
if not keys_to_get:
return succeed(self.create_status_dict([torrent_id], keys)[torrent_id])
else: else:
d = client.core.get_torrent_status(torrent_id, keys, True) d = client.core.get_torrent_status(torrent_id, keys_to_get, True)
def on_status(result, torrent_id): def on_status(result, torrent_id):
self.torrents[torrent_id][0] = time.time() t = time.time()
self.torrents[torrent_id][0] = t
self.torrents[torrent_id][1].update(result) self.torrents[torrent_id][1].update(result)
for key in keys_to_get:
self.cache_times[torrent_id][key] = t
return self.create_status_dict([torrent_id], keys)[torrent_id] return self.create_status_dict([torrent_id], keys)[torrent_id]
return d.addCallback(on_status, torrent_id) return d.addCallback(on_status, torrent_id)
else: else:
d = client.core.get_torrent_status(torrent_id, keys, True) d = client.core.get_torrent_status(torrent_id, keys, True)
def on_status(result): def on_status(result):
if result: if result:
self.torrents[torrent_id] = (time.time(), result) t = time.time()
self.torrents[torrent_id] = (t, result)
self.cache_times[torrent_id] = {}
for key in result:
self.cache_times[torrent_id][key] = t
return result return result
return d.addCallback(on_status) return d.addCallback(on_status)
@ -158,6 +181,8 @@ class SessionProxy(component.Component):
for key, value in result.items(): for key, value in result.items():
self.torrents[key][0] = t self.torrents[key][0] = t
self.torrents[key][1].update(value) self.torrents[key][1].update(value)
for k in value:
self.cache_times[key][k] = t
# Create the status dict # Create the status dict
if not torrent_ids: if not torrent_ids:
@ -168,10 +193,16 @@ class SessionProxy(component.Component):
def find_torrents_to_fetch(torrent_ids): def find_torrents_to_fetch(torrent_ids):
to_fetch = [] to_fetch = []
t = time.time() t = time.time()
for key in torrent_ids: for torrent_id in torrent_ids:
torrent = self.torrents[key] torrent = self.torrents[torrent_id]
if t - torrent[0] > self.cache_time: if t - torrent[0] > self.cache_time:
to_fetch.append(key) to_fetch.append(torrent_id)
else:
# We need to check if a key is expired
for key in keys:
if t - self.cache_times[torrent_id][key] > self.cache_time:
to_fetch.append(torrent_id)
break
return to_fetch return to_fetch
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
@ -206,12 +237,18 @@ class SessionProxy(component.Component):
def on_torrent_state_changed(self, torrent_id, state): def on_torrent_state_changed(self, torrent_id, state):
if torrent_id in self.torrents: if torrent_id in self.torrents:
self.torrents[torrent_id][1]["state"] = state self.torrents[torrent_id][1]["state"] = state
self.cache_times[torrent_id]["state"] = time.time()
def on_torrent_added(self, torrent_id): def on_torrent_added(self, torrent_id):
self.torrents[torrent_id] = [time.time() - self.cache_time - 1, {}] self.torrents[torrent_id] = [time.time() - self.cache_time - 1, {}]
def on_status(status): def on_status(status):
self.torrents[torrent_id][1].update(status) self.torrents[torrent_id][1].update(status)
self.cache_times[torrent_id] = {}
t = time.time()
for key in status:
self.cache_times[torrent_id][key] = t
client.core.get_torrent_status(torrent_id, []).addCallback(on_status) client.core.get_torrent_status(torrent_id, []).addCallback(on_status)
def on_torrent_removed(self, torrent_id): def on_torrent_removed(self, torrent_id):
del self.torrents[torrent_id] del self.torrents[torrent_id]
del self.cache_times[torrent_id]