Subreddit._by_name: Set 1 hour cache TTL

In the case of cache outages the values in cache can become out of
sync with reality. If a subreddit is created during an outage it will
be impossible to access it with Subreddit._by_name until the cache is
corrected. Setting a TTL will ensure this happens eventually.
This commit is contained in:
Brian Simpson
2016-08-08 20:25:25 -07:00
parent 05b24d44da
commit ea226bbc10
2 changed files with 13 additions and 3 deletions

View File

@@ -476,7 +476,11 @@ class Subreddit(Thing, Printable, BaseSite):
still_missing = set(srnames) - set(fetched)
fetched.update((name, cls.SRNAME_NOTFOUND) for name in still_missing)
try:
g.gencache.set_multi(fetched, prefix='srid:')
g.gencache.set_multi(
keys=fetched,
prefix='srid:',
time=3600,
)
except MemcachedError:
pass

View File

@@ -226,7 +226,10 @@ class ByNameTest(unittest.TestCase):
self.assertEqual(ret, sr)
self.cache.set_multi.assert_called_once_with(
{sr.name: sr._id}, prefix="srid:")
keys={sr.name: sr._id},
prefix="srid:",
time=3600,
)
def testCacheNegativeResults(self):
self.cache.get_multi.return_value = {}
@@ -237,7 +240,10 @@ class ByNameTest(unittest.TestCase):
Subreddit._by_name("doesnotexist")
self.cache.set_multi.assert_called_once_with(
{"doesnotexist": Subreddit.SRNAME_NOTFOUND}, prefix="srid:")
keys={"doesnotexist": Subreddit.SRNAME_NOTFOUND},
prefix="srid:",
time=3600,
)
def testExcludeNegativeLookups(self):
self.cache.get_multi.return_value = {"doesnotexist": Subreddit.SRNAME_NOTFOUND}