Skip to content

Commit da3ce93

Browse files
committed
Merge changes to extract cache and cleanup strangeness in test.
2 parents e574128 + e14e10e commit da3ce93

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

couchdb/http.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ class RedirectLimit(Exception):
163163

164164

165165
CHUNK_SIZE = 1024 * 8
166-
CACHE_SIZE = 10, 75 # some random values to limit memory use
167166

168167
def cache_sort(i):
169168
t = time.mktime(time.strptime(i[1][1]['Date'][5:-4], '%d %b %Y %H:%M:%S'))
@@ -227,8 +226,17 @@ def __init__(self, cache=None, timeout=None, max_redirects=5,
227226
"""
228227
from couchdb import __version__ as VERSION
229228
self.user_agent = 'CouchDB-Python/%s' % VERSION
230-
if cache is None:
231-
cache = {}
229+
# XXX We accept a `cache` dict arg, but the ref gets overwritten later
230+
# during cache cleanup. Do we remove the cache arg (does using a shared
231+
# Session instance cover the same use cases?) or fix the cache cleanup?
232+
# For now, let's just assign the dict to the Cache instance to retain
233+
# current behaviour.
234+
if cache is not None:
235+
cache_by_url = cache
236+
cache = Cache()
237+
cache.by_url = cache_by_url
238+
else:
239+
cache = Cache()
232240
self.cache = cache
233241
self.max_redirects = max_redirects
234242
self.perm_redirects = {}
@@ -331,7 +339,7 @@ def _try_request():
331339
data = StringIO(data)
332340
return status, msg, data
333341
elif cached_resp:
334-
del self.cache[url]
342+
self.cache.remove(url)
335343

336344
# Handle redirects
337345
if status == 303 or \
@@ -394,18 +402,37 @@ def _try_request():
394402

395403
# Store cachable responses
396404
if not streamed and method == 'GET' and 'etag' in resp.msg:
397-
self.cache[url] = (status, resp.msg, data)
398-
if len(self.cache) > CACHE_SIZE[1]:
399-
self._clean_cache()
405+
self.cache.put(url, (status, resp.msg, data))
400406

401407
if not streamed and data is not None:
402408
data = StringIO(data)
403409

404410
return status, resp.msg, data
405411

406-
def _clean_cache(self):
407-
ls = sorted(self.cache.iteritems(), key=cache_sort)
408-
self.cache = dict(ls[-CACHE_SIZE[0]:])
412+
413+
class Cache(object):
414+
"""Content cache."""
415+
416+
# Some random values to limit memory use
417+
keep_size, max_size = 10, 75
418+
419+
def __init__(self):
420+
self.by_url = {}
421+
422+
def get(self, url):
423+
return self.by_url.get(url)
424+
425+
def put(self, url, response):
426+
self.by_url[url] = response
427+
if len(self.by_url) > self.max_size:
428+
self._clean()
429+
430+
def remove(self, url):
431+
del self.by_url[url]
432+
433+
def _clean(self):
434+
ls = sorted(self.by_url.iteritems(), key=cache_sort)
435+
self.by_url = dict(ls[-self.keep_size:])
409436

410437

411438
class ConnectionPool(object):

couchdb/tests/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from couchdb import client, http
2222
from couchdb.tests import testutil
23-
http.CACHE_SIZE = 2, 3
23+
2424

2525
class ServerTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
2626

0 commit comments

Comments
 (0)