Py学习  »  Redis

rails redis expire方法

rail_engine • 6 年前 • 878 次点击  

如果我有21天内到期的代币

Rails.cache.write token, id, expires_in: 21.days

我正在尝试编写一个方法来检查是否已过期。请告诉我方向

def check_token_expiry(token)

end
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47606
文章 [ 2 ]  |  最新文章 6 年前
Pramod Shinde
Reply   •   1 楼
Pramod Shinde    7 年前

当您指定 expires_in: 21.days 在缓存时,告诉底层缓存存储存储给定密钥的数据(这里 token )在21天内,21天后它将过期,这意味着数据不再可用于您使用的give密钥 exist? 方法检查此。

Rails.cache.exist?(token) # => true  (data is available and you read)
Rails.cache.exist?(token) # => false (cache is expired, data is not available)
Kapil Bhosale
Reply   •   2 楼
Kapil Bhosale    7 年前

如果与 token/key 是否过期。

def check_token_expiry(token)
  Rails.cache.read(token).nil?
end

基本上,尝试从缓存中读取值。如果它已过期,read方法将返回nil。