Making what.cd cache threadsafe through simple locking

This commit is contained in:
Aaron Cohen
2012-09-15 21:13:42 -07:00
parent 362b9c1cdf
commit 04571e5813
2 changed files with 13 additions and 6 deletions

View File

@@ -831,7 +831,7 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False):
logger.warn("What.cd credentials incorrect or site is down.")
if whatcd:
# whatcd.enableCaching()
whatcd.enableCaching()
logger.info("Getting artist information for %s..." % artistterm)
artist = whatcd.getArtist(artistterm)
artist_id = artist.getArtistId()
@@ -855,8 +855,9 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False):
all_children = []
for group in release_torrent_groups:
logger.info(u"Getting individual torrents for parent ID %s" % group.getTorrentParentId())
all_children += group.getTorrentChildren()
logger.info(u"Found torrent IDs: %s" % ", ".join(all_children))
new_children = group.getTorrentChildren()
all_children += new_children
logger.info(u"Found torrent IDs: %s" % ", ".join(new_children))
# cap at 10 matches, 1 per second to reduce hits on API...don't wanna get in trouble.
# Might want to turn up number of matches later.
# max_torrent_info_reads = 10

View File

@@ -36,6 +36,7 @@ import re
import urllib
import shelve
import tempfile
import threading
from htmlentitydefs import name2codepoint as n2cp
@@ -339,17 +340,22 @@ def getWhatcdNetwork(username="", password=""):
class _ShelfCacheBackend(object):
"""Used as a backend for caching cacheable requests."""
cache_lock = threading.Lock()
def __init__(self, file_path=None):
self.shelf = shelve.open(file_path)
def getHTML(self, key):
return self.shelf[key]
with _ShelfCacheBackend.cache_lock:
return self.shelf[key]
def setHTML(self, key, xml_string):
self.shelf[key] = xml_string
with _ShelfCacheBackend.cache_lock:
self.shelf[key] = xml_string
def hasKey(self, key):
return key in self.shelf.keys()
with _ShelfCacheBackend.cache_lock:
return key in self.shelf.keys()
class Request(object):