added ftp connectin cache

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@1840 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2004-09-20 19:21:35 +00:00
parent 5201de5cb6
commit 304bdda3bf

View file

@ -47,7 +47,8 @@ def _check_morsel (m, host, path):
class Cache (object):
"""Store and provide routines for cached data. Currently there are
caches for cookies, check urls and robots.txt contents.
caches for cookies, checked urls, FTP connections and robots.txt
contents.
All public operations (except __init__()) are thread-safe.
"""
@ -58,6 +59,9 @@ class Cache (object):
self.lock = threading.Lock()
# already checked urls
self.checked = {}
# open FTP connections
# {(host,user,pass) -> [connection, status]}
self.ftp_connections = {}
# urls that are being checked
self.in_progress = {}
# to-be-checked urls
@ -196,6 +200,44 @@ class Cache (object):
finally:
self.lock.release()
def get_ftp_connection (self, host, username, password):
"""Get open FTP connection to given host. Return None if no such
connection is available.
"""
self.lock.acquire()
try:
key = (host, username, password)
if key in self.ftp_connections:
conn_and_status = self.ftp_connections[key]
if conn_and_status[1] == 'busy':
# connection is in use
return None
conn_and_status[1] = 'busy'
return conn_and_status[0]
finally:
self.lock.release()
def add_ftp_connection (self, host, username, password, conn):
"""Store open FTP connection into cache for reuse."""
self.lock.acquire()
try:
key = (host, username, password)
cached = key in self.ftp_connections
if not cached:
self.ftp_connections[key] = [conn, 'busy']
return cached
finally:
self.lock.release()
def release_ftp_connection (self, host, username, password):
"""Store open FTP connection into cache for reuse."""
self.lock.acquire()
try:
key = (host, username, password)
self.ftp_connections[key][1] = 'available'
finally:
self.lock.release()
def store_cookies (self, headers, host):
"""Thread-safe cookie cache setter function. Can raise the
exception Cookie.CookieError.