5 new revisions:
Revision: 6ab0ed59cf1d
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Tue Nov 29 03:23:58 2011
Log: Extract cache from HTTP code.
http://code.google.com/p/couchdb-python/source/detail?r=6ab0ed59cf1d
Revision: 2e705dfc2f17
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Dec 1 02:14:25 2011
Log: Move CACHE_SIZE settngs to Cache class and give values some
meaning.
http://code.google.com/p/couchdb-python/source/detail?r=2e705dfc2f17
Revision: e1f59d237754
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Dec 1 02:16:19 2011
Log: Remove spurious code from test module....
http://code.google.com/p/couchdb-python/source/detail?r=e1f59d237754
Revision: b95750c17c7b
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Sep 20 11:13:47 2012
Log: Merge changes to extract cache and cleanup strangeness in test.
http://code.google.com/p/couchdb-python/source/detail?r=b95750c17c7b
Revision: 4c4d3ecf896b
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Sep 20 11:22:48 2012
Log: Fix cache remove miss error.
http://code.google.com/p/couchdb-python/source/detail?r=4c4d3ecf896b
==============================================================================
Revision: 6ab0ed59cf1d
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Tue Nov 29 03:23:58 2011
Log: Extract cache from HTTP code.
http://code.google.com/p/couchdb-python/source/detail?r=6ab0ed59cf1d
Modified:
/couchdb/http.py
=======================================
--- /couchdb/http.py Sat Oct 29 07:56:51 2011
+++ /couchdb/http.py Tue Nov 29 03:23:58 2011
@@ -227,8 +227,17 @@
"""
from couchdb import __version__ as VERSION
self.user_agent = 'CouchDB-Python/%s' % VERSION
- if cache is None:
- cache = {}
+ # XXX We accept a `cache` dict arg, but the ref gets overwritten
later
+ # during cache cleanup. Do we remove the cache arg (does using a
shared
+ # Session instance cover the same use cases?) or fix the cache
cleanup?
+ # For now, let's just assign the dict to the Cache instance to
retain
+ # current behaviour.
+ if cache is not None:
+ cache_by_url = cache
+ cache = Cache()
+ cache.by_url = cache_by_url
+ else:
+ cache = Cache()
self.cache = cache
self.max_redirects = max_redirects
self.perm_redirects = {}
@@ -331,7 +340,7 @@
data = StringIO(data)
return status, msg, data
elif cached_resp:
- del self.cache[url]
+ self.cache.remove(url)
# Handle redirects
if status == 303 or \
@@ -394,18 +403,34 @@
# Store cachable responses
if not streamed and method == 'GET' and 'etag' in resp.msg:
- self.cache[url] = (status, resp.msg, data)
- if len(self.cache) > CACHE_SIZE[1]:
- self._clean_cache()
+ self.cache.put(url, (status, resp.msg, data))
if not streamed and data is not None:
data = StringIO(data)
return status, resp.msg, data
- def _clean_cache(self):
- ls = sorted(self.cache.iteritems(), key=cache_sort)
- self.cache = dict(ls[-CACHE_SIZE[0]:])
+
+class Cache(object):
+ """Content cache."""
+
+ def __init__(self):
+ self.by_url = {}
+
+ def get(self, url):
+ return self.by_url.get(url)
+
+ def put(self, url, response):
+ self.by_url[url] = response
+ if len(self.by_url) > CACHE_SIZE[1]:
+ self._clean()
+
+ def remove(self, url):
+ del self.by_url[url]
+
+ def _clean(self):
+ ls = sorted(self.by_url.iteritems(), key=cache_sort)
+ self.by_url = dict(ls[-CACHE_SIZE[0]:])
class ConnectionPool(object):
==============================================================================
Revision: 2e705dfc2f17
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Dec 1 02:14:25 2011
Log: Move CACHE_SIZE settngs to Cache class and give values some
meaning.
http://code.google.com/p/couchdb-python/source/detail?r=2e705dfc2f17
Modified:
/couchdb/http.py
=======================================
--- /couchdb/http.py Tue Nov 29 03:23:58 2011
+++ /couchdb/http.py Thu Dec 1 02:14:25 2011
@@ -163,7 +163,6 @@
CHUNK_SIZE = 1024 * 8
-CACHE_SIZE = 10, 75 # some random values to limit memory use
def cache_sort(i):
t =
time.mktime(time.strptime(i[1][1]['Date'][5:-4], '%d %b %Y %H:%M:%S'))
@@ -414,6 +413,9 @@
class Cache(object):
"""Content cache."""
+ # Some random values to limit memory use
+ keep_size, max_size = 10, 75
+
def __init__(self):
self.by_url = {}
@@ -422,7 +424,7 @@
def put(self, url, response):
self.by_url[url] = response
- if len(self.by_url) > CACHE_SIZE[1]:
+ if len(self.by_url) > self.max_size:
self._clean()
def remove(self, url):
@@ -430,7 +432,7 @@
def _clean(self):
ls = sorted(self.by_url.iteritems(), key=cache_sort)
- self.by_url = dict(ls[-CACHE_SIZE[0]:])
+ self.by_url = dict(ls[-self.keep_size:])
class ConnectionPool(object):
==============================================================================
Revision: e1f59d237754
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Dec 1 02:16:19 2011
Log: Remove spurious code from test module.
It doesn't seem to make any difference now or when the change was
committed. It
also affects all tests running in the process.
http://code.google.com/p/couchdb-python/source/detail?r=e1f59d237754
Modified:
/couchdb/tests/client.py
=======================================
--- /couchdb/tests/client.py Tue Oct 18 03:15:27 2011
+++ /couchdb/tests/client.py Thu Dec 1 02:16:19 2011
@@ -20,7 +20,7 @@
from couchdb import client, http
from couchdb.tests import testutil
-http.CACHE_SIZE = 2, 3
+
class ServerTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
==============================================================================
Revision: b95750c17c7b
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Sep 20 11:13:47 2012
Log: Merge changes to extract cache and cleanup strangeness in test.
http://code.google.com/p/couchdb-python/source/detail?r=b95750c17c7b
==============================================================================
Revision: 4c4d3ecf896b
Branch: default
Author: Matt Goodall <
matt.g...@gmail.com>
Date: Thu Sep 20 11:22:48 2012
Log: Fix cache remove miss error.
http://code.google.com/p/couchdb-python/source/detail?r=4c4d3ecf896b
Modified:
/couchdb/http.py
/couchdb/tests/http.py
=======================================
--- /couchdb/http.py Thu Dec 1 02:14:25 2011
+++ /couchdb/http.py Thu Sep 20 11:22:48 2012
@@ -428,7 +428,7 @@
self._clean()
def remove(self, url):
- del self.by_url[url]
+ self.by_url.pop(url, None)
def _clean(self):
ls = sorted(self.by_url.iteritems(), key=cache_sort)
=======================================
--- /couchdb/tests/http.py Sat May 28 15:14:29 2011
+++ /couchdb/tests/http.py Thu Sep 20 11:22:48 2012
@@ -67,11 +67,23 @@
self.assertEqual(list(response), [])
+class CacheTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
+
+ def test_remove_miss(self):
+ """Check that a cache remove miss is handled gracefully."""
+ url = '
http://localhost:5984/foo'
+ cache = http.Cache()
+ cache.put(url, (None, None, None))
+ cache.remove(url)
+ cache.remove(url)
+
+
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(http))
suite.addTest(unittest.makeSuite(SessionTestCase, 'test'))
suite.addTest(unittest.makeSuite(ResponseBodyTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(CacheTestCase, 'test'))
return suite