def bounty_lookup(vendor): key = '%s-bounties' % vendor val = cache.read(key) if val is not None: return val try: bounties = read_bounty_tables(vendor) except ScrapeError as e: return e bounty_template = '%(name)s - %(desc)s' bounty_output = [] for name, desc in bounties.items(): line = bounty_template % {'name': name, 'desc': desc} bounty_output.append(line) out = '\n'.join(bounty_output) expiry = datetime.datetime.now() + datetime.timedelta(hours=1) cache.write(key, out, expiry) return out
def test_writing_with_newer_expire(): cache.write('test', '1', tomorrow) result = cache.write('test', '2', today) assert result == False val = cache.read('test') assert val == '1'
def _get_cache(self): if 'REFRESH_CACHE' in os.environ: if os.environ['REFRESH_CACHE'] == 'True': return False if not self.ops_config.get('inventory.max_age'): logger.info("Inventory caching disabled") return False max_age = self.ops_config.get('inventory.max_age') logger.info("Checking cache from max_age=%s, location=%s" % (max_age, self.cache_location)) if caching.is_valid(self.cache_location, int(max_age)): res = caching.read(self.cache_location) display.display("Loading cached inventory info from: %s" % (self.cache_location), color='blue', stderr=True) return res return False
def test_reading_nonexistent_key(): val = cache.read('test') assert val is None
def test_writing_expires_old_value(): cache.write('test', '1', yesterday) cache.write('test', '2', tomorrow) value = cache.read('test') assert value == '2'
def test_reading_expires_old_value(): cache.write('test', '1', yesterday) value = cache.read('test') assert value == None
def test_write(): cache.write('test', '92', tomorrow) value = cache.read('test') assert value == '92'