【问题标题】:Python: Store variable in function and restart time on conditionPython:在函数中存储变量并根据条件重新启动时间
【发布时间】:2021-09-06 11:54:12
【问题描述】:

这可能比我想象的要简单,但我想将令牌或类似字符串存储大约 24 小时,24 小时后,我希望存储的令牌或存储的字符串过期或没有价值。

# Function I currently have, it does not store the token and does not expire after 24 hours
import streamlit as st
def check_token(token_provided):
   strip_words = "ab 1jd9k1la0 "
   no_token = ""
   if strip_words in token_provided:
      new_token = token_provided.replace(strip_words,"")
   else:
      new_token = token_provided
   token = new_token
   return token

另外,令牌存储后,如何检查用户是否输入了正确的令牌值。

我目前正在使用 streamlit,但我不确定 Session.state 是否可行。

【问题讨论】:

  • 您需要使用某种缓存机制,或者使用 Redis 之类的缓存框架,或者您需要让脚本以“无限”状态运行,以保持变量的值内存无限期(不过我不推荐这种方法)。
  • 感谢您的洞察力。我会研究一下。

标签: python time streamlit


【解决方案1】:

Streamlit 提供了一个带有 ttl 参数的缓存装饰器:

ttl (float or None) – 在缓存中保留条目的最大秒数,如果缓存条目不应过期,则为 None。默认为无。

https://docs.streamlit.io/en/stable/api.html?highlight=cache#streamlit.cache

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2013-11-29
    • 2020-12-28
    • 1970-01-01
    • 2019-12-29
    • 2022-11-19
    • 1970-01-01
    相关资源
    最近更新 更多