In TempURL, cache "no keys" for a shorter time.

In other places in the codebase, we cache a negative result for less
time (usually a tenth) than we cache a positive result. This commit
makes TempURL do the same.

This creates a slightly nicer experience for someone trying to set up
TempURL; if they get the initial key setup wrong, try it, then fix it,
it now takes only 6 seconds until they can use signed URLs, not
60. That's a short enough time that they may not even notice the
caching of no keys.

Change-Id: I521f023e7cddaecd07f0ce32aedd4059bd0b8ec4
This commit is contained in:
Samuel Merritt 2013-05-29 18:55:10 -07:00
parent a2db3b6f32
commit 2c9aee55fb
2 changed files with 43 additions and 1 deletions

View File

@ -384,7 +384,8 @@ class TempURL(object):
except StopIteration:
pass
if memcache:
memcache.set(memcache_hash_key, keys, time=60)
timeout = 60 if keys else 6
memcache.set(memcache_hash_key, keys, time=timeout)
return keys
def _get_hmacs(self, env, expires, keys, request_method=None):

View File

@ -27,18 +27,25 @@ class FakeMemcache(object):
def __init__(self):
self.store = {}
self.times = {}
def get(self, key):
return self.store.get(key)
def set(self, key, value, time=0):
self.store[key] = value
self.times[key] = time
return True
def incr(self, key, time=0):
self.store[key] = self.store.setdefault(key, 0) + 1
if time:
self.times[key] = time
return self.store[key]
def time_for_key(self, key):
return self.times.get(key)
@contextmanager
def soft_lock(self, key, timeout=0, retries=5):
yield True
@ -218,6 +225,40 @@ class TestTempURL(unittest.TestCase):
self.assertEquals(resp.status_int, 401)
self.assertTrue('Temp URL invalid' in resp.body)
def test_cache_miss_with_keys(self):
self.app.status_headers_body_iter = iter(
[('200 OK', {'X-Account-Meta-Temp-Url-Key': 'some-key'}, '')])
# doesn't have to be valid, just has to trigger a check
req = self._make_request('/v1/a/c/o',
environ={'QUERY_STRING':
'temp_url_sig=abcd&temp_url_expires=%d' %
int(time() + 1000)})
resp = req.get_response(self.tempurl)
self.assertEquals(resp.status_int, 401)
self.assertEquals(
['some-key'],
req.environ['swift.cache'].get('temp-url-keys/a'))
self.assertEquals(
60,
req.environ['swift.cache'].time_for_key('temp-url-keys/a'))
def test_cache_miss_without_keys(self):
self.app.status_headers_body_iter = iter([('200 OK', {}, '')])
req = self._make_request('/v1/a/c/o',
environ={'QUERY_STRING':
'temp_url_sig=abcd&temp_url_expires=%d' %
int(time() + 1000)})
resp = req.get_response(self.tempurl)
self.assertEquals(resp.status_int, 401)
self.assertEquals(
[],
req.environ['swift.cache'].get('temp-url-keys/a'))
self.assertEquals(
6,
req.environ['swift.cache'].time_for_key('temp-url-keys/a'))
def test_missing_sig(self):
method = 'GET'
expires = int(time() + 86400)