def _get_token(self, type, data, expiration): """ Generate new token if: 1. first time 2. previous token is expired Otherwise get token from cache. """ s = Serializer(current_app.config['SECRET_KEY'], expiration) cached_token = _token_cache.get((self, type)) header = s.make_header({}) if cached_token and header['exp'] >= s.now(): return cached_token token = s.dumps(data) _token_cache[(self, type)] = token return token