def set(self, key, value): new_bytes = sys.getsizeof(value) total = (self.curr_bytes + new_bytes) if total > self.max_bytes: message = ("Tried adding %d bytes but %d bytes are currently saved" " in the cache and the max_bytes is set to %d." % (new_bytes, self.curr_bytes, self.max_bytes)) self.logger.warning(message) return FileCache.set(self, key, value) self.curr_bytes += new_bytes
def set(self, key, value): new_bytes = sys.getsizeof(value) total = (self.curr_bytes + new_bytes) if total > self.max_bytes: message = "Tried adding %d bytes but %d bytes are currently saved" \ " in the cache and the max_bytes is set to %d.\n" % \ (new_bytes, self.curr_bytes, self.max_bytes) self.logger.warn(message) return FileCache.set(self, key, value) self.curr_bytes += new_bytes
def get_session(): CACHE_FOLDER.mkdir(exist_ok=True) cache = FileCache(str(CACHE_FOLDER), forever=True) cache.set("foo", b"bar") assert cache.get("foo") == b"bar" session = RateLimitingSession() # session.headers.update({"x-api-key": "something-something-darkside"}) session.mount( "https://www.metlink.org.nz/", CacheControlAdapter(heuristic=BetterExpiresAfter(days=7), cache=cache), ) session.mount( METLINK_API_URL_PREFIX, CacheControlAdapter(heuristic=BetterExpiresAfter(days=1), cache=cache), ) session.mount( METLINK_API_URL_PREFIX + "ServiceLocation/", CacheControlAdapter(heuristic=BetterExpiresAfter(seconds=90), cache=cache), ) return session