本文整理汇总了Python中cachetools.func.ttl_cache方法的典型用法代码示例。如果您正苦于以下问题:Python func.ttl_cache方法的具体用法?Python func.ttl_cache怎么用?Python func.ttl_cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cachetools.func
的用法示例。
在下文中一共展示了func.ttl_cache方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify_token
# 需要导入模块: from cachetools import func [as 别名]
# 或者: from cachetools.func import ttl_cache [as 别名]
def verify_token(token):
"""Token verification callback."""
# this inner function checks if a token appears in the revoked token list
# the ttl_cache decorator from the cachetools package saves the revoked
# status for a token for one minute, to avoid lots of duplicated calls to
# the etcd service.
@ttl_cache(ttl=60)
def is_token_revoked(token):
try:
etcd_client().read('/revoked-tokens/' + token)
except EtcdKeyNotFound:
return False
return True
if not current_app.config['TESTING'] and is_token_revoked(token):
return False
secret_key = current_app.config['JWT_SECRET_KEY']
g.jwt_claims = {}
try:
g.jwt_claims = jwt.decode(token, secret_key, algorithms=['HS256'])
except:
# we really don't care what is the error here, any tokens that do not
# pass validation are rejected
return False
return True